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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
// Package bloomfilter is face-meltingly fast, thread-safe,
// marshalable, unionable, probability- and
// optimal-size-calculating Bloom filter in go
//
// https://github.com/steakknife/bloomfilter
//
// Copyright © 2014, 2015, 2018 Barry Allard
//
// MIT license
//
package v2
import (
"fmt"
"math/rand"
"testing"
)
// a read-only type that conforms to hash.Hash64, but only Sum64() works.
// It is set by writing the underlying value.
type hashableUint64 uint64
func (h hashableUint64) Write([]byte) (int, error) {
panic("Unimplemented")
}
func (h hashableUint64) Sum([]byte) []byte {
panic("Unimplemented")
}
func (h hashableUint64) Reset() {
panic("Unimplemented")
}
func (h hashableUint64) BlockSize() int {
panic("Unimplemented")
}
func (h hashableUint64) Size() int {
panic("Unimplemented")
}
func (h hashableUint64) Sum64() uint64 {
return uint64(h)
}
func hashableUint64Values() []hashableUint64 {
return []hashableUint64{
0,
7,
0x0c0ffee0,
0xdeadbeef,
0xffffffff,
}
}
func hashableUint64NotValues() []hashableUint64 {
return []hashableUint64{
1,
5,
42,
0xa5a5a5a5,
0xfffffffe,
}
}
func Test0(t *testing.T) {
bf, _ := New(10000, 5)
t.Log("Filled ratio before adds :", bf.PreciseFilledRatio())
for _, x := range hashableUint64Values() {
bf.Add(x)
}
t.Log("Filled ratio after adds :", bf.PreciseFilledRatio())
// these may or may not be true
for _, y := range hashableUint64Values() {
if bf.Contains(y) {
t.Log("value in set querties: may contain ", y)
} else {
t.Fatal("value in set queries: definitely does not contain ", y,
", but it should")
}
}
// these must all be false
for _, z := range hashableUint64NotValues() {
if bf.Contains(z) {
t.Log("value not in set queries: may or may not contain ", z)
} else {
t.Log("value not in set queries: definitely does not contain ", z,
" which is correct")
}
}
}
func TestUnion(t *testing.T) {
f1, _ := New(8*500, 4)
tmp, _ := New(8*500, 4)
if _, err := tmp.Union(f1); err == nil {
t.Errorf("Incompatible, should error")
}
f2, err := f1.NewCompatible()
if err != nil {
t.Fatal(err)
}
rand.Seed(1337)
// Add some content
var tests = make([]hashableUint64, 200)
for i := 0; i < len(tests); i++ {
tests[i] = hashableUint64(rand.Uint64())
if i&1 == 0 {
f1.Add(tests[i])
} else {
f2.Add(tests[i])
}
}
unionF, err := f2.Union(f1)
if err != nil {
t.Fatal(err)
}
copyF, err := unionF.Copy()
if err != nil {
t.Fatal(err)
}
for i, v := range tests {
if !unionF.Contains(v) {
t.Errorf("missing item %d", i)
}
if !copyF.Contains(v) {
t.Errorf("missing item %d", i)
}
if i&1 == 0 {
if !f1.Contains(v) {
t.Errorf("missing item %d", i)
}
if f2.Contains(v) {
t.Errorf("f2 has item it shouldn't have")
}
} else {
if !f2.Contains(v) {
t.Errorf("missing item %d", i)
}
if f1.Contains(v) {
t.Errorf("f1 has item it shouldn't have")
}
}
}
// And test merging f1 into f2
if err := f2.UnionInPlace(f1); err != nil {
t.Fatal(err)
}
for i, v := range tests {
if !f2.Contains(v) {
t.Errorf("missing item %d", i)
}
if i&1 == 0 {
if !f1.Contains(v) {
t.Errorf("missing item %d", i)
}
} else {
if f1.Contains(v) {
t.Errorf("f1 has item it shouldn't have")
}
}
}
}
func TestFPRate(t *testing.T) {
f, _ := New(8*32, 4)
f.n = 101 // "insert" 101 items
// yes we could add some more tests here...
have, want := f.FalsePosititveProbability(), 0.402507
if int(1000*have) != int(1000*want) {
t.Errorf("have %08f, want %f", have, want)
}
}
func BenchmarkAddX10kX5(b *testing.B) {
bf, _ := New(10000, 5)
b.Run("add-10kx5", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
bf.Add(hashableUint64(rand.Uint32()))
}
})
b.Run("add-10kx5-hash", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
bf.AddHash(uint64(rand.Uint32()))
}
})
}
func TestAddX10kX5(t *testing.T) {
b1, _ := New(10000, 5)
b2, _ := b1.NewCompatible()
verify := func() {
for i := 0; i < len(b1.bits); i++ {
if b1.bits[i] != b2.bits[i] {
t.Fatalf("error at bit %d!", i)
}
}
}
for i := 0; i < 1000000; i++ {
v := hashableUint64(rand.Uint32())
b1.Add(v)
b2.AddHash(v.Sum64())
verify()
if !b2.Contains(v) {
t.Fatal("contain error")
}
}
}
func BenchmarkContains1kX10kX5(b *testing.B) {
bf, _ := New(10000, 5)
for i := 0; i < 1000; i++ {
bf.Add(hashableUint64(rand.Uint32()))
}
b.Run("contains", func(b *testing.B) {
for i := 0; i < b.N; i++ {
bf.Contains(hashableUint64(rand.Uint32()))
}
})
b.Run("containsHash", func(b *testing.B) {
for i := 0; i < b.N; i++ {
bf.ContainsHash(uint64(rand.Uint32()))
}
})
}
func BenchmarkContains100kX10BX20(b *testing.B) {
rand.Seed(1337)
b.StopTimer()
bf, _ := New(10*1000*1000*1000, 20)
for i := 0; i < 100*1000; i++ {
bf.Add(hashableUint64(rand.Uint32()))
}
b.Run("contains", func(b *testing.B) {
for i := 0; i < b.N; i++ {
bf.Contains(hashableUint64(rand.Uint32()))
}
})
b.Run("containshash", func(b *testing.B) {
for i := 0; i < b.N; i++ {
bf.ContainsHash(uint64(rand.Uint32()))
}
})
}
func TestContains(t *testing.T) {
rand.Seed(1337)
bf, _ := New(10*1000*1000, 20)
for i := 0; i < 100*10000; i++ {
x := hashableUint64(rand.Uint32())
bf.Add(x)
if !bf.Contains(x) {
t.Fatalf("Did not contain newly added elem: %d", x.Sum64())
}
}
}
//BenchmarkUnionInPlace/union-8-6 15270 77848 ns/op
func BenchmarkUnionInPlace(b *testing.B) {
var filters []*Filter
b1, _ := New(813129, 6)
for i := 0; i < 2000; i++ {
b1.Add(hashableUint64(rand.Uint32()))
}
filters = append(filters, b1)
for i := 0; i < 7; i++ {
b, _ := b1.NewCompatible()
filters = append(filters, b)
}
b.ResetTimer()
b.Run("union-8", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, bx := range filters {
_ = b1.UnionInPlace(bx)
}
}
})
}
func BenchmarkContains94percentMisses(b *testing.B) {
// This test should produce about
// 5.4K hits and 94k misses
rand.Seed(1337)
b.StopTimer()
bf, _ := New(10*1000*1000, 20)
for i := 0; i < 100*1000; i++ {
bf.Add(hashableUint64(rand.Uint32()))
}
b.Run("contains", func(b *testing.B) {
for i := 0; i < b.N; i++ {
bf.Contains(hashableUint64(rand.Uint32()))
}
})
b.Run("containsHash", func(b *testing.B) {
for i := 0; i < b.N; i++ {
bf.ContainsHash(uint64(rand.Uint32()))
}
})
}
// This test is quite long-running, thus disabled
func TestHitrate(t *testing.T) {
t.Skip("Long-running test, use only for sanity-checking")
/**
After changes:
Fill ratio: 9.303936 %
Theoretical hitrate : 0.007493 %
Hit rate (100K random tests): 0.009000 % (9 out of 100000)
Hit rate (100K random tests): 0.009000 % (9 out of 100000)
Zero-filter Hit rate (100K random tests): 9.373000 % (9373 out of 100000)
1-filter Hit rate: 9.474021 % (888 out of 9373)
Original changes:
Fill ratio: 9.303647 %
Theoretical hitrate : 0.007492 %
Hit rate (100K random tests): 2.658000 % (2658 out of 100000)
Zero-filter Hit rate (100K random tests): 9.456000 % (9456 out of 100000)
1-filter Hit rate: 53.489848 % (5058 out of 9456)
*/
// 512 MB bloom filter
f, _ := New(512*1024*1024*8, 4)
// Fill it with 100M items
for i := 0; i < 100*1024*1024; i++ {
val := rand.Uint64()
f.AddHash(val)
if !f.ContainsHash(val) {
t.Fatalf("Missing value (just inserted) %d", val)
}
}
// Test individual matches
numTests := 100000
hits := 0
for i := 0; i < numTests; i++ {
h := rand.Uint64()
if f.ContainsHash(h) {
hits++
}
}
fmt.Printf("Error rate: %f %%\n", 100*f.FalsePosititveProbability())
// With four keys, we should obtain fillrate^4 chance of false positive
fp := f.PreciseFilledRatio()
fmt.Printf("Fill ratio: %02f %%\n", 100*fp)
fmt.Printf("Theoretical hitrate : %02f %%\n", 100*fp*fp*fp*fp)
fmt.Printf("Hit rate (100K random tests): %02f %% (%d out of %d) \n", 100*float64(hits)/float64(numTests), hits, numTests)
}
|