forked from shadowsocks/go-shadowsocks2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloomring_test.go
68 lines (60 loc) · 1.62 KB
/
bloomring_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
package internal_test
import (
"fmt"
"os"
"testing"
"github.com/shadowsocks/go-shadowsocks2/internal"
)
var (
bloomRingInstance *internal.BloomRing
)
func TestMain(m *testing.M) {
bloomRingInstance = internal.NewBloomRing(internal.DefaultSFSlot, int(internal.DefaultSFCapacity),
internal.DefaultSFFPR)
os.Exit(m.Run())
}
func TestBloomRing_Add(t *testing.T) {
defer func() {
if any := recover(); any != nil {
t.Fatalf("Should not got panic while adding item: %v", any)
}
}()
bloomRingInstance.Add(make([]byte, 16))
}
func TestBloomRing_Test(t *testing.T) {
buf := []byte("shadowsocks")
bloomRingInstance.Add(buf)
if !bloomRingInstance.Test(buf) {
t.Fatal("Test on filter missing")
}
}
func BenchmarkBloomRing(b *testing.B) {
// Generate test samples with different length
samples := make([][]byte, internal.DefaultSFCapacity-internal.DefaultSFSlot)
var checkPoints [][]byte
for i := 0; i < len(samples); i++ {
samples[i] = []byte(fmt.Sprint(i))
if i%1000 == 0 {
checkPoints = append(checkPoints, samples[i])
}
}
b.Logf("Generated %d samples and %d check points", len(samples), len(checkPoints))
for i := 1; i < 16; i++ {
b.Run(fmt.Sprintf("Slot%d", i), benchmarkBloomRing(samples, checkPoints, i))
}
}
func benchmarkBloomRing(samples, checkPoints [][]byte, slot int) func(*testing.B) {
filter := internal.NewBloomRing(slot, int(internal.DefaultSFCapacity), internal.DefaultSFFPR)
for _, sample := range samples {
filter.Add(sample)
}
return func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, cp := range checkPoints {
filter.Test(cp)
}
}
}
}