-
Notifications
You must be signed in to change notification settings - Fork 1
/
hyperloglog_redis_test.go
157 lines (129 loc) · 3.16 KB
/
hyperloglog_redis_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
package gostatix
import (
"math"
"math/rand"
"strconv"
"testing"
)
func TestHyperLogLogRedis(t *testing.T) {
initMockRedis()
numDistinct := 100
h, _ := NewHyperLogLogRedis(128)
for i := 0; i < 1000; i++ {
data := []byte(strconv.FormatInt(int64(i), 10))
h.Update(data)
}
distinctVals, _ := h.Count(true, true)
if math.Abs(float64(distinctVals)-float64(numDistinct)) > 2 {
t.Errorf("too much variance in calculated distinct values; got %d, exact %d", distinctVals, numDistinct)
}
}
func TestHyperLogLogRedisMerge(t *testing.T) {
initMockRedis()
f, _ := NewHyperLogLogRedis(16)
g, _ := NewHyperLogLogRedis(16)
h, _ := NewHyperLogLogRedis(16)
i, _ := NewHyperLogLogRedis(16)
f.Update([]byte("foo"))
f.Update([]byte("bar"))
g.Update([]byte("abc"))
g.Update([]byte("xyz"))
h.Merge(g)
h.Merge(f)
i.Merge(f)
i.Merge(g)
ok, _ := h.Equals(i)
if !ok {
t.Errorf("h and i should be equal")
}
}
func TestHyperLogLogRedisEquals(t *testing.T) {
initMockRedis()
f, _ := NewHyperLogLogRedis(32)
g, _ := NewHyperLogLogRedis(16)
h, _ := NewHyperLogLogRedis(16)
h.Update([]byte("john"))
h.Update([]byte("jane"))
g.Update([]byte("john"))
g.Update([]byte("jane"))
ok1, _ := f.Equals(g)
ok2, _ := f.Equals(h)
if ok1 || ok2 {
t.Errorf("f is neither equal to g nor h")
}
ok3, _ := h.Equals(g)
if !ok3 {
t.Errorf("h and g should be equal")
}
g.Update([]byte("alice"))
ok3, _ = h.Equals(g)
if ok3 {
t.Errorf("h and g shouldn't be equal")
}
}
func TestHyperLogLogRedisImportExport(t *testing.T) {
initMockRedis()
g, _ := NewHyperLogLogRedis(16)
h, _ := NewHyperLogLogRedis(16)
h.Update([]byte("foo"))
h.Update([]byte("bar"))
h.Update([]byte("baz"))
g.Update([]byte("foo"))
g.Update([]byte("bar"))
g.Update([]byte("baz"))
s2, _ := g.Export()
f, _ := NewHyperLogLogRedis(16)
f.Import(s2, true)
ok1, _ := g.Equals(f)
ok2, _ := h.Equals(f)
if !ok1 || !ok2 {
t.Errorf("h, g and f should be same")
}
}
func TestHyperLogLogRedisFromKey(t *testing.T) {
initMockRedis()
h, _ := NewHyperLogLogRedis(32)
h.Update([]byte("john"))
h.Update([]byte("jane"))
g, _ := NewHyperLogLogRedisFromKey(h.MetadataKey())
ok, _ := h.Equals(g)
if !ok {
t.Errorf("h and g should be equal")
}
}
func BenchmarkHLLRedisUpdate8192(b *testing.B) {
b.StopTimer()
connOpts, _ := ParseRedisURI("redis://127.0.0.1:6379")
MakeRedisClient(*connOpts)
h, _ := NewHyperLogLogRedis(8192)
b.StartTimer()
for i := 0; i < b.N; i++ {
h.Update([]byte(strconv.FormatUint(rand.Uint64(), 10)))
}
}
func BenchmarkHLLRedisCount8192(b *testing.B) {
b.StopTimer()
connOpts, _ := ParseRedisURI("redis://127.0.0.1:6379")
MakeRedisClient(*connOpts)
h, _ := NewHyperLogLogRedis(8192)
for i := 0; i < 10000; i++ {
h.Update([]byte(strconv.FormatUint(rand.Uint64(), 10)))
}
b.StartTimer()
for i := 0; i < b.N; i++ {
h.Count(true, true)
}
}
func BenchmarkHLLRedisCount65536(b *testing.B) {
b.StopTimer()
connOpts, _ := ParseRedisURI("redis://127.0.0.1:6379")
MakeRedisClient(*connOpts)
h, _ := NewHyperLogLogRedis(8192)
for i := 0; i < 1000000; i++ {
h.Update([]byte(strconv.FormatUint(rand.Uint64(), 10)))
}
b.StartTimer()
for i := 0; i < b.N; i++ {
h.Count(true, true)
}
}