File: bbloom_test.go

package info (click to toggle)
golang-github-dgraph-io-ristretto 0.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,160 kB
  • sloc: makefile: 3
file content (112 lines) | stat: -rw-r--r-- 2,154 bytes parent folder | download | duplicates (4)
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
package z

import (
	"crypto/rand"
	"fmt"
	"testing"

	"github.com/stretchr/testify/require"
)

var (
	wordlist1 [][]byte
	n         = 1 << 16
	bf        *Bloom
)

func TestMain(m *testing.M) {
	wordlist1 = make([][]byte, n)
	for i := range wordlist1 {
		b := make([]byte, 32)
		rand.Read(b)
		wordlist1[i] = b
	}
	fmt.Println("\n###############\nbbloom_test.go")
	fmt.Print("Benchmarks relate to 2**16 OP. --> output/65536 op/ns\n###############\n\n")

	m.Run()

}

func TestM_NumberOfWrongs(t *testing.T) {
	bf = NewBloomFilter(float64(n*10), float64(7))

	cnt := 0
	for i := range wordlist1 {
		hash := MemHash(wordlist1[i])
		if !bf.AddIfNotHas(hash) {
			cnt++
		}
	}
	fmt.Printf("Bloomfilter New(7* 2**16, 7) (-> size=%v bit): \n            Check for 'false positives': %v wrong positive 'Has' results on 2**16 entries => %v %%\n", len(bf.bitset)<<6, cnt, float64(cnt)/float64(n))

}

func TestM_JSON(t *testing.T) {
	const shallBe = int(1 << 16)

	bf = NewBloomFilter(float64(n*10), float64(7))

	cnt := 0
	for i := range wordlist1 {
		hash := MemHash(wordlist1[i])
		if !bf.AddIfNotHas(hash) {
			cnt++
		}
	}

	Json := bf.JSONMarshal()

	// create new bloomfilter from bloomfilter's JSON representation
	bf2, err := JSONUnmarshal(Json)
	require.NoError(t, err)

	cnt2 := 0
	for i := range wordlist1 {
		hash := MemHash(wordlist1[i])
		if !bf2.AddIfNotHas(hash) {
			cnt2++
		}
	}
	require.Equal(t, shallBe, cnt2)
}

func BenchmarkM_New(b *testing.B) {
	for r := 0; r < b.N; r++ {
		_ = NewBloomFilter(float64(n*10), float64(7))
	}
}

func BenchmarkM_Clear(b *testing.B) {
	bf = NewBloomFilter(float64(n*10), float64(7))
	for i := range wordlist1 {
		hash := MemHash(wordlist1[i])
		bf.Add(hash)
	}
	b.ResetTimer()
	for r := 0; r < b.N; r++ {
		bf.Clear()
	}
}

func BenchmarkM_Add(b *testing.B) {
	bf = NewBloomFilter(float64(n*10), float64(7))
	b.ResetTimer()
	for r := 0; r < b.N; r++ {
		for i := range wordlist1 {
			hash := MemHash(wordlist1[i])
			bf.Add(hash)
		}
	}

}

func BenchmarkM_Has(b *testing.B) {
	b.ResetTimer()
	for r := 0; r < b.N; r++ {
		for i := range wordlist1 {
			hash := MemHash(wordlist1[i])
			bf.Has(hash)
		}
	}
}