-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathxorfilter_test.go
220 lines (199 loc) · 4.38 KB
/
xorfilter_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
package xorfilter
import (
"fmt"
"math/rand/v2"
"testing"
"time"
"unsafe"
"github.com/cespare/xxhash/v2"
"github.com/stretchr/testify/assert"
)
var rng = uint64(time.Now().UnixNano())
func TestBasic(t *testing.T) {
keys := make([]uint64, NUM_KEYS)
for i := range keys {
keys[i] = splitmix64(&rng)
}
filter, _ := Populate(keys)
for _, v := range keys {
assert.Equal(t, true, filter.Contains(v))
}
falsesize := 10000000
matches := 0
bpv := float64(len(filter.Fingerprints)) * 8.0 / float64(NUM_KEYS)
fmt.Println("Xor8 filter:")
fmt.Println("bits per entry ", bpv)
for i := 0; i < falsesize; i++ {
v := splitmix64(&rng)
if filter.Contains(v) {
matches++
}
}
fpp := float64(matches) * 100.0 / float64(falsesize)
fmt.Println("false positive rate ", fpp)
assert.Equal(t, true, fpp < 0.40)
cut := 1000
if cut > NUM_KEYS {
cut = NUM_KEYS
}
keys = keys[:cut]
for trial := 0; trial < 10; trial++ {
for i := range keys {
keys[i] = splitmix64(&rng)
}
filter, _ = Populate(keys)
for _, v := range keys {
assert.Equal(t, true, filter.Contains(v))
}
}
}
func TestSmall(t *testing.T) {
keys := make([]uint64, SMALL_NUM_KEYS)
for i := range keys {
keys[i] = splitmix64(&rng)
}
filter, _ := Populate(keys)
for _, v := range keys {
assert.Equal(t, true, filter.Contains(v))
}
falsesize := 10000000
matches := 0
for i := 0; i < falsesize; i++ {
v := splitmix64(&rng)
if filter.Contains(v) {
matches++
}
}
fpp := float64(matches) * 100.0 / float64(falsesize)
assert.Equal(t, true, fpp < 0.40)
cut := 1000
if cut > SMALL_NUM_KEYS {
cut = SMALL_NUM_KEYS
}
keys = keys[:cut]
for trial := 0; trial < 10; trial++ {
for i := range keys {
keys[i] = splitmix64(&rng)
}
filter, _ = Populate(keys)
for _, v := range keys {
assert.Equal(t, true, filter.Contains(v))
}
}
}
func BenchmarkPopulate100000(b *testing.B) {
testsize := 10000
keys := make([]uint64, testsize)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
b.StopTimer()
for i := range keys {
keys[i] = splitmix64(&rng)
}
b.StartTimer()
Populate(keys)
}
}
func encode(v1, v2 int32) []byte {
v := make([]byte, 8)
v = append(v, unsafe.Slice((*byte)(unsafe.Pointer(&v1)), 4)...)
v = append(v, unsafe.Slice((*byte)(unsafe.Pointer(&v2)), 4)...)
return v
}
// credit: el10savio
func Test_DuplicateKeys(t *testing.T) {
keys := []uint64{1, 77, 31, 241, 303, 303}
_, err := Populate(keys)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
}
func BenchmarkContains100000(b *testing.B) {
testsize := 10000
keys := make([]uint64, testsize)
for i := range keys {
keys[i] = splitmix64(&rng)
}
filter, _ := Populate(keys)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
filter.Contains(keys[n%len(keys)])
}
}
const CONSTRUCT_SIZE = 10000000
var bigrandomarray []uint64
func bigrandomarrayInit() {
if bigrandomarray == nil {
fmt.Println("bigrandomarray setup with CONSTRUCT_SIZE = ", CONSTRUCT_SIZE)
bigrandomarray = make([]uint64, CONSTRUCT_SIZE)
for i := range bigrandomarray {
bigrandomarray[i] = rand.Uint64()
}
}
}
func BenchmarkConstructXor8(b *testing.B) {
bigrandomarrayInit()
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < b.N; n++ {
Populate(bigrandomarray)
}
}
var xor8big *Xor8
func xor8bigInit() {
fmt.Println("Xor8 setup")
keys := make([]uint64, 50000000)
for i := range keys {
keys[i] = rand.Uint64()
}
xor8big, _ = Populate(keys)
fmt.Println("Xor8 setup ok")
}
func BenchmarkXor8bigContains50000000(b *testing.B) {
if xor8big == nil {
xor8bigInit()
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
xor8big.Contains(rand.Uint64())
}
}
func TestFSDIssue35_basic(t *testing.T) {
hashes := make([]uint64, 0)
for i := 0; i < 2000; i++ {
v := encode(rand.Int32N(10), rand.Int32N(100000))
hashes = append(hashes, xxhash.Sum64(v))
}
inner, err := Populate(hashes)
if err != nil {
panic(err)
}
for i, d := range hashes {
e := inner.Contains(d)
fmt.Println("checking ", d)
if !e {
panic(i)
}
}
}
func Test_Issue35_basic(t *testing.T) {
for test := 0; test < 100; test++ {
hashes := make([]uint64, 0)
for i := 0; i < 40000; i++ {
v := encode(rand.Int32N(10), rand.Int32N(100000))
hashes = append(hashes, xxhash.Sum64(v))
}
inner, err := PopulateBinaryFuse8(hashes)
if err != nil {
panic(err)
}
for i, d := range hashes {
e := inner.Contains(d)
if !e {
panic(i)
}
}
}
}