github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/bindex/vector_index_test.go (about)

     1  package bindex
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestVectorIndex_Perf32(t *testing.T) {
    13  	const count = 10 << 20
    14  
    15  	golist := make([]uint32, 0, count)
    16  	gomap := make(map[uint32]uint32, count)
    17  	for i := 0; i < count; i++ {
    18  		tmp := uint32(rand.Int31n(1 << 30))
    19  		gomap[tmp] = tmp
    20  	}
    21  
    22  	vindex := NewVectorIndex()
    23  	vindex.InitWriter(count, VectorValTypeUint32)
    24  
    25  	hindex := NewHashIndex(true)
    26  	hindex.InitWriter()
    27  
    28  	for k, v := range gomap {
    29  		vindex.Add32(k, v)
    30  		hindex.Add(k, v)
    31  		golist = append(golist, k)
    32  	}
    33  
    34  	vindex.Serialize()
    35  	hindex.Serialize()
    36  
    37  	llen := len(golist)
    38  
    39  	errno := 0
    40  	bt := time.Now()
    41  	for i := 0; i < llen; i++ {
    42  		v, ok := gomap[golist[i]]
    43  		if !ok || v != golist[i] {
    44  			errno++
    45  		}
    46  	}
    47  	et := time.Since(bt)
    48  	fmt.Printf("gomap cost=%v; llen=%d; errno=%d\n", et, llen, errno)
    49  
    50  	errno = 0
    51  	bt = time.Now()
    52  	for i := 0; i < llen; i++ {
    53  		v, ok := vindex.Get32(golist[i])
    54  		if !ok || v != golist[i] {
    55  			errno++
    56  		}
    57  	}
    58  	et = time.Since(bt)
    59  	fmt.Printf("vector_index cost=%v; llen=%d; errno=%d; size=%dMB\n", et, llen, errno, vindex.Size()/1024/1024)
    60  
    61  	errno = 0
    62  	bt = time.Now()
    63  	for i := 0; i < llen; i++ {
    64  		v, ok := hindex.Get32(golist[i])
    65  		if !ok || v != golist[i] {
    66  			errno++
    67  		}
    68  	}
    69  	et = time.Since(bt)
    70  	fmt.Printf("hash_index cost=%v; llen=%d; errno=%d; size=%dMB\n", et, llen, errno, hindex.size/1024/1024)
    71  
    72  	vindex.Finish()
    73  	hindex.Finish()
    74  }
    75  
    76  func TestVectorIndex_AddUint32(t *testing.T) {
    77  	vindex := NewVectorIndex()
    78  	vindex.InitWriter(5, VectorValTypeUint32)
    79  	for i := 0; i < 256; i++ {
    80  		vindex.Add32(uint32(i), uint32(i))
    81  	}
    82  	var data = make([]byte, vindex.Size())
    83  	vindex.SetWriter(data)
    84  	vindex.Serialize()
    85  	for i := 0; i < 256; i++ {
    86  		v, ok := vindex.innerMemGet32(uint32(i))
    87  		assert.Equal(t, true, ok)
    88  		assert.Equal(t, uint32(i), v)
    89  	}
    90  	vindex.Finish()
    91  }
    92  
    93  func TestVectorIndex_AddUint64(t *testing.T) {
    94  	vindex := NewVectorIndex()
    95  	vindex.InitWriter(5, VectorValTypeUint64)
    96  	for i := 0; i < 256; i++ {
    97  		vindex.Add64(uint32(i), uint64(i))
    98  	}
    99  	var data = make([]byte, vindex.Size())
   100  	vindex.SetWriter(data)
   101  	vindex.Serialize()
   102  	for i := 0; i < 256; i++ {
   103  		v, ok := vindex.innerMemGet64(uint32(i))
   104  		assert.Equal(t, true, ok)
   105  		assert.Equal(t, uint64(i), v)
   106  	}
   107  	vindex.Finish()
   108  }
   109  
   110  func TestVectorIndex_AddGetUint32(t *testing.T) {
   111  	vindex := NewVectorIndex()
   112  	vindex.InitWriter(5, VectorValTypeUint32)
   113  	for i := 0; i < 256; i++ {
   114  		vindex.Add32(uint32(i), uint32(i))
   115  	}
   116  	var data = make([]byte, vindex.Size())
   117  	vindex.SetWriter(data)
   118  	vindex.Serialize()
   119  
   120  	ni := NewVectorIndex()
   121  	ni.SetReader(data)
   122  	for i := 0; i < 256; i++ {
   123  		v, ok := ni.Get32(uint32(i))
   124  		assert.Equal(t, true, ok)
   125  		assert.Equal(t, uint32(i), v)
   126  	}
   127  	vindex.Finish()
   128  }
   129  
   130  func TestVectorIndex_AddGetUint64(t *testing.T) {
   131  	vindex := NewVectorIndex()
   132  	vindex.InitWriter(5, VectorValTypeUint64)
   133  	for i := 0; i < 256; i++ {
   134  		vindex.Add64(uint32(i), uint64(i))
   135  	}
   136  	var data = make([]byte, vindex.Size())
   137  	vindex.SetWriter(data)
   138  	vindex.Serialize()
   139  
   140  	ni := NewVectorIndex()
   141  	ni.SetReader(data)
   142  	for i := 0; i < 256; i++ {
   143  		v, ok := ni.Get64(uint32(i))
   144  		assert.Equal(t, true, ok)
   145  		assert.Equal(t, uint64(i), v)
   146  	}
   147  	vindex.Finish()
   148  }
   149  
   150  func TestVectorIndex_AddGetRandomUint32(t *testing.T) {
   151  	vindex := NewVectorIndex()
   152  	vindex.InitWriter(256, VectorValTypeUint32)
   153  	m := make(map[uint32]uint32, 256)
   154  	for i := 0; i < 256; i++ {
   155  		k, v := uint32(rand.Int31n(1<<24)), uint32(i)
   156  		m[k] = v
   157  		vindex.Add32(k, v)
   158  	}
   159  	var data = make([]byte, vindex.Size())
   160  	vindex.SetWriter(data)
   161  	vindex.Serialize()
   162  
   163  	ni := NewVectorIndex()
   164  	ni.SetReader(data)
   165  	for k, v := range m {
   166  		v1, ok := ni.Get32(k)
   167  		assert.Equal(t, true, ok)
   168  		assert.Equal(t, v, v1)
   169  	}
   170  	vindex.Finish()
   171  }
   172  
   173  func TestVectorIndex_AddGetRandomUint64(t *testing.T) {
   174  	vindex := NewVectorIndex()
   175  	vindex.InitWriter(256, VectorValTypeUint64)
   176  	m := make(map[uint32]uint64, 256)
   177  	for i := 0; i < 256; i++ {
   178  		k, v := uint32(rand.Int31n(1<<24)), uint64(i)
   179  		m[k] = v
   180  		vindex.Add64(k, v)
   181  	}
   182  	var data = make([]byte, vindex.Size())
   183  	vindex.SetWriter(data)
   184  	vindex.Serialize()
   185  	ni := NewVectorIndex()
   186  	ni.SetReader(data)
   187  	for k, v := range m {
   188  		v1, ok := ni.Get64(k)
   189  		assert.Equal(t, true, ok)
   190  		assert.Equal(t, v, v1)
   191  	}
   192  	vindex.Finish()
   193  }
   194  
   195  func TestVectorIndex_AddGetRandomRehashUint32(t *testing.T) {
   196  	vindex := NewVectorIndex()
   197  	vindex.InitWriter(1, VectorValTypeUint32)
   198  	m := make(map[uint32]uint32, 256)
   199  	for i := 0; i < 256; i++ {
   200  		k, v := uint32(rand.Int31n(1<<24)), uint32(i)
   201  		m[k] = v
   202  		vindex.Add32(k, v)
   203  	}
   204  	var data = make([]byte, vindex.Size())
   205  	vindex.SetWriter(data)
   206  	vindex.Serialize()
   207  
   208  	ni := NewVectorIndex()
   209  	ni.SetReader(data)
   210  	for k, v := range m {
   211  		v1, ok := ni.Get32(k)
   212  		assert.Equal(t, true, ok)
   213  		assert.Equal(t, v, v1)
   214  	}
   215  	vindex.Finish()
   216  }
   217  
   218  func TestVectorIndex_AddGetRandomRehashUint64(t *testing.T) {
   219  	vindex := NewVectorIndex()
   220  	vindex.InitWriter(1, VectorValTypeUint64)
   221  	m := make(map[uint32]uint64, 256)
   222  	for i := 0; i < 256; i++ {
   223  		k, v := uint32(rand.Int31n(1<<24)), uint64(i)
   224  		m[k] = v
   225  		vindex.Add64(k, v)
   226  	}
   227  	var data = make([]byte, vindex.Size())
   228  	vindex.SetWriter(data)
   229  	vindex.Serialize()
   230  
   231  	ni := NewVectorIndex()
   232  	ni.SetReader(data)
   233  	for k, v := range m {
   234  		v1, ok := ni.Get64(k)
   235  		assert.Equal(t, true, ok)
   236  		assert.Equal(t, v, v1)
   237  	}
   238  	vindex.Finish()
   239  }
   240  
   241  func TestVector32LenCap(t *testing.T) {
   242  	vindex := NewVectorIndex()
   243  	vindex.InitWriter(1, VectorValTypeUint32)
   244  	var count uint32 = 1024
   245  	m := make(map[uint32]uint32, count)
   246  	for i := 0; i < int(count); i++ {
   247  		k, v := uint32(rand.Int31n(1<<24)), uint32(i)
   248  		m[k] = v
   249  		vindex.Add32(k, v)
   250  	}
   251  	assert.Equal(t, count, vindex.Length())
   252  	var data = make([]byte, vindex.Size())
   253  	vindex.SetWriter(data)
   254  	vindex.Serialize()
   255  
   256  	ni := NewVectorIndex()
   257  	ni.SetReader(data)
   258  	assert.Equal(t, count, ni.Length())
   259  	assert.Equal(t, uint32(0), ni.Capacity())
   260  }
   261  
   262  func TestVector64LenCap(t *testing.T) {
   263  	vindex := NewVectorIndex()
   264  	vindex.InitWriter(1, VectorValTypeUint64)
   265  	var count uint32 = 1024
   266  	for i := 0; i < int(count); i++ {
   267  		k, v := uint32(rand.Int31n(1<<24)), uint64(i)
   268  		vindex.Add64(k, v)
   269  	}
   270  	assert.Equal(t, count, vindex.Length())
   271  	var data = make([]byte, vindex.Size())
   272  	vindex.SetWriter(data)
   273  	vindex.Serialize()
   274  
   275  	ni := NewVectorIndex()
   276  	ni.SetReader(data)
   277  	assert.Equal(t, count, ni.Length())
   278  	assert.Equal(t, uint32(0), ni.Capacity())
   279  }