/
filter_example_benchmark_test.go
67 lines (54 loc) · 1.25 KB
/
filter_example_benchmark_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package bench
import (
"math/rand"
"testing"
"time"
)
func BenchmarkFilter(b *testing.B) {
for i := 0; i < 25; i++ {
n := i * 2
s := i
filterLabels, benchmarkLabels := generateNumLabels(i*2, i)
benchmark := Benchmark{
numLabels: benchmarkLabels,
}
includeFilter := Include(filterLabels)
excludeFilter := Exclude(filterLabels)
match := true
b.Run(Format(map[string]int{
"keys": n,
"length": s,
}, "include"), func(b *testing.B) {
for i := 0; i < b.N; i++ {
includeFilter.Apply(benchmark, &match)
}
})
b.Run(Format(map[string]int{
"keys": n,
"length": s,
}, "exclude"), func(b *testing.B) {
for i := 0; i < b.N; i++ {
excludeFilter.Apply(benchmark, &match)
}
})
}
}
const charset = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func generateNumLabels(n, s int) (filter, bench map[string]float64) {
filter = map[string]float64{}
bench = map[string]float64{}
seed := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < n; i++ {
b := make([]byte, s)
for c := range b {
b[c] = charset[seed.Intn(len(charset))]
}
key := string(b)
value := seed.Float64()
otherValue := seed.Float64()
filter[key] = value
bench[key] = otherValue
}
return
}