-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtopk_test.go
165 lines (138 loc) · 3.54 KB
/
topk_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
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package approx
import (
"encoding/json"
"fmt"
"math/rand"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
/*
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
BenchmarkTopK/k=5-24 72101881 16.54 ns/op 0 B/op 0 allocs/op
BenchmarkTopK/k=100-24 71823086 16.56 ns/op 0 B/op 0 allocs/op
BenchmarkTopK/k=1000-24 72941390 16.53 ns/op 0 B/op 0 allocs/op
*/
func BenchmarkTopK(b *testing.B) {
const cardinality = 10000
data := deck(cardinality)
for _, k := range []uint{5, 100, 1000} {
topk, err := NewTopK(k)
assert.NoError(b, err)
b.Run(fmt.Sprintf("k=%d", k), func(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
topk.Update(data[n%cardinality])
}
})
}
}
func TestTopK(t *testing.T) {
const cardinality = 100
for _, k := range []uint{2, 5, 10, 15} {
k := k // capture
t.Run(fmt.Sprintf("k=%d", k), func(t *testing.T) {
k := uint(k)
topk, err := NewTopK(k)
assert.NoError(t, err)
for _, v := range deck(cardinality) {
topk.Update(v)
}
elements := topk.Values()
assert.Len(t, elements, int(k))
assert.InDelta(t, cardinality, int(topk.Cardinality()), 1)
x := 0
for i := cardinality - k; i < cardinality; i++ {
//assert.Equal(t, strconv.Itoa(int(i)), string(elements[x].Value))
assert.InDelta(t, uint32(i), elements[x].Count, 10)
x++
}
})
}
}
func TestTopK_Simple(t *testing.T) {
topk, err := NewTopK(5)
assert.NoError(t, err)
// Add 10 elements to the topk
for _, v := range deck(10) {
topk.Update(v)
}
elements := topk.Values()
assert.Len(t, elements, 5)
assert.InDelta(t, 10, int(topk.Cardinality()), 1)
// The top 5 elements should be 5, 6, 7, 8, 9
for i, e := range elements {
assert.Equal(t, strconv.Itoa(5+i), string(e.Value))
assert.Equal(t, uint32(5+i), e.Count)
}
}
func TestTopK_Reset(t *testing.T) {
topk, err := NewTopK(5)
assert.NoError(t, err)
// Check for multiple resets
for i := 0; i < 10; i++ {
for _, v := range deck(10) {
topk.Update(v)
}
// Reset the topk
out, n := topk.Reset(5)
assert.Len(t, out, 5)
assert.InDelta(t, 10, int(n), 1)
assert.Equal(t, uint(0), topk.Cardinality())
assert.Len(t, topk.Values(), 0)
assert.Equal(t, 0, int(topk.Cardinality()))
}
}
func TestTopK_Race(t *testing.T) {
topk, err := NewTopK(5)
assert.NoError(t, err)
// Check for multiple resets
for i := 0; i < 20; i++ {
go func() {
assert.NotPanics(t, func() {
for _, v := range deck(10) {
topk.Update(v)
}
topk.Values()
topk.Reset(5)
})
}()
}
}
func TestTopK_JSON(t *testing.T) {
topk, err := NewTopK(5)
assert.NoError(t, err)
// Add 10 elements to the topk
for _, v := range deck(10) {
topk.Update(v)
}
values := topk.Values()
assert.Len(t, values, 5)
// Marshal the output
encoded, err := json.Marshal(values)
assert.NoError(t, err)
assert.JSONEq(t, `[
{"value":"5","count":5},
{"value":"6","count":6},
{"value":"7","count":7},
{"value":"8","count":8},
{"value":"9","count":9}
]`, string(encoded))
}
// Generate a random set of values
func deck(n int) []string {
values := make([]string, 0, n)
for i := 0; i < n; i++ {
for j := 0; j < i; j++ {
values = append(values, strconv.Itoa(i))
}
}
// Randomly shuffle the values
for i := range values {
j := int(rand.Int63n(int64(n)))
values[i], values[j] = values[j], values[i]
}
return values
}