github.com/ledgerwatch/erigon-lib@v1.0.0/state/locality_index_test.go (about) 1 package state 2 3 import ( 4 "context" 5 "encoding/binary" 6 "math" 7 "sync/atomic" 8 "testing" 9 10 "github.com/ledgerwatch/log/v3" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func BenchmarkName2(b *testing.B) { 15 b.Run("1", func(b *testing.B) { 16 j := atomic.Int32{} 17 for i := 0; i < b.N; i++ { 18 j.Add(1) 19 } 20 }) 21 b.Run("2", func(b *testing.B) { 22 j := &atomic.Int32{} 23 for i := 0; i < b.N; i++ { 24 j.Add(1) 25 } 26 }) 27 } 28 29 func TestLocality(t *testing.T) { 30 logger := log.New() 31 ctx, require := context.Background(), require.New(t) 32 const Module uint64 = 31 33 path, db, ii, txs := filledInvIndexOfSize(t, 300, 4, Module, logger) 34 mergeInverted(t, db, ii, txs) 35 ic := ii.MakeContext() 36 defer ic.Close() 37 li, _ := NewLocalityIndex(path, path, 4, "inv", logger) 38 defer li.Close() 39 err := li.BuildMissedIndices(ctx, ic) 40 require.NoError(err) 41 t.Run("locality iterator", func(t *testing.T) { 42 ic := ii.MakeContext() 43 defer ic.Close() 44 it := ic.iterateKeysLocality(math.MaxUint64) 45 require.True(it.HasNext()) 46 key, bitmap := it.Next() 47 require.Equal(uint64(2), binary.BigEndian.Uint64(key)) 48 require.Equal([]uint64{0, 1}, bitmap) 49 require.True(it.HasNext()) 50 key, bitmap = it.Next() 51 require.Equal(uint64(3), binary.BigEndian.Uint64(key)) 52 require.Equal([]uint64{0, 1}, bitmap) 53 54 var last []byte 55 for it.HasNext() { 56 key, _ = it.Next() 57 last = key 58 } 59 require.Equal(Module, binary.BigEndian.Uint64(last)) 60 }) 61 62 files, err := li.buildFiles(ctx, ic, ii.endTxNumMinimax()/ii.aggregationStep) 63 require.NoError(err) 64 defer files.Close() 65 t.Run("locality index: get full bitamp", func(t *testing.T) { 66 res, err := files.bm.At(0) 67 require.NoError(err) 68 require.Equal([]uint64{0, 1}, res) 69 res, err = files.bm.At(1) 70 require.NoError(err) 71 require.Equal([]uint64{0, 1}, res) 72 res, err = files.bm.At(32) //too big, must error 73 require.Error(err) 74 require.Empty(res) 75 }) 76 77 t.Run("locality index: search from given position", func(t *testing.T) { 78 fst, snd, ok1, ok2, err := files.bm.First2At(0, 1) 79 require.NoError(err) 80 require.True(ok1) 81 require.False(ok2) 82 require.Equal(uint64(1), fst) 83 require.Zero(snd) 84 }) 85 t.Run("locality index: search from given position in future", func(t *testing.T) { 86 fst, snd, ok1, ok2, err := files.bm.First2At(0, 2) 87 require.NoError(err) 88 require.False(ok1) 89 require.False(ok2) 90 require.Zero(fst) 91 require.Zero(snd) 92 }) 93 t.Run("locality index: lookup", func(t *testing.T) { 94 liCtx := li.MakeContext() 95 defer liCtx.Close(logger) 96 var k [8]byte 97 binary.BigEndian.PutUint64(k[:], 1) 98 v1, v2, from, ok1, ok2 := li.lookupIdxFiles(liCtx, k[:], 1*li.aggregationStep*StepsInBiggestFile) 99 require.True(ok1) 100 require.False(ok2) 101 require.Equal(uint64(1*StepsInBiggestFile), v1) 102 require.Equal(uint64(0*StepsInBiggestFile), v2) 103 require.Equal(2*li.aggregationStep*StepsInBiggestFile, from) 104 }) 105 }