forked from tylertreat/BoomFilters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stable_test.go
265 lines (222 loc) · 5.71 KB
/
stable_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
package boom
import (
"math"
"strconv"
"testing"
)
// Ensures that NewUnstableBloomFilter creates a Stable Bloom Filter with p=0,
// max=1 and k hash functions.
func TestNewUnstableBloomFilter(t *testing.T) {
f := NewUnstableBloomFilter(100, 0.1)
k := OptimalK(0.1)
if f.k != k {
t.Errorf("Expected %f, got %d", k, f.k)
}
if f.m != 100 {
t.Errorf("Expected 100, got %d", f.m)
}
if f.P() != 0 {
t.Errorf("Expected 0, got %d", f.p)
}
if f.max != 1 {
t.Errorf("Expected 1, got %d", f.max)
}
}
// Ensures that Cells returns the number of cells, m, in the Stable Bloom
// Filter.
func TestCells(t *testing.T) {
f := NewStableBloomFilter(100, 1, 0.1)
if cells := f.Cells(); cells != 100 {
t.Errorf("Expected 100, got %d", cells)
}
}
// Ensures that K returns the number of hash functions in the Stable Bloom
// Filter.
func TestK(t *testing.T) {
f := NewStableBloomFilter(100, 1, 0.01)
if k := f.K(); k != 3 {
t.Errorf("Expected 3, got %d", k)
}
}
// Ensures that Test, Add, and TestAndAdd behave correctly.
func TestTestAndAdd(t *testing.T) {
f := NewDefaultStableBloomFilter(10000, 0.01)
// `a` isn't in the filter.
if f.Test([]byte(`a`)) {
t.Error("`a` should not be a member")
}
if f.Add([]byte(`a`)) != f {
t.Error("Returned StableBloomFilter should be the same instance")
}
// `a` is now in the filter.
if !f.Test([]byte(`a`)) {
t.Error("`a` should be a member")
}
// `a` is still in the filter.
if !f.TestAndAdd([]byte(`a`)) {
t.Error("`a` should be a member")
}
// `b` is not in the filter.
if f.TestAndAdd([]byte(`b`)) {
t.Error("`b` should not be a member")
}
// `a` is still in the filter.
if !f.Test([]byte(`a`)) {
t.Error("`a` should be a member")
}
// `b` is now in the filter.
if !f.Test([]byte(`b`)) {
t.Error("`b` should be a member")
}
// `c` is not in the filter.
if f.Test([]byte(`c`)) {
t.Error("`c` should not be a member")
}
for i := 0; i < 1000000; i++ {
f.TestAndAdd([]byte(strconv.Itoa(i)))
}
// `a` should have been evicted.
if f.Test([]byte(`a`)) {
t.Error("`a` should not be a member")
}
}
// Ensures that StablePoint returns the expected fraction of zeros for large
// iterations.
func TestStablePoint(t *testing.T) {
f := NewStableBloomFilter(1000, 1, 0.1)
for i := 0; i < 1000000; i++ {
f.Add([]byte(strconv.Itoa(i)))
}
zeros := 0
for i := uint(0); i < f.m; i++ {
if f.cells.Get(i) == 0 {
zeros++
}
}
actual := round(float64(zeros)/float64(f.m), 0.5, 1)
expected := round(f.StablePoint(), 0.5, 1)
if actual != expected {
t.Errorf("Expected stable point %f, got %f", expected, actual)
}
// A classic Bloom filter is a special case of SBF where P is 0 and max is
// 1. It doesn't have a stable point.
bf := NewUnstableBloomFilter(1000, 0.1)
if stablePoint := bf.StablePoint(); stablePoint != 0 {
t.Errorf("Expected stable point 0, got %f", stablePoint)
}
}
// Ensures that FalsePositiveRate returns the upper bound on false positives
// for stable filters.
func TestFalsePositiveRate(t *testing.T) {
f := NewDefaultStableBloomFilter(1000, 0.01)
fps := round(f.FalsePositiveRate(), 0.5, 2)
if fps > 0.01 {
t.Errorf("Expected fps less than or equal to 0.01, got %f", fps)
}
// Classic Bloom filters have an unbounded rate of false positives. Once
// they become full, every query returns a false positive.
bf := NewUnstableBloomFilter(1000, 0.1)
if fps := bf.FalsePositiveRate(); fps != 1 {
t.Errorf("Expected fps 1, got %f", fps)
}
}
// Ensures that Reset sets every cell to zero.
func TestReset(t *testing.T) {
f := NewDefaultStableBloomFilter(1000, 0.01)
for i := 0; i < 1000; i++ {
f.Add([]byte(strconv.Itoa(i)))
}
if f.Reset() != f {
t.Error("Returned StableBloomFilter should be the same instance")
}
for i := uint(0); i < f.m; i++ {
if cell := f.cells.Get(i); cell != 0 {
t.Errorf("Expected zero cell, got %d", cell)
}
}
}
func BenchmarkStableAdd(b *testing.B) {
b.StopTimer()
f := NewDefaultStableBloomFilter(100000, 0.01)
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++ {
f.Add(data[n])
}
}
func BenchmarkStableTest(b *testing.B) {
b.StopTimer()
f := NewDefaultStableBloomFilter(100000, 0.01)
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++ {
f.Test(data[n])
}
}
func BenchmarkStableTestAndAdd(b *testing.B) {
b.StopTimer()
f := NewDefaultStableBloomFilter(100000, 0.01)
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++ {
f.TestAndAdd(data[n])
}
}
func BenchmarkUnstableAdd(b *testing.B) {
b.StopTimer()
f := NewUnstableBloomFilter(100000, 0.1)
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++ {
f.Add(data[n])
}
}
func BenchmarkUnstableTest(b *testing.B) {
b.StopTimer()
f := NewUnstableBloomFilter(100000, 0.1)
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++ {
f.Test(data[n])
}
}
func BenchmarkUnstableTestAndAdd(b *testing.B) {
b.StopTimer()
f := NewUnstableBloomFilter(100000, 0.1)
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++ {
f.TestAndAdd(data[n])
}
}
func round(val float64, roundOn float64, places int) (newVal float64) {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
if div >= roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
newVal = round / pow
return
}