-
Notifications
You must be signed in to change notification settings - Fork 8
/
gohll_test.go
246 lines (193 loc) · 4.62 KB
/
gohll_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// +build go1.1
package gohll
import (
"fmt"
"hash/fnv"
"math"
"math/rand"
"runtime/debug"
"testing"
"github.com/stretchr/testify/assert"
)
func checkErrorBounds(t *testing.T, c, i, errorRate float64) {
actualError := math.Abs(c/(i-1) - 1)
if actualError > 3*errorRate {
debug.PrintStack()
t.Fatalf("Error too high for cardinality estimation: %f should be %f (rate of %f instead of %f)", c, i-1, actualError, errorRate)
}
}
func fnv1a(s string) uint64 {
h := fnv.New64a()
h.Write(byteSlice(s))
return h.Sum64()
}
func BenchmarkAddSparse(b *testing.B) {
h, _ := NewHLL(20)
h.sparseList.MaxSize = 1e8
b.ReportAllocs()
b.ResetTimer()
for i := 0; i <= b.N; i++ {
h.Add(fmt.Sprintf("%d", int(i)))
}
}
func BenchmarkAddNormal(b *testing.B) {
h, _ := NewHLL(20)
h.ToNormal()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i <= b.N; i++ {
h.Add(fmt.Sprintf("%d", int(i)))
}
}
func BenchmarkAddSparseFNV1A(b *testing.B) {
h, _ := NewHLL(20)
h.Hasher = fnv1a
h.sparseList.MaxSize = 1e8
b.ReportAllocs()
b.ResetTimer()
for i := 0; i <= b.N; i++ {
h.Add(fmt.Sprintf("%d", int(i)))
}
}
func BenchmarkAddNormalFNV1A(b *testing.B) {
h, _ := NewHLL(20)
h.Hasher = fnv1a
h.ToNormal()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i <= b.N; i++ {
h.Add(fmt.Sprintf("%d", int(i)))
}
}
func BenchmarkCardinalityNormal(b *testing.B) {
h, _ := NewHLL(20)
h.ToNormal()
for i := 0; i <= 10000; i++ {
h.Add(fmt.Sprintf("%d", int(i)))
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i <= b.N; i++ {
h.Cardinality()
}
}
func BenchmarkCardinalitySparse(b *testing.B) {
h, _ := NewHLL(20)
h.sparseList.MaxSize = 1e8
for i := 0; i <= 10000; i++ {
h.Add(fmt.Sprintf("%d", int(i)))
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i <= b.N; i++ {
h.Cardinality()
}
}
func TestHolistic(t *testing.T) {
h, err := NewHLL(10)
assert.Nil(t, err)
errorRate := 1.04 / math.Sqrt(float64(h.m1))
var i float64
for i = 0.0; i < 1000000; i++ {
h.Add(fmt.Sprintf("%d-%d", int(i), rand.Uint32()))
if int(i+1001)%1000 == 0 {
c := h.Cardinality()
checkErrorBounds(t, c, i, errorRate)
}
}
}
func TestSparse(t *testing.T) {
h, err := NewHLL(20)
assert.Nil(t, err)
var i float64
for i = 0; i <= 50000; i++ {
h.Add(fmt.Sprintf("%d-%d", int(i), rand.Uint32()))
}
assert.Equal(t, h.format, SPARSE, "Not using sparse mode")
c := h.Cardinality()
errorRate := 1.04 / math.Sqrt(float64(h.m2))
checkErrorBounds(t, c, i, errorRate)
}
func TestNormal(t *testing.T) {
h, err := NewHLL(10)
assert.Nil(t, err)
h.ToNormal()
var i float64
for i = 0; i <= 100000; i++ {
h.Add(fmt.Sprintf("%d-%d", int(i), rand.Uint32()))
}
assert.Equal(t, h.format, NORMAL, "Not using normal mode")
c := h.Cardinality()
errorRate := 1.04 / math.Sqrt(float64(h.m1))
checkErrorBounds(t, c, i, errorRate)
}
func TestModeChange(t *testing.T) {
h, err := NewHLL(10)
assert.Nil(t, err)
var i float64
for i = 0; i < 100000; i++ {
h.Add(fmt.Sprintf("%d-%d", int(i), rand.Uint32()))
}
assert.Equal(t, h.format, NORMAL, "Did not convert to normal mode")
c := h.Cardinality()
errorRate := 1.04 / math.Sqrt(float64(h.m1))
checkErrorBounds(t, c, i, errorRate)
}
func TestUnionNormalNormal(t *testing.T) {
h1, err := NewHLL(10)
assert.Nil(t, err)
h2, err := NewHLL(10)
assert.Nil(t, err)
h1.ToNormal()
h2.ToNormal()
testSetOperations(t, h1, h2)
}
func TestUnionNormalSparse(t *testing.T) {
h1, err := NewHLL(10)
assert.Nil(t, err)
h2, err := NewHLL(10)
assert.Nil(t, err)
h1.ToNormal()
h2.sparseList.MaxSize = 1e8
testSetOperations(t, h1, h2)
}
func TestUnionSparseNormal(t *testing.T) {
h1, err := NewHLL(10)
assert.Nil(t, err)
h2, err := NewHLL(10)
assert.Nil(t, err)
h1.sparseList.MaxSize = 1e8
h2.ToNormal()
testSetOperations(t, h1, h2)
assert.Equal(t, h1.format, NORMAL, "Did not convert h1 to normal mode")
}
func TestUnionSparseSparse(t *testing.T) {
h1, err := NewHLL(10)
assert.Nil(t, err)
h2, err := NewHLL(10)
assert.Nil(t, err)
h1.sparseList.MaxSize = 1e9
h2.sparseList.MaxSize = 1e9
testSetOperations(t, h1, h2)
}
func testSetOperations(t *testing.T, h1, h2 *HLL) {
var i float64
h1Format := h1.format
for i = 0; i <= 75000; i++ {
h1.Add(fmt.Sprintf("%d", int(i)))
}
assert.Equal(t, h1Format, h1.format)
h2Format := h2.format
for i = 25000; i <= 100000; i++ {
h2.Add(fmt.Sprintf("%d", int(i)))
}
assert.Equal(t, h2Format, h2.format)
errorRate := 1.04 / math.Sqrt(float64(h1.m1))
c, _ := h1.CardinalityUnion(h2)
checkErrorBounds(t, c, i, errorRate)
c, _ = h1.CardinalityIntersection(h2)
checkErrorBounds(t, c, 50000, errorRate)
h1.Union(h2)
c = h1.Cardinality()
checkErrorBounds(t, c, i, errorRate)
}