github.com/pingcap/badger@v1.5.1-0.20230103063557-828f39b09b6d/cache/z/bbloom_test.go (about)

     1  package z
     2  
     3  import (
     4  	"crypto/rand"
     5  	"fmt"
     6  	"testing"
     7  )
     8  
     9  var (
    10  	wordlist1 [][]byte
    11  	n         = 1 << 16
    12  	bf        *Bloom
    13  )
    14  
    15  func TestMain(m *testing.M) {
    16  	wordlist1 = make([][]byte, n)
    17  	for i := range wordlist1 {
    18  		b := make([]byte, 32)
    19  		rand.Read(b)
    20  		wordlist1[i] = b
    21  	}
    22  	fmt.Println("\n###############\nbbloom_test.go")
    23  	fmt.Print("Benchmarks relate to 2**16 OP. --> output/65536 op/ns\n###############\n\n")
    24  
    25  	m.Run()
    26  
    27  }
    28  
    29  func TestM_NumberOfWrongs(t *testing.T) {
    30  	bf = NewBloomFilter(float64(n*10), float64(7))
    31  
    32  	cnt := 0
    33  	for i := range wordlist1 {
    34  		hash := MemHash(wordlist1[i])
    35  		if !bf.AddIfNotHas(hash) {
    36  			cnt++
    37  		}
    38  	}
    39  	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))
    40  
    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 := JSONUnmarshal(Json)
    60  
    61  	cnt2 := 0
    62  	for i := range wordlist1 {
    63  		hash := MemHash(wordlist1[i])
    64  		if !bf2.AddIfNotHas(hash) {
    65  			cnt2++
    66  		}
    67  	}
    68  
    69  	if cnt2 != shallBe {
    70  		t.Errorf("FAILED !AddIfNotHasBytes = %v; want %v", cnt2, shallBe)
    71  	}
    72  
    73  }
    74  
    75  func BenchmarkM_New(b *testing.B) {
    76  	for r := 0; r < b.N; r++ {
    77  		_ = NewBloomFilter(float64(n*10), float64(7))
    78  	}
    79  }
    80  
    81  func BenchmarkM_Clear(b *testing.B) {
    82  	bf = NewBloomFilter(float64(n*10), float64(7))
    83  	for i := range wordlist1 {
    84  		hash := MemHash(wordlist1[i])
    85  		bf.Add(hash)
    86  	}
    87  	b.ResetTimer()
    88  	for r := 0; r < b.N; r++ {
    89  		bf.Clear()
    90  	}
    91  }
    92  
    93  func BenchmarkM_Add(b *testing.B) {
    94  	bf = NewBloomFilter(float64(n*10), float64(7))
    95  	b.ResetTimer()
    96  	for r := 0; r < b.N; r++ {
    97  		for i := range wordlist1 {
    98  			hash := MemHash(wordlist1[i])
    99  			bf.Add(hash)
   100  		}
   101  	}
   102  
   103  }
   104  
   105  func BenchmarkM_Has(b *testing.B) {
   106  	b.ResetTimer()
   107  	for r := 0; r < b.N; r++ {
   108  		for i := range wordlist1 {
   109  			hash := MemHash(wordlist1[i])
   110  			bf.Has(hash)
   111  		}
   112  	}
   113  }