-
Notifications
You must be signed in to change notification settings - Fork 28
/
cuckoofilter_test.go
187 lines (155 loc) · 4.02 KB
/
cuckoofilter_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
/*
* Copyright (C) linvon
* Date 2021/2/18 10:29
*/
package cuckoo
import (
"bytes"
"crypto/rand"
"fmt"
"io"
"reflect"
"testing"
)
const size = 100000
var (
testBucketSize = []uint{2, 4, 8}
testFingerprintSize = []uint{2, 4, 5, 6, 7, 8, 9, 10, 12, 13, 16, 17, 23, 31, 32}
testTableType = []uint{TableTypeSingle, TableTypePacked}
)
func TestFilter(t *testing.T) {
var insertNum uint = 50000
var hash [32]byte
for _, b := range testBucketSize {
for _, f := range testFingerprintSize {
for _, table := range testTableType {
if f == 2 && table == TableTypePacked {
continue
}
if table == TableTypePacked && b != 4 {
continue
}
cf := NewFilter(b, f, 8190, table)
// fmt.Println(cf.Info())
a := make([][]byte, 0)
for i := uint(0); i < insertNum; i++ {
_, _ = io.ReadFull(rand.Reader, hash[:])
if cf.AddUnique(hash[:]) {
tmp := make([]byte, 32)
copy(tmp, hash[:])
a = append(a, tmp)
}
}
count := cf.Size()
if count != uint(len(a)) {
t.Errorf("Expected count = %d, instead count = %d, b %v f %v", uint(len(a)), count, b, f)
return
}
encodedBytes, err := cf.Encode()
if err != nil {
t.Fatalf("err %v", err)
}
if len(encodedBytes) != cap(encodedBytes) {
t.Fatalf("len(%d) != cap(%d)", len(encodedBytes), cap(encodedBytes))
}
ncf, err := Decode(encodedBytes)
if err != nil || !reflect.DeepEqual(cf, ncf) {
t.Errorf("Expected epual, err %v", err)
return
}
encodedBytes, err = cf.Encode()
if err != nil {
t.Fatalf("err %v", err)
}
ncf, err = DecodeFrom(encodedBytes)
if err != nil || !reflect.DeepEqual(cf, ncf) {
t.Errorf("Expected epual, err %v", err)
return
}
filterReader, _ := cf.EncodeReader()
bytesFromReader, err := io.ReadAll(filterReader)
if err != nil {
t.Fatalf("Error reading from reader")
}
if !bytes.Equal(bytesFromReader, encodedBytes) {
t.Fatalf("Expected to be equal")
}
fmt.Println(cf.Info())
cf.BitsPerItem()
cf.SizeInBytes()
cf.LoadFactor()
for _, v := range a {
if !cf.Contain(v) {
t.Errorf("Expected contain, instead not contain, b %v f %v table type %v", b, f, table)
return
}
cf.Delete(v)
}
count = cf.Size()
if count != 0 {
t.Errorf("Expected count = 0, instead count == %d, b %v f %v table type %v", count, b, f, table)
return
}
fmt.Printf("Filter bucketSize %v fingerprintSize %v tableType %v falsePositive Rate %v \n", b, f, table, cf.FalsePositiveRate())
}
}
}
}
func BenchmarkFilterSingle_Reset(b *testing.B) {
filter := NewFilter(4, 8, size, TableTypeSingle)
b.ResetTimer()
for i := 0; i < b.N; i++ {
filter.Reset()
}
}
func BenchmarkFilterSingle_Insert(b *testing.B) {
filter := NewFilter(4, 8, size, TableTypeSingle)
b.ResetTimer()
var hash [32]byte
for i := 0; i < b.N; i++ {
_, _ = io.ReadFull(rand.Reader, hash[:])
filter.Add(hash[:])
}
}
func BenchmarkFilterSingle_Lookup(b *testing.B) {
filter := NewFilter(4, 8, size, TableTypeSingle)
var hash [32]byte
for i := 0; i < size; i++ {
_, _ = io.ReadFull(rand.Reader, hash[:])
filter.Add(hash[:])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = io.ReadFull(rand.Reader, hash[:])
filter.Contain(hash[:])
}
}
func BenchmarkFilterPacked_Reset(b *testing.B) {
filter := NewFilter(4, 9, size, TableTypePacked)
b.ResetTimer()
for i := 0; i < b.N; i++ {
filter.Reset()
}
}
func BenchmarkFilterPacked_Insert(b *testing.B) {
filter := NewFilter(4, 9, size, TableTypePacked)
b.ResetTimer()
var hash [32]byte
for i := 0; i < b.N; i++ {
_, _ = io.ReadFull(rand.Reader, hash[:])
filter.Add(hash[:])
}
}
func BenchmarkFilterPacked_Lookup(b *testing.B) {
filter := NewFilter(4, 9, size, TableTypePacked)
var hash [32]byte
for i := 0; i < size; i++ {
_, _ = io.ReadFull(rand.Reader, hash[:])
filter.Add(hash[:])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = io.ReadFull(rand.Reader, hash[:])
filter.Contain(hash[:])
}
}