-
Notifications
You must be signed in to change notification settings - Fork 265
/
testutils_test.go
358 lines (291 loc) · 7.74 KB
/
testutils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// nolint:errcheck
package iavl
import (
"bytes"
"fmt"
"math/rand"
"runtime"
"sort"
"testing"
corestore "cosmossdk.io/core/store"
"github.com/stretchr/testify/require"
dbm "github.com/cosmos/iavl/db"
"github.com/cosmos/iavl/internal/encoding"
iavlrand "github.com/cosmos/iavl/internal/rand"
)
type iteratorTestConfig struct {
startIterate, endIterate []byte
startByteToSet, endByteToSet byte
ascending bool
}
func randstr(length int) string {
return iavlrand.RandStr(length)
}
func i2b(i int) []byte {
buf := new(bytes.Buffer)
encoding.EncodeVarint(buf, int64(i))
return buf.Bytes()
}
func b2i(bz []byte) int {
i, _, err := encoding.DecodeVarint(bz)
if err != nil {
panic(err)
}
return int(i)
}
// Construct a MutableTree
func getTestTree(cacheSize int) *MutableTree {
return NewMutableTree(dbm.NewMemDB(), cacheSize, false, NewNopLogger())
}
// Convenience for a new node
func N(l, r interface{}) *Node {
var left, right *Node
if _, ok := l.(*Node); ok {
left = l.(*Node)
} else {
left = NewNode(i2b(l.(int)), nil)
}
if _, ok := r.(*Node); ok {
right = r.(*Node)
} else {
right = NewNode(i2b(r.(int)), nil)
}
n := &Node{
key: right.lmd(nil).key,
value: nil,
leftNode: left,
rightNode: right,
}
n.calcHeightAndSize(nil)
return n
}
// Setup a deep node
func T(n *Node) (*MutableTree, error) {
t := getTestTree(0)
n.hashWithCount(t.version + 1)
t.root = n
return t, nil
}
// Convenience for simple printing of keys & tree structure
func P(n *Node, t *ImmutableTree) string {
if n.subtreeHeight == 0 {
return fmt.Sprintf("%v", b2i(n.key))
}
leftNode, _ := n.getLeftNode(t)
rightNode, _ := n.getRightNode(t)
return fmt.Sprintf("(%v %v)", P(leftNode, t), P(rightNode, t))
}
type traverser struct {
first string
last string
count int
}
func (t *traverser) view(key, _ []byte) bool {
if t.first == "" {
t.first = string(key)
}
t.last = string(key)
t.count++
return false
}
func expectTraverse(t *testing.T, trav traverser, start, end string, count int) {
if trav.first != start {
t.Error("Bad start", start, trav.first)
}
if trav.last != end {
t.Error("Bad end", end, trav.last)
}
if trav.count != count {
t.Error("Bad count", count, trav.count)
}
}
func assertMutableMirrorIterate(t *testing.T, tree *MutableTree, mirror map[string]string) {
sortedMirrorKeys := make([]string, 0, len(mirror))
for k := range mirror {
sortedMirrorKeys = append(sortedMirrorKeys, k)
}
sort.Strings(sortedMirrorKeys)
curKeyIdx := 0
tree.Iterate(func(k, v []byte) bool {
nextMirrorKey := sortedMirrorKeys[curKeyIdx]
nextMirrorValue := mirror[nextMirrorKey]
require.Equal(t, []byte(nextMirrorKey), k)
require.Equal(t, []byte(nextMirrorValue), v)
curKeyIdx++
return false
})
}
func assertImmutableMirrorIterate(t *testing.T, tree *ImmutableTree, mirror map[string]string) {
sortedMirrorKeys := getSortedMirrorKeys(mirror)
curKeyIdx := 0
tree.Iterate(func(k, v []byte) bool {
nextMirrorKey := sortedMirrorKeys[curKeyIdx]
nextMirrorValue := mirror[nextMirrorKey]
require.Equal(t, []byte(nextMirrorKey), k)
require.Equal(t, []byte(nextMirrorValue), v)
curKeyIdx++
return false
})
}
func getSortedMirrorKeys(mirror map[string]string) []string {
sortedMirrorKeys := make([]string, 0, len(mirror))
for k := range mirror {
sortedMirrorKeys = append(sortedMirrorKeys, k)
}
sort.Strings(sortedMirrorKeys)
return sortedMirrorKeys
}
func getRandomizedTreeAndMirror(t *testing.T) (*MutableTree, map[string]string) {
const cacheSize = 100
tree := getTestTree(cacheSize)
mirror := make(map[string]string)
randomizeTreeAndMirror(t, tree, mirror)
return tree, mirror
}
func randomizeTreeAndMirror(t *testing.T, tree *MutableTree, mirror map[string]string) {
if mirror == nil {
mirror = make(map[string]string)
}
const keyValLength = 5
numberOfSets := 1000
numberOfUpdates := numberOfSets / 4
numberOfRemovals := numberOfSets / 4
for numberOfSets > numberOfRemovals*3 {
key := iavlrand.RandBytes(keyValLength)
value := iavlrand.RandBytes(keyValLength)
isUpdated, err := tree.Set(key, value)
require.NoError(t, err)
require.False(t, isUpdated)
mirror[string(key)] = string(value)
numberOfSets--
}
for numberOfSets+numberOfRemovals+numberOfUpdates > 0 {
randOp := rand.Intn(3)
switch randOp {
case 0:
if numberOfSets == 0 {
continue
}
numberOfSets--
key := iavlrand.RandBytes(keyValLength)
value := iavlrand.RandBytes(keyValLength)
isUpdated, err := tree.Set(key, value)
require.NoError(t, err)
require.False(t, isUpdated)
mirror[string(key)] = string(value)
case 1:
if numberOfUpdates == 0 {
continue
}
numberOfUpdates--
key := getRandomKeyFrom(mirror)
value := iavlrand.RandBytes(keyValLength)
isUpdated, err := tree.Set([]byte(key), value)
require.NoError(t, err)
require.True(t, isUpdated)
mirror[key] = string(value)
case 2:
if numberOfRemovals == 0 {
continue
}
numberOfRemovals--
key := getRandomKeyFrom(mirror)
val, isRemoved, err := tree.Remove([]byte(key))
require.NoError(t, err)
require.True(t, isRemoved)
require.NotNil(t, val)
delete(mirror, key)
default:
t.Error("Invalid randOp", randOp)
}
}
}
func getRandomKeyFrom(mirror map[string]string) string {
for k := range mirror {
return k
}
panic("no keys in mirror")
}
func setupMirrorForIterator(t *testing.T, config *iteratorTestConfig, tree *MutableTree) [][]string {
var mirror [][]string
startByteToSet := config.startByteToSet
endByteToSet := config.endByteToSet
if !config.ascending {
startByteToSet, endByteToSet = endByteToSet, startByteToSet
}
curByte := startByteToSet
for curByte != endByteToSet {
value := iavlrand.RandBytes(5)
if (config.startIterate == nil || curByte >= config.startIterate[0]) && (config.endIterate == nil || curByte < config.endIterate[0]) {
mirror = append(mirror, []string{string(curByte), string(value)})
}
isUpdated, err := tree.Set([]byte{curByte}, value)
require.NoError(t, err)
require.False(t, isUpdated)
if config.ascending {
curByte++
} else {
curByte--
}
}
return mirror
}
// assertIterator confirms that the iterator returns the expected values desribed by mirror in the same order.
// mirror is a slice containing slices of the form [key, value]. In other words, key at index 0 and value at index 1.
func assertIterator(t *testing.T, itr corestore.Iterator, mirror [][]string, ascending bool) {
startIdx, endIdx := 0, len(mirror)-1
increment := 1
mirrorIdx := startIdx
// flip the iteration order over mirror if descending
if !ascending {
startIdx = endIdx - 1
endIdx = -1
increment *= -1
}
for startIdx != endIdx {
nextExpectedPair := mirror[mirrorIdx]
require.True(t, itr.Valid())
require.Equal(t, []byte(nextExpectedPair[0]), itr.Key())
require.Equal(t, []byte(nextExpectedPair[1]), itr.Value())
itr.Next()
require.NoError(t, itr.Error())
startIdx += increment
mirrorIdx++
}
}
func BenchmarkImmutableAvlTreeMemDB(b *testing.B) {
db := dbm.NewMemDB()
benchmarkImmutableAvlTreeWithDB(b, db)
}
func benchmarkImmutableAvlTreeWithDB(b *testing.B, db corestore.KVStoreWithBatch) {
defer db.Close()
b.StopTimer()
t := NewMutableTree(db, 100000, false, NewNopLogger())
value := []byte{}
for i := 0; i < 1000000; i++ {
t.Set(i2b(int(iavlrand.RandInt31())), value)
if i > 990000 && i%1000 == 999 {
t.SaveVersion()
}
}
b.ReportAllocs()
t.SaveVersion()
runtime.GC()
b.StartTimer()
for i := 0; i < b.N; i++ {
ri := i2b(int(iavlrand.RandInt31()))
t.Set(ri, value)
t.Remove(ri)
if i%100 == 99 {
t.SaveVersion()
}
}
}
func (node *Node) lmd(t *ImmutableTree) *Node {
if node.isLeaf() {
return node
}
// TODO: Should handle this error?
leftNode, _ := node.getLeftNode(t)
return leftNode.lmd(t)
}