github.com/outcaste-io/ristretto@v0.2.3/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  
    31  func TestM_NumberOfWrongs(t *testing.T) {
    32  	bf = NewBloomFilter(float64(n*10), float64(7))
    33  
    34  	cnt := 0
    35  	for i := range wordlist1 {
    36  		hash := MemHash(wordlist1[i])
    37  		if !bf.AddIfNotHas(hash) {
    38  			cnt++
    39  		}
    40  	}
    41  	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))
    42  
    43  }
    44  
    45  func TestM_JSON(t *testing.T) {
    46  	const shallBe = int(1 << 16)
    47  
    48  	bf = NewBloomFilter(float64(n*10), float64(7))
    49  
    50  	cnt := 0
    51  	for i := range wordlist1 {
    52  		hash := MemHash(wordlist1[i])
    53  		if !bf.AddIfNotHas(hash) {
    54  			cnt++
    55  		}
    56  	}
    57  
    58  	Json := bf.JSONMarshal()
    59  
    60  	// create new bloomfilter from bloomfilter's JSON representation
    61  	bf2, err := JSONUnmarshal(Json)
    62  	require.NoError(t, err)
    63  
    64  	cnt2 := 0
    65  	for i := range wordlist1 {
    66  		hash := MemHash(wordlist1[i])
    67  		if !bf2.AddIfNotHas(hash) {
    68  			cnt2++
    69  		}
    70  	}
    71  	require.Equal(t, shallBe, cnt2)
    72  }
    73  
    74  func BenchmarkM_New(b *testing.B) {
    75  	for r := 0; r < b.N; r++ {
    76  		_ = NewBloomFilter(float64(n*10), float64(7))
    77  	}
    78  }
    79  
    80  func BenchmarkM_Clear(b *testing.B) {
    81  	bf = NewBloomFilter(float64(n*10), float64(7))
    82  	for i := range wordlist1 {
    83  		hash := MemHash(wordlist1[i])
    84  		bf.Add(hash)
    85  	}
    86  	b.ResetTimer()
    87  	for r := 0; r < b.N; r++ {
    88  		bf.Clear()
    89  	}
    90  }
    91  
    92  func BenchmarkM_Add(b *testing.B) {
    93  	bf = NewBloomFilter(float64(n*10), float64(7))
    94  	b.ResetTimer()
    95  	for r := 0; r < b.N; r++ {
    96  		for i := range wordlist1 {
    97  			hash := MemHash(wordlist1[i])
    98  			bf.Add(hash)
    99  		}
   100  	}
   101  
   102  }
   103  
   104  func BenchmarkM_Has(b *testing.B) {
   105  	b.ResetTimer()
   106  	for r := 0; r < b.N; r++ {
   107  		for i := range wordlist1 {
   108  			hash := MemHash(wordlist1[i])
   109  			bf.Has(hash)
   110  		}
   111  	}
   112  }