-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch_bench_test.go
152 lines (136 loc) · 3.88 KB
/
sketch_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package topk_test
import (
"fmt"
"math"
"math/rand/v2"
"testing"
"github.com/keilerkonzept/topk"
segmentiotopk "github.com/segmentio/topk"
)
var (
ks = []int{10, 100}
depths = []int{3, 4}
widths = []int{1024, 8192}
items = generateItems(1_000_000)
)
func generateItems(n int) []string {
items := make([]string, n)
for i := 0; i < n; i++ {
items[i] = fmt.Sprintf("item%d", i)
}
return items
}
// BenchmarkSketchAdd benchmarks the Add method of Sketch.
func BenchmarkSketchAdd(b *testing.B) {
for _, k := range ks {
for _, depth := range depths {
for _, width := range widths {
b.Run(fmt.Sprintf("K=%d_Depth=%d_Width=%d", k, depth, width), func(b *testing.B) {
sketch := topk.New(k,
topk.WithDepth(depth),
topk.WithWidth(width),
)
b.ResetTimer()
for i := 0; i < b.N; i++ {
sketch.Add(items[rand.IntN(len(items))], uint32(rand.IntN(10)))
}
})
}
}
}
}
// BenchmarkSketchIncr benchmarks the Incr method of Sketch.
func BenchmarkSketchIncr(b *testing.B) {
for _, k := range ks {
for _, depth := range depths {
for _, width := range widths {
b.Run(fmt.Sprintf("K=%d_Depth=%d_Width=%d", k, depth, width), func(b *testing.B) {
sketch := topk.New(k,
topk.WithDepth(depth),
topk.WithWidth(width),
)
b.ResetTimer()
for i := 0; i < b.N; i++ {
sketch.Incr(items[rand.IntN(len(items))])
}
})
}
}
}
}
// BenchmarkSketchCount benchmarks the Count method of Sketch.
func BenchmarkSketchCount(b *testing.B) {
for _, k := range ks {
for _, depth := range depths {
for _, width := range widths {
b.Run(fmt.Sprintf("K=%d_Depth=%d_Width=%d", k, depth, width), func(b *testing.B) {
sketch := topk.New(k,
topk.WithDepth(depth),
topk.WithWidth(width),
)
for _, item := range items {
sketch.Add(item, uint32(rand.IntN(10)))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
sketch.Count(items[rand.IntN(len(items))])
}
})
}
}
}
}
// BenchmarkSketchQuery benchmarks the Query method of Sketch.
func BenchmarkSketchQuery(b *testing.B) {
for _, k := range ks {
for _, depth := range depths {
for _, width := range widths {
b.Run(fmt.Sprintf("K=%d_Depth=%d_Width=%d", k, depth, width), func(b *testing.B) {
sketch := topk.New(k,
topk.WithDepth(depth),
topk.WithWidth(width),
)
for _, item := range items {
sketch.Add(item, uint32(rand.IntN(10)))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
sketch.Query(items[rand.IntN(len(items))])
}
})
}
}
}
}
// BenchmarkSegmentioTopkSample benchmarks the Sample method of a [github.com/segmentio/topk.HeavyKeeper].
func BenchmarkSegmentioTopkSample(b *testing.B) {
for _, k := range []int{10} {
for _, decay := range []float64{0.6, 0.8, 0.9} {
depth := max(3, int(math.Log(float64(k))))
width := max(256, int(float64(k)*math.Log(float64(k))))
b.Run(fmt.Sprintf("K=%d_Depth=%d_Width=%d_Decay=%f", k, depth, width, decay), func(b *testing.B) {
sketch := segmentiotopk.New(k, decay)
b.ResetTimer()
for i := 0; i < b.N; i++ {
sketch.Sample(items[rand.IntN(len(items))], uint32(rand.IntN(10)))
}
})
}
}
}
// BenchmarkSketchAddForComparison benchmarks the Add method of a [topk.Sketch] for comparison with the segmentio implementation.
func BenchmarkSketchAddForComparison(b *testing.B) {
for _, k := range []int{10} {
for _, decay := range []float32{0.6, 0.8, 0.9} {
depth := max(3, int(math.Log(float64(k))))
width := max(256, int(float64(k)*math.Log(float64(k))))
b.Run(fmt.Sprintf("K=%d_Depth=%d_Width=%d_Decay=%f", k, depth, width, decay), func(b *testing.B) {
sketch := topk.New(k, topk.WithDecay(decay), topk.WithDepth(depth), topk.WithWidth(width))
b.ResetTimer()
for i := 0; i < b.N; i++ {
sketch.Add(items[rand.IntN(len(items))], uint32(rand.IntN(10)))
}
})
}
}
}