github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/forensics/storage/kvcache_test.go (about) 1 package storage 2 3 import ( 4 bin "encoding/binary" 5 "math/rand" 6 "testing" 7 8 "github.com/hyperledger/burrow/storage" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestKVCache_Iterator(t *testing.T) { 14 kvc := NewKVCache() 15 kvp := kvPairs( 16 "f", "oo", 17 "b", "ar", 18 "aa", "ooh", 19 "im", "aginative", 20 ) 21 sortedKVP := kvPairs( 22 "aa", "ooh", 23 "b", "ar", 24 "f", "oo", 25 "im", "aginative", 26 ) 27 for _, kv := range kvp { 28 kvc.Set(kv.Key, kv.Value) 29 } 30 assert.Equal(t, sortedKVP, collectIterator(kvc.Iterator(nil, nil))) 31 } 32 33 func TestKVCache_Iterator2(t *testing.T) { 34 assert.Equal(t, []string{"b"}, testIterate([]byte("b"), []byte("c"), false, "a", "b", "c", "d")) 35 assert.Equal(t, []string{"b", "c"}, testIterate([]byte("b"), []byte("cc"), false, "a", "b", "c", "d")) 36 assert.Equal(t, []string{"a", "b", "c", "d"}, testIterate([]byte(""), nil, false, "a", "b", "c", "d")) 37 assert.Equal(t, []string{"d", "c", "b", "a"}, testIterate([]byte(""), nil, true, "a", "b", "c", "d")) 38 assert.Equal(t, []string{"a", "b", "c", "d"}, testIterate(nil, nil, false, "a", "b", "c", "d")) 39 40 assert.Equal(t, []string{}, testIterate([]byte(""), []byte(""), false, "a", "b", "c", "d")) 41 assert.Equal(t, []string{}, testIterate([]byte("ab"), []byte("ab"), false, "a", "b", "c", "d")) 42 assert.Equal(t, []string{"a"}, testIterate([]byte("0"), []byte("ab"), true, "a", "b", "c", "d")) 43 assert.Equal(t, []string{"c", "b", "a"}, testIterate([]byte("a"), []byte("c1"), true, "a", "b", "c", "d")) 44 assert.Equal(t, []string{"b", "a"}, testIterate([]byte("a"), []byte("c"), true, "a", "b", "c", "d")) 45 assert.Equal(t, []string{"b", "a"}, testIterate([]byte("a"), []byte("c"), true, "a", "b", "c", "d")) 46 assert.Equal(t, []string{}, testIterate([]byte("c"), []byte("e"), true, "a", "b")) 47 assert.Equal(t, []string{}, testIterate([]byte("c"), []byte("e"), true, "z", "f")) 48 } 49 50 func BenchmarkKVCache_Iterator_1E6_Inserts(b *testing.B) { 51 benchmarkKVCache_Iterator(b, 1e6) 52 } 53 54 func BenchmarkKVCache_Iterator_1E7_Inserts(b *testing.B) { 55 benchmarkKVCache_Iterator(b, 1e7) 56 } 57 58 func benchmarkKVCache_Iterator(b *testing.B, inserts int) { 59 b.StopTimer() 60 cache := NewKVCache() 61 rnd := rand.NewSource(23425) 62 keyvals := make([][]byte, inserts) 63 for i := 0; i < inserts; i++ { 64 bs := make([]byte, 8) 65 bin.BigEndian.PutUint64(bs, uint64(rnd.Int63())) 66 keyvals[i] = bs 67 } 68 for i := 0; i < inserts; i++ { 69 cache.Set(keyvals[i], keyvals[i]) 70 } 71 b.StartTimer() 72 for i := 0; i < b.N; i++ { 73 it := cache.Iterator(nil, nil) 74 for it.Valid() { 75 it.Next() 76 } 77 } 78 } 79 80 func testIterate(low, high []byte, reverse bool, keys ...string) []string { 81 kvc := NewKVCache() 82 for _, k := range keys { 83 bs := []byte(k) 84 kvc.Set(bs, bs) 85 } 86 var it storage.KVIterator 87 if reverse { 88 it = kvc.ReverseIterator(low, high) 89 } else { 90 it = kvc.Iterator(low, high) 91 } 92 keysOut := []string{} 93 for it.Valid() { 94 keysOut = append(keysOut, string(it.Value())) 95 it.Next() 96 } 97 return keysOut 98 }