github.com/etecs-ru/ristretto@v0.9.1/z/bbloom_test.go (about)

     1  package z
     2  
     3  import (
     4  	"crypto/rand"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  var (
    12  	wordlist1 [][]byte
    13  	n         = 1 << 16
    14  	bf        *Bloom
    15  )
    16  
    17  func TestMain(m *testing.M) {
    18  	wordlist1 = make([][]byte, n)
    19  	for i := range wordlist1 {
    20  		b := make([]byte, 32)
    21  		rand.Read(b)
    22  		wordlist1[i] = b
    23  	}
    24  	fmt.Println("\n###############\nbbloom_test.go")
    25  	fmt.Print("Benchmarks relate to 2**16 OP. --> output/65536 op/ns\n###############\n\n")
    26  
    27  	m.Run()
    28  }
    29  
    30  func TestM_NumberOfWrongs(t *testing.T) {
    31  	bf = NewBloomFilter(float64(n*10), float64(7))
    32  
    33  	cnt := 0
    34  	for i := range wordlist1 {
    35  		hash := MemHash(wordlist1[i])
    36  		if !bf.AddIfNotHas(hash) {
    37  			cnt++
    38  		}
    39  	}
    40  	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))
    41  }
    42  
    43  func TestM_JSON(t *testing.T) {
    44  	const shallBe = int(1 << 16)
    45  
    46  	bf = NewBloomFilter(float64(n*10), float64(7))
    47  
    48  	cnt := 0
    49  	for i := range wordlist1 {
    50  		hash := MemHash(wordlist1[i])
    51  		if !bf.AddIfNotHas(hash) {
    52  			cnt++
    53  		}
    54  	}
    55  
    56  	Json := bf.JSONMarshal()
    57  
    58  	// create new bloomfilter from bloomfilter's JSON representation
    59  	bf2, err := JSONUnmarshal(Json)
    60  	require.NoError(t, err)
    61  
    62  	cnt2 := 0
    63  	for i := range wordlist1 {
    64  		hash := MemHash(wordlist1[i])
    65  		if !bf2.AddIfNotHas(hash) {
    66  			cnt2++
    67  		}
    68  	}
    69  	require.Equal(t, shallBe, cnt2)
    70  }
    71  
    72  func BenchmarkM_New(b *testing.B) {
    73  	for r := 0; r < b.N; r++ {
    74  		_ = NewBloomFilter(float64(n*10), float64(7))
    75  	}
    76  }
    77  
    78  func BenchmarkM_Clear(b *testing.B) {
    79  	bf = NewBloomFilter(float64(n*10), float64(7))
    80  	for i := range wordlist1 {
    81  		hash := MemHash(wordlist1[i])
    82  		bf.Add(hash)
    83  	}
    84  	b.ResetTimer()
    85  	for r := 0; r < b.N; r++ {
    86  		bf.Clear()
    87  	}
    88  }
    89  
    90  func BenchmarkM_Add(b *testing.B) {
    91  	bf = NewBloomFilter(float64(n*10), float64(7))
    92  	b.ResetTimer()
    93  	for r := 0; r < b.N; r++ {
    94  		for i := range wordlist1 {
    95  			hash := MemHash(wordlist1[i])
    96  			bf.Add(hash)
    97  		}
    98  	}
    99  }
   100  
   101  func BenchmarkM_Has(b *testing.B) {
   102  	b.ResetTimer()
   103  	for r := 0; r < b.N; r++ {
   104  		for i := range wordlist1 {
   105  			hash := MemHash(wordlist1[i])
   106  			bf.Has(hash)
   107  		}
   108  	}
   109  }