This repository has been archived by the owner on Sep 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
bloomfilter_test.go
127 lines (110 loc) · 2.54 KB
/
bloomfilter_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
// 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 bloomfilter
import (
"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 BenchmarkAddX10kX5(b *testing.B) {
b.StopTimer()
bf, _ := New(10000, 5)
b.StartTimer()
for i := 0; i < b.N; i++ {
bf.Add(hashableUint64(rand.Uint32()))
}
}
func BenchmarkContains1kX10kX5(b *testing.B) {
b.StopTimer()
bf, _ := New(10000, 5)
for i := 0; i < 1000; i++ {
bf.Add(hashableUint64(rand.Uint32()))
}
b.StartTimer()
for i := 0; i < b.N; i++ {
bf.Contains(hashableUint64(rand.Uint32()))
}
}
func BenchmarkContains100kX10BX20(b *testing.B) {
b.StopTimer()
bf, _ := New(10*1000*1000*1000, 20)
for i := 0; i < 100*1000; i++ {
bf.Add(hashableUint64(rand.Uint32()))
}
b.StartTimer()
for i := 0; i < b.N; i++ {
bf.Contains(hashableUint64(rand.Uint32()))
}
}