Debian Codegraph @

bloomring_test.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package ss_bloomring
2
3 import (
4 "fmt"
5 "os"
6 "testing"
7 )
8
9 var (
10 bloomRingInstance *BloomRing
11 )
12
13 const (
14 DefaultSFCapacity = 1e6
15 // FalsePositiveRate
16 DefaultSFFPR = 1e-6
17 DefaultSFSlot = 10
18 )
19
20 func TestMain(m *testing.M) {
21 bloomRingInstance = NewBloomRing(DefaultSFSlot, int(DefaultSFCapacity),
22 DefaultSFFPR)
23 os.Exit(m.Run())
24 }
25
26 func TestBloomRing_Add(t *testing.T) {
27 defer func() {
28 if any := recover(); any != nil {
29 t.Fatalf("Should not got panic while adding item: %v", any)
30 }
31 }()
32 bloomRingInstance.Add(make([]byte, 16))
33 }
34
35 func TestBloomRing_NilAdd(t *testing.T) {
36 defer func() {
37 if any := recover(); any != nil {
38 t.Fatalf("Should not got panic while adding item: %v", any)
39 }
40 }()
41 var nilRing *BloomRing
42 nilRing.Add(make([]byte, 16))
43 }
44
45 func TestBloomRing_Test(t *testing.T) {
46 buf := []byte("shadowsocks")
47 bloomRingInstance.Add(buf)
48 if !bloomRingInstance.Test(buf) {
49 t.Fatal("Test on filter missing")
50 }
51 }
52
53 func TestBloomRing_NilTestIsFalse(t *testing.T) {
54 var nilRing *BloomRing
55 if nilRing.Test([]byte("shadowsocks")) {
56 t.Fatal("Test should return false for nil BloomRing")
57 }
58 }
59
60 func BenchmarkBloomRing(b *testing.B) {
61 // Generate test samples with different length
62 samples := make([][]byte, DefaultSFCapacity-DefaultSFSlot)
63 var checkPoints [][]byte
64 for i := 0; i < len(samples); i++ {
65 samples[i] = []byte(fmt.Sprint(i))
66 if i%1000 == 0 {
67 checkPoints = append(checkPoints, samples[i])
68 }
69 }
70 b.Logf("Generated %d samples and %d check points", len(samples), len(checkPoints))
71 for i := 1; i < 16; i++ {
72 b.Run(fmt.Sprintf("Slot%d", i), benchmarkBloomRing(samples, checkPoints, i))
73 }
74 }
75
76 func benchmarkBloomRing(samples, checkPoints [][]byte, slot int) func(*testing.B) {
77 filter := NewBloomRing(slot, int(DefaultSFCapacity), DefaultSFFPR)
78 for _, sample := range samples {
79 filter.Add(sample)
80 }
81 return func(b *testing.B) {
82 b.ResetTimer()
83 b.ReportAllocs()
84 for i := 0; i < b.N; i++ {
85 for _, cp := range checkPoints {
86 filter.Test(cp)
87 }
88 }
89 }
90 }