forked from dna2fork/zoekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloom_test.go
507 lines (456 loc) · 13.2 KB
/
bloom_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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// Copyright 2021 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zoekt // import "github.com/google/zoekt"
import (
"bytes"
"flag"
"fmt"
"io/fs"
"math"
"math/rand"
"os"
"path"
"reflect"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"testing"
)
var (
ngramDataDir = flag.String("ngramdir", "", "directory containing testdata with files with one word per line")
docCount = flag.Int("docs", 0, "number of docs to load, (default 0 for all)")
hasherNum = flag.Int("hasher", 1, "index of the hasher to test")
loadPerc = flag.String("load", "42", "space-separated lists of target load percentages")
docFpr = flag.Bool("docfpr", false, "show per-document FPRs")
trigramFpr = flag.Bool("tri", false, "compute FPR for trigram-based filtering")
)
func TestFindNextWord(t *testing.T) {
for _, tc := range []struct {
input string
want []string
}{
{
"aeiou and SOMETIMES y",
[]string{"aeiou", "sometimes"},
},
{
"\n//_azAZ09[~]3456",
[]string{"_azaz09", "3456"},
},
{
"nee\u212A aa\u212A", // kelvin degree symbol => 'k'
[]string{"neek"},
},
} {
out := []string{}
in := []byte(tc.input)
for i := 0; i < len(in); {
var s []byte
i, s = findNextWord(i, in)
if s != nil {
out = append(out, string(s))
}
}
if !reflect.DeepEqual(tc.want, out) {
t.Errorf("findNextWord(%q) got %q want %q", tc.input, out, tc.want)
}
}
}
func TestBloomHasher(t *testing.T) {
b := makeBloomFilterEmpty()
hashCount := len(b.hasher([]byte("testing")))
expected := 3 * len(strings.Split("test testi testin testing esti estin esting stin sting ting", " "))
if hashCount != expected {
t.Errorf("hasher(\"testing\") produced %d hashes instead of %d", hashCount, expected)
}
inpA := []byte("some inputs to the bloom filter hashing")
inpB := []byte("SOME inputs to the bloom filter hashing a b cd")
if !reflect.DeepEqual(b.hasher(inpA), b.hasher(inpB)) {
t.Errorf("hash(%v) => %v != hash(%v) => %v", inpA, b.hasher(inpA), inpB, b.hasher(inpB))
}
}
func TestBloomHasherStability(t *testing.T) {
want := []uint32{
0x41b0c462, 0x41b0c46c, 0x41b0c5a8, 0x79882c16, 0x79882c62, 0x79882d0f,
0x79882dbc, 0x79882d03, 0x79882d64, 0x79882cfd, 0x79882d90, 0x79882c74,
0x79882d79, 0x79882d75, 0x79882df3, 0xde692090, 0xde69219a, 0xde6920db,
0xde6920c0, 0xde6921ce, 0xde692132, 0xde6920a7, 0xde69207b, 0xde69201a,
0xde6920df, 0xde69214b, 0xde692183, 0x814351a8, 0x81435050, 0x81435090,
0x81435037, 0x814350db, 0x814350ce, 0x81435188, 0x8143509d, 0x81435113,
0x814351bc, 0x814351b6, 0x81435054, 0x88772190, 0x8877201d, 0x887720b1,
0x88772148, 0x8877208b, 0x887720b5, 0x88772154, 0x88772069, 0x887720aa,
0x8877215c, 0x8877213a, 0x887720b2, 0x3654361b, 0x36543795, 0x365436c6,
0x3654364e, 0x3654361a, 0x36543623, 0x365436ec, 0x3654365f, 0x3654364d,
0x3654368b, 0x365437a4, 0x3654375c, 0x2d64f078, 0x2d64f159, 0x2d64f105,
0x2d64f033, 0x2d64f145, 0x2d64f1ea, 0x2d64f130, 0x2d64f085, 0x2d64f029,
0x2d64f0ad, 0x2d64f188, 0x2d64f148, 0xc9ba3319, 0xc9ba326e, 0xc9ba32d9,
0xc9ba3381, 0xc9ba3331, 0xc9ba32ff, 0xc9ba320f, 0xc9ba335d, 0xc9ba3345,
0xc9ba338a, 0xc9ba32aa, 0xc9ba3273, 0xc9cb6fb7, 0xc9cb6e72, 0xc9cb6fd9,
0xc9cb6ed0, 0xc9cb6e47, 0xc9cb6ee2, 0xc9cb6e31, 0xc9cb6f8b, 0xc9cb6f06,
0x07b383c1, 0x07b383ec, 0x07b38200, 0x07b3830a, 0x07b382ec, 0x07b3838d,
0x90a95aad, 0x90a95a2a, 0x90a95bf2}
have := bloomHasherCRCBlocked64B8K3([]byte("nee\u212A STAbilizAtion??"))
if !reflect.DeepEqual(have, want) {
t.Error("Bloom hasher outputs have changed. This will break queries! Revert and add a new method.")
t.Errorf("have=%#v want=%#v", have, want)
}
}
func TestBloomZero(t *testing.T) {
var b bloom
if !b.maybeHasBytes([]byte("some example strings")) {
t.Error("bloom{}.maybeHasBytes should always return true")
}
if !b.maybeHas([]uint32{123}) {
t.Error("bloom{}.maybeHas should always return true")
}
}
func TestBloomBasic(t *testing.T) {
b := makeBloomFilterEmpty()
// Edge case: empty bloom filter resizing
b1 := b.shrinkToSize(0.9999)
if b1.Len() != 8 {
t.Error("Empty bloom filter didn't resize to 1B")
}
// Edge case: nearly empty bloom filter resizing
b.addBytes([]byte("some"))
b2 := b.shrinkToSize(0.999)
if b2.Len() != 8 {
t.Error("Nearly empty bloom filter didn't resize to 1B")
}
// these test strings are carefully selected to not collide
// with the default hash functions.
inp := []byte(`some different test words that will definitely be present
within the bloom filter`)
missed := []byte("somehow another sequences falsified probabilisitically")
b.addBytes(inp)
for i := 0; i < 90; i += 5 {
bi := b.shrinkToSize(float64(i) * .01)
t.Logf("target %d%% load: shrink %d=>%d bytes, load factor %.07f%% => %.02f%%",
i, len(b.bits), len(bi.bits), b.load()*100, bi.load()*100)
for _, w := range bytes.Split(inp, []byte{' '}) {
if !bi.maybeHasBytes(w) {
t.Errorf("%d filter should contain %q but doesn't", i, string(w))
}
}
for _, w := range bytes.Split(missed, []byte{' '}) {
if bi.maybeHasBytes(w) {
t.Errorf("%d filter shouldn't contain %q but does", i, string(w))
}
}
}
}
func BenchmarkBloomFilterResize(b *testing.B) {
f := makeBloomFilterEmpty()
rng := rand.New(rand.NewSource(123))
for i := 0; i < 1e6; i++ {
f.addBytes(randWord(4, 10, rng))
}
b.SetBytes(int64(len(f.bits)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
f.shrinkToSize(bloomDefaultLoad)
}
}
func randWord(min, max int, rng *rand.Rand) []byte {
length := rng.Intn(max-min) + max
out := make([]byte, length)
for i := 0; i < length; i++ {
out[i] = "abcdefghijklmnopqrstuvwxyz0123456789"[rng.Intn(36)]
}
return out
}
type customSort struct {
len func() int
less func(i, j int) bool
swap func(i, j int)
}
func (c *customSort) Len() int { return c.len() }
func (c *customSort) Less(i, j int) bool { return c.less(i, j) }
func (c *customSort) Swap(i, j int) { c.swap(i, j) }
func TestBloomFalsePositiveRate(t *testing.T) {
rng := rand.New(rand.NewSource(123))
var wg sync.WaitGroup
var lock sync.Mutex
cpuCount := runtime.NumCPU()
var hasher bloomHash
var hname string
switch *hasherNum {
case 0:
hasher = bloomHasherCRC
hname = "crc"
case 1:
hasher = bloomHasherCRCBlocked64B8K3
hname = "crcblock64k3_8"
}
t.Log("hasher:", hname)
targetRate := []int{}
for _, n := range strings.Split(*loadPerc, " ") {
tr, err := strconv.Atoi(n)
if err != nil {
t.Fatal(err)
}
targetRate = append(targetRate, tr)
}
t.Log("load percentage targets:", targetRate)
totsize := make([]int, len(targetRate))
docsize := 0
docs := [][]byte{}
docNames := []string{}
blooms := [][]bloom{}
addDoc := func(name string, doc []byte, parallel bool) ([]bloom, int) {
b := makeBloomFilterWithHasher(hasher)
b.addBytes(doc)
bs := []bloom{}
for _, r := range targetRate {
bs = append(bs, b.shrinkToSize(float64(r)*0.01))
}
if parallel {
lock.Lock()
}
docNames = append(docNames, name)
blooms = append(blooms, bs)
i := len(blooms)
docs = append(docs, doc)
docsize += len(doc)
for i, b := range bs {
totsize[i] += b.Len()
}
if parallel {
lock.Unlock()
}
return bs, i
}
if *ngramDataDir != "" {
dirents, err := os.ReadDir(*ngramDataDir)
if err != nil {
t.Fatal(err)
}
sort.Slice(dirents, func(i, j int) bool {
return dirents[i].Name() < dirents[j].Name()
})
if *docCount > 0 && *docCount < len(dirents) {
dirents = dirents[:*docCount]
}
work := make(chan fs.DirEntry)
for i := 0; i < cpuCount; i++ {
go func() {
for dirent := range work {
doc, err := os.ReadFile(path.Join(*ngramDataDir, dirent.Name()))
if err != nil {
t.Error(err)
return
}
b, i := addDoc(dirent.Name(), doc, true)
if i%100 == 0 {
fmt.Println(i, bytes.Count(doc, []byte{'\n'}), b[0].Len(), b[0].load(),
dirent.Name())
}
wg.Done()
}
}()
}
for _, dirent := range dirents {
if dirent.IsDir() {
continue
}
wg.Add(1)
work <- dirent
}
close(work)
wg.Wait()
} else {
if *docCount == 0 {
*docCount = 4
}
for i := 0; i < *docCount; i++ {
wordCount := 100 + rng.Intn(100)*rng.Intn(100)
doc := []byte{}
for j := 0; j < wordCount; j++ {
doc = append(doc, randWord(4, 7, rng)...)
doc = append(doc, '\n')
}
b, l := addDoc(fmt.Sprintf("%04d", i), doc, false)
t.Log(l, wordCount, b[0].Len(), b[0].load())
}
}
// sort docs by name for more deterministic output
sort.Sort(&customSort{
len: func() int { return len(docs) },
less: func(i, j int) bool { return docNames[i] < docNames[j] },
swap: func(i, j int) {
docNames[i], docNames[j] = docNames[j], docNames[i]
docs[i], docs[j] = docs[j], docs[i]
blooms[i], blooms[j] = blooms[j], blooms[i]
},
})
t.Logf("loaded %d docs (%d MB / avg %d KB)", len(docs), docsize/1024/1024, docsize/len(docs)/1024)
probes := [][]byte{}
probeHashes := [][]uint32{}
for _, doc := range docs {
ws := bytes.Split(doc, []byte{'\n'})
n := 0
for _, w := range ws {
if len(w) == 0 || len(w) > 20 {
continue
}
if w[0] < '0' || w[0] > '9' {
ws[n] = w
n++
}
}
ws = ws[:n]
rng.Shuffle(len(ws), func(i, j int) {
ws[i], ws[j] = ws[j], ws[i]
})
if len(ws) > 100 {
ws = ws[:100]
}
if len(docs) > 1000 && len(ws) > 10 {
ws = ws[:10]
}
probes = append(probes, ws...)
for _, w := range ws {
probeHashes = append(probeHashes, blooms[0][0].hasher(w))
}
}
t.Logf("created %d probes", len(probes))
fpCount := make([][]int, len(docs)) // false positive
tpCount := make([][]int, len(docs)) // true positive
tnCount := make([][]int, len(docs)) // true negative
// false negative is impossible in bloom filters
for i := 0; i < len(docs); i++ {
fpCount[i] = make([]int, len(targetRate)+1)
tpCount[i] = make([]int, len(targetRate)+1)
tnCount[i] = make([]int, len(targetRate)+1)
}
work := make(chan int)
for n := 0; n < cpuCount; n++ {
wg.Add(1)
go func() {
for i := range work {
// compute all ngrams that might be tested to reduce probing
// time complexity from O(mn) to O(mlogn+nlogn)
gram := make([]string, 0, len(docs[i])/8)
// also compute trigrams for FPR baseling
trigrams := map[string]bool{}
for _, s := range bytes.Split(docs[i], []byte{'\n'}) {
for i := 0; i <= len(s)-4; i++ {
if '0' <= s[i] && s[i] <= '9' {
continue
}
for j := i + 4; j < i+20 && j <= len(s); j++ {
gram = append(gram, string(s[i:j]))
}
if *trigramFpr {
trigrams[string(s[i:i+3])] = true
trigrams[string(s[i+1:i+4])] = true
}
}
}
sort.Strings(gram)
for j, w := range probes {
gidx := -1
trueValue := false
if *trigramFpr {
maybeTrigrams := true
for wo := 0; wo < len(w)-3; wo++ {
if !trigrams[string(w[wo:wo+3])] {
maybeTrigrams = false
break
}
}
if maybeTrigrams {
gidx = sort.SearchStrings(gram, string(w))
trueValue = gidx >= 0 && gidx < len(gram) && gram[gidx] == string(w)
if trueValue {
tpCount[i][len(targetRate)]++
} else {
fpCount[i][len(targetRate)]++
}
} else {
tnCount[i][len(targetRate)]++
}
}
for bn, b := range blooms[i] {
maybeHas := b.maybeHas(probeHashes[j])
if maybeHas {
if gidx == -1 {
gidx = sort.SearchStrings(gram, string(w))
trueValue = gidx >= 0 && gidx < len(gram) && gram[gidx] == string(w)
}
if trueValue {
tpCount[i][bn]++
} else {
fpCount[i][bn]++
}
} else {
tnCount[i][bn]++
}
}
}
}
wg.Done()
}()
}
for i := 0; i < len(docs); i++ {
work <- i
}
close(work)
wg.Wait()
summer := make([]kahanSummer, len(targetRate)+1)
for i := 0; i < len(docs); i++ {
fprs := []string{}
for bn := 0; bn < len(targetRate); bn++ {
fpr := float64(fpCount[i][bn]) / float64(fpCount[i][bn]+tnCount[i][bn])
if math.IsNaN(fpr) {
fpr = 1
}
if fpr > 0.1 {
t.Errorf("false positive rate %.04f > 0.01", fpr)
}
summer[bn].add(fpr)
if *docFpr {
fprs = append(fprs, fmt.Sprintf("%5.2f", 100*fpr))
}
}
if *docFpr {
fmt.Printf("doc: %4d bits: %8d fprs: %v name: %s\n", i, blooms[i][0].Len(), fprs[:len(targetRate)], docNames[i])
}
}
t.Logf("hash=%s", hname)
t.Log("load,fpr,avg size")
for bn, rate := range targetRate {
t.Logf("%d, %.03f, %d\n", rate, 100*summer[bn].avg(), totsize[bn]/8/len(docs))
}
if *trigramFpr {
t.Logf("trigram fpr: %.03f\n", 100*summer[len(targetRate)].avg())
}
}
type kahanSummer struct { // Kahan Summation
sum float64
c float64
n int
}
func (k *kahanSummer) add(x float64) {
y := x - k.c
t := k.sum + y
k.c = (t - k.sum) - y
k.sum = t
k.n++
}
func (k *kahanSummer) avg() float64 {
return k.sum / float64(k.n)
}