-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigbloom_test.go
298 lines (260 loc) · 7.22 KB
/
bigbloom_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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package bloom
import (
"fmt"
"testing"
"strconv"
"github.com/stretchr/testify/assert"
)
func TestNewBigBloomFromCapacity(t *testing.T) {
// test zero capacity
_, err := NewBigBloomFromCap(32, 0)
assert.EqualError(t, err, "capacity cannot be less than 1")
}
func TestNewBigBloomFromAcc(t *testing.T) {
// test zero accuracy
_, err := NewBigBloomFromAcc(32, 0)
assert.EqualError(t, err, "false positive rate must be between 0 and 1")
// test one accuracy
_, err = NewBigBloomFromAcc(32, 1)
assert.EqualError(t, err, "false positive rate must be between 0 and 1")
}
func TestNewBigBloomFromK(t *testing.T) {
// test zero k
_, err := NewBigBloomFromK(32, 0)
assert.EqualError(t, err, "k cannot be less than 1")
}
func TestNewBigBloomAlloc(t *testing.T) {
// test zero capacity
zeroCap := 0
_, err := NewBigBloomAlloc(zeroCap, 1)
assert.EqualError(t, err, "capacity cannot be less than 1")
// test zero false postive rate
zeroAcc := float64(0)
_, err = NewBigBloomAlloc(1, zeroAcc)
assert.EqualError(t, err, "false positive rate must be between 0 and 1")
type allocTest struct {
cap int
acc float64
expectedLen int
}
// wolfram alpha: https://www.wolframalpha.com/input?i2d=true&i=Power%5B%5C%2840%291-Power%5B%5C%2840%291%E2%88%92%5C%2840%29Divide%5B1%2C4096%5D%5C%2841%29%5C%2841%29%2C3000%5D%5C%2841%29%2C3%5D
tests := []allocTest{
{
cap: 10,
acc: 0.0013597239492769301702394684205393411304012623906373231471869292,
expectedLen: 18, // 144 bits
},
{
cap: 1000,
acc: 0.1400406877800123403129581978899597802443405570160297883718149039,
expectedLen: 512, // 512 bytes
},
}
for _, test := range tests {
b, _ := NewBigBloomAlloc(test.cap, test.acc)
assert.Equal(t, test.expectedLen, b.len)
}
}
func TestNewBigBloomFromBytes(t *testing.T) {
var bs []byte
// test empty bloom
_, err := NewBigBloomFromBytes(bs, 1)
assert.EqualError(t, err, "bloom filter length cannot be 0")
// test zero k
bs = make([]byte, 1)
_, err = NewBigBloomFromBytes(bs, 0)
assert.EqualError(t, err, "k cannot be less than 1")
}
// TestPutStr also tests PutBytes because PutStr calls PutBytes
// most of put functionality tested in TestExistsStr
func TestBigBloomPutStr(t *testing.T) {
b, err := NewBigBloomFromK(32, testk)
assert.Nil(t, err)
// make sure n increases on put
b.PutStr("test")
assert.Equal(t, 1, b.n)
// make sure n stays the same after same insertion
b.PutStr("test")
assert.Equal(t, 1, b.n)
}
// TestExistsStr also tests ExistsBytes because ExistsStr calls ExistsBytes
func TestBigBloomExistsStr(t *testing.T) {
type existTest struct {
entry string
expected bool
}
validEntries := []existTest{
{
entry: "exists1",
expected: true,
},
{
entry: "exists2",
expected: true,
},
{
entry: "exists3",
expected: true,
},
}
invalidEntries := []existTest{
{
entry: "not-exists1",
expected: false,
},
{
entry: "not-exists2",
expected: false,
},
{
entry: "not-exists3",
expected: false,
},
}
tests := append(validEntries, invalidEntries...)
// small filter test
// 10 bytes will require 1 hash
b10, err := NewBigBloomFromK(5, testk)
assert.Nil(t, err)
// populate
for _, test := range validEntries {
b10.PutStr(test.entry)
}
// check if exists
for _, test := range tests {
got, _ := b10.ExistsStr(test.entry)
assert.Equal(t, test.expected, got)
}
// big filter test
// 1000 bytes will require 32 hashes
b1000, err := NewBigBloomFromK(1000, 3)
assert.Nil(t, err)
// populate
for _, test := range validEntries {
b1000.PutStr(test.entry)
}
// check if exists
for _, test := range tests {
got, _ := b1000.ExistsStr(test.entry)
assert.Equal(t, test.expected, got)
}
// huge filter test
// 10000 bytes will require 320 hashes
b10000, err := NewBigBloomFromK(10000, 3)
assert.Nil(t, err)
// populate
for _, test := range validEntries {
b10000.PutStr(test.entry)
}
// check if exists
for _, test := range tests {
got, _ := b10000.ExistsStr(test.entry)
assert.Equal(t, test.expected, got)
}
}
func TestBigBloomAccuracy(t *testing.T) {
// test if accuracy is 1 when no entries
b, err := NewBigBloomFromK(32, testk)
assert.Nil(t, err)
assert.Equal(t, float64(1), b.Accuracy())
// cannot calculate accuracy if loaded in
b.isLoaded = true
assert.Equal(t, float64(-1), b.Accuracy())
// rest of accuracy tested in TestFalsePositiveRate
}
func TestBigBlooomCapacityConstraint(t *testing.T) {
// test loaded bloom filter
bs := make([]byte, 32)
b, err := NewBigBloomFromBytes(bs, 1)
assert.Nil(t, err)
err = b.AddCapacityConstraint(100)
assert.EqualError(t, err, "cannot add constraints to loaded bloom filters")
cap := 5
b, err = NewBigBloomFromK(32, testk)
assert.Nil(t, err)
b.AddCapacityConstraint(cap)
for i := 0; i < cap; i++ {
_, err := b.PutStr(strconv.Itoa(i))
assert.Nil(t, err)
}
// test already added
_, err = b.PutStr(strconv.Itoa(0))
assert.Nil(t, err)
// should fail on 6th try
_, err = b.PutStr("fail")
assert.IsType(t, err, &CapacityError{})
// test capacity and accuracy incompatibility
cap = 1000
acc := .1
b, err = NewBigBloomFromK(32, testk)
assert.Nil(t, err)
err = b.AddAccuracyConstraint(acc)
assert.Nil(t, err)
err = b.AddCapacityConstraint(cap)
assert.EqualError(t, err, "false positive rate will be higher at full capacity than the maxFalsePositiveRate provided")
}
func TestBigBloomAccuracyConstraint(t *testing.T) {
// test loaded bloom filter
bs := make([]byte, 32)
b, err := NewBigBloomFromBytes(bs, 1)
assert.Nil(t, err)
err = b.AddAccuracyConstraint(.1)
assert.EqualError(t, err, "cannot add constraints to loaded bloom filters")
acc := float64(0.00000001)
b, err = NewBigBloomFromK(32, testk)
assert.Nil(t, err)
b.AddAccuracyConstraint(acc)
_, err = b.PutStr("fail")
assert.IsType(t, err, &AccuracyError{})
// test capacity and accuracy incompatibility
cap := 1000
acc = .1
b, err = NewBigBloomFromK(32, testk)
assert.Nil(t, err)
err = b.AddCapacityConstraint(cap)
assert.Nil(t, err)
err = b.AddAccuracyConstraint(acc) // accuracy call should fail
assert.EqualError(t, err, "false positive rate will be higher at full capacity than the maxFalsePositiveRate provided")
}
// tests huge bloom filter
func TestTrillionBitBloom(t *testing.T) {
m := 125000000000
b, err := NewBigBloomFromCap(m, 100000)
assert.Nil(t, err)
_, err = b.PutStr("test")
assert.Nil(t, err)
exists, _ := b.ExistsStr("test")
assert.True(t, exists)
exists, _ = b.ExistsStr("fail")
assert.False(t, exists)
}
//
// Benchmarks
//
// benchmark for increasing bloom filter len
func BenchmarkBigBloomPutStr(b *testing.B) {
for i := 512; i < 10000; i += 512 {
bloom, err := NewBigBloomFromK(i, 3)
assert.Nil(b, err)
b.Run(fmt.Sprintf("len_%d_bytes", i), func(b *testing.B) {
for j := 0; j < 100; j++ {
bloom.PutStr(strconv.Itoa(j))
}
})
}
}
// benchmark for exists for increasing bloom filter len
func BenchmarkBigBloomExistsStr(b *testing.B) {
for i := 512; i < 10000; i += 512 {
bloom, err := NewBigBloomFromK(i, 3)
assert.Nil(b, err)
for j := 0; j < 100; j++ {
bloom.PutStr(strconv.Itoa(j))
}
b.Run(fmt.Sprintf("len_%d_bytes", i), func(b *testing.B) {
for j := 0; j < 100; j++ {
bloom.ExistsStr(strconv.Itoa(j))
}
})
}
}