go-jsonnet/ast/util_test.go
Leandro López 067dc391aa Add IdentifierSet.ToOrderedSlice and ToSlice benchmarks
This will be useful later on when implementing the performance
improvements to compare how much we gain.

Signed-off-by: Leandro López <leandro.lopez@grafana.com>
2021-03-25 15:44:43 +01:00

65 lines
1.2 KiB
Go

package ast
import (
"fmt"
"testing"
)
func testIdentifiers(n int) Identifiers {
var is []Identifier
for i := 0; i < n; i++ {
is = append(is, Identifier(fmt.Sprintf("id-%06d", i)))
}
return Identifiers(is)
}
var results []Identifier
func BenchmarkToOrderedSlice(b *testing.B) {
tests := []Identifiers{
testIdentifiers(1),
testIdentifiers(10),
testIdentifiers(100),
testIdentifiers(1000),
testIdentifiers(10000),
testIdentifiers(100000),
}
for _, t := range tests {
is := IdentifierSet{}
is.AddIdentifiers(t)
b.Run(fmt.Sprintf("%d unique identifiers", len(t)), func(b *testing.B) {
var r []Identifier
for i := 0; i < b.N; i++ {
r = is.ToOrderedSlice()
}
results = r
})
}
}
func BenchmarkToSlice(b *testing.B) {
tests := []Identifiers{
testIdentifiers(1),
testIdentifiers(10),
testIdentifiers(100),
testIdentifiers(1000),
testIdentifiers(10000),
testIdentifiers(100000),
}
for _, t := range tests {
is := IdentifierSet{}
is.AddIdentifiers(t)
b.Run(fmt.Sprintf("%d unique identifiers", len(t)), func(b *testing.B) {
var r []Identifier
for i := 0; i < b.N; i++ {
r = is.ToSlice()
}
results = r
})
}
}