github.com/coyove/sdss@v0.0.0-20231129015646-c2ec58cca6a2/contrib/cursor/cursor_test.go (about)

     1  package cursor
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"testing"
     7  
     8  	"github.com/FastFilter/xorfilter"
     9  	"github.com/coyove/sdss/contrib/bitmap"
    10  	"github.com/coyove/sdss/contrib/clock"
    11  )
    12  
    13  func TestCursor(t *testing.T) {
    14  	rand.Seed(clock.Unix())
    15  
    16  	{
    17  		c := New()
    18  		what := 0
    19  		for i := 0; i < 1e6; i++ {
    20  			if !c.Add(bitmap.Uint64Key(uint64(i))) {
    21  				what++
    22  			}
    23  		}
    24  		fmt.Println("ok")
    25  		hits := 0
    26  		for i := 0; i < 1e6; i++ {
    27  			// if c.Contains(bitmap.Uint64Key(rand.Uint64())) {
    28  			if ok := c.Contains((bitmap.Uint64Key(rand.Uint64()))); ok {
    29  				// fmt.Println(i, idx)
    30  				hits++
    31  			}
    32  		}
    33  		fmt.Println(hits, what)
    34  	}
    35  
    36  	for n := 10; n < 1e7; n *= 8 {
    37  		c := New()
    38  		data := []uint64{}
    39  		dedup := map[uint32]bool{}
    40  		for i := 0; i < n; i++ {
    41  		AGAIN:
    42  			v := rand.Uint32()
    43  			if dedup[v] {
    44  				goto AGAIN
    45  			}
    46  			dedup[v] = true
    47  			data = append(data, uint64(v))
    48  			c.Add(bitmap.Uint64Key(uint64(v)))
    49  		}
    50  		buf := c.MarshalBinary()
    51  
    52  		xf, _ := xorfilter.Populate(data)
    53  		fmt.Println(n, len(buf), len(xf.Fingerprints)*2)
    54  
    55  		c, _ = Parse([]byte(c.String()))
    56  		// c, _ = Read(bytes.NewReader(c.MarshalBinary()))
    57  		for _, v := range data {
    58  			if !c.Contains(bitmap.Uint64Key(uint64(v))) {
    59  				panic(v)
    60  			}
    61  		}
    62  
    63  		if n < 100 {
    64  			fmt.Println(c)
    65  		}
    66  	}
    67  }
    68  
    69  func BenchmarkAdd(b *testing.B) {
    70  	c := New()
    71  	for i := 0; i < b.N; i++ {
    72  		c.Add(bitmap.Uint64Key(uint64(i)))
    73  	}
    74  }