-
Notifications
You must be signed in to change notification settings - Fork 112
/
topk_test.go
77 lines (64 loc) · 1.66 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
package boom
import (
"strconv"
"testing"
)
// Ensures that TopK return the top-k most frequent elements.
func TestTopK(t *testing.T) {
topk := NewTopK(0.001, 0.99, 5)
topk.Add([]byte(`bob`)).Add([]byte(`bob`)).Add([]byte(`bob`))
topk.Add([]byte(`tyler`)).Add([]byte(`tyler`)).Add([]byte(`tyler`)).Add([]byte(`tyler`)).Add([]byte(`tyler`))
topk.Add([]byte(`fred`))
topk.Add([]byte(`alice`)).Add([]byte(`alice`)).Add([]byte(`alice`)).Add([]byte(`alice`))
topk.Add([]byte(`james`))
topk.Add([]byte(`fred`))
topk.Add([]byte(`sara`)).Add([]byte(`sara`))
if topk.Add([]byte(`bill`)) != topk {
t.Error("Returned TopK should be the same instance")
}
// latest one also
expected := []struct {
name string
freq uint64
}{
{"fred", 2},
{"sara", 2},
{"bob", 3},
{"alice", 4},
{"tyler", 5},
}
actual := topk.Elements()
if l := len(actual); l != 5 {
t.Errorf("Expected len %d, got %d", 5, l)
}
for i, element := range actual {
if e := string((*element).Data); e != expected[i].name {
t.Errorf("Expected %s, got %s", expected[i].name, e)
}
// freq check
if freq := element.Freq; freq != expected[i].freq {
t.Errorf("Expected %d, got %d", expected[i].freq, freq)
}
}
if topk.Reset() != topk {
t.Error("Returned TopK should be the same instance")
}
if l := topk.elements.Len(); l != 0 {
t.Errorf("Expected 0, got %d", l)
}
if n := topk.n; n != 0 {
t.Errorf("Expected 0, got %d", n)
}
}
func BenchmarkTopKAdd(b *testing.B) {
b.StopTimer()
topk := NewTopK(0.001, 0.99, 5)
data := make([][]byte, b.N)
for i := 0; i < b.N; i++ {
data[i] = []byte(strconv.Itoa(i))
}
b.StartTimer()
for n := 0; n < b.N; n++ {
topk.Add(data[n])
}
}