forked from cosmos/iavl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutils_test.go
152 lines (129 loc) · 2.75 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
// nolint:errcheck
package iavl
import (
"bytes"
"fmt"
"runtime"
"testing"
mrand "math/rand"
cmn "github.com/cosmos/iavl/common"
"github.com/stretchr/testify/require"
db "github.com/tendermint/tm-db"
)
func randstr(length int) string {
return cmn.RandStr(length)
}
func i2b(i int) []byte {
buf := new(bytes.Buffer)
encodeVarint(buf, int64(i))
return buf.Bytes()
}
func b2i(bz []byte) int {
i, _, err := decodeVarint(bz)
if err != nil {
panic(err)
}
return int(i)
}
// Construct a MutableTree
func getTestTree(cacheSize int) (*MutableTree, error) {
return NewMutableTreeWithOpts(db.NewMemDB(), cacheSize, nil)
}
// 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, 0)
}
if _, ok := r.(*Node); ok {
right = r.(*Node)
} else {
right = NewNode(i2b(r.(int)), nil, 0)
}
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 {
t, _ := getTestTree(0)
n.hashWithCount()
t.root = n
return t
}
// Convenience for simple printing of keys & tree structure
func P(n *Node) string {
if n.height == 0 {
return fmt.Sprintf("%v", b2i(n.key))
}
return fmt.Sprintf("(%v %v)", P(n.leftNode), P(n.rightNode))
}
func randBytes(length int) []byte {
key := make([]byte, length)
// math.rand.Read always returns err=nil
// we do not need cryptographic randomness for this test:
//nolint:gosec
mrand.Read(key)
return key
}
type traverser struct {
first string
last string
count int
}
func (t *traverser) view(key, value []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 BenchmarkImmutableAvlTreeMemDB(b *testing.B) {
db, err := db.NewDB("test", db.MemDBBackend, "")
require.NoError(b, err)
benchmarkImmutableAvlTreeWithDB(b, db)
}
func benchmarkImmutableAvlTreeWithDB(b *testing.B, db db.DB) {
defer db.Close()
b.StopTimer()
t, err := NewMutableTree(db, 100000)
require.NoError(b, err)
value := []byte{}
for i := 0; i < 1000000; i++ {
t.Set(i2b(int(cmn.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(cmn.RandInt31()))
t.Set(ri, value)
t.Remove(ri)
if i%100 == 99 {
t.SaveVersion()
}
}
}