-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbench_test.go
61 lines (54 loc) · 1.3 KB
/
bench_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
package histogram_test
import (
"fmt"
"math"
"testing"
)
func BenchmarkAdd(b *testing.B) {
// use uniform data
data := scenarios[0].Data
for _, new := range factories {
sketch := new()
b.Run(sketch.Name(), func(b *testing.B) {
for i := 0; i < b.N; i++ {
sketch.Add(data[i%len(data)])
}
})
}
}
func BenchmarkQuery(b *testing.B) {
for _, scn := range scenarios {
b.Run(scn.Name, func(b *testing.B) {
for _, sketch := range scn.Candidates {
b.Run(sketch.Name(), func(b *testing.B) {
for i := 0; i < b.N; i += 4 {
sketch.Query(0.1)
sketch.Query(0.5)
sketch.Query(0.9)
sketch.Query(0.99)
}
})
}
})
}
}
func BenchmarkPrecision(b *testing.B) {
percentiles := []float64{.1, .25, .5, .75, .9, .95, .99}
for _, scn := range scenarios {
b.Run(scn.Name, func(b *testing.B) {
for _, pct := range percentiles {
exp := scn.Data[int(pct*float64(len(scn.Data)))-1] // actual quantile
b.Run(fmt.Sprintf("p%.0f", pct*100), func(b *testing.B) {
for _, sketch := range scn.Candidates {
b.Run(sketch.Name(), func(b *testing.B) {
got := sketch.Query(pct)
rel := math.Abs(got-exp) / (math.Abs(got) + math.Abs(exp)) * 200
b.ReportMetric(0, "ns/op")
b.ReportMetric(rel, "%err")
})
}
})
}
})
}
}