This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
node_test.go
79 lines (71 loc) · 1.5 KB
/
node_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
package iavl
import (
"bytes"
"math/rand"
"testing"
"github.com/stretchr/testify/require"
)
func TestNode_aminoSize(t *testing.T) {
node := &Node{
key: randBytes(10),
value: randBytes(10),
version: 1,
height: 0,
size: 100,
hash: randBytes(20),
leftHash: randBytes(20),
leftNode: nil,
rightHash: randBytes(20),
rightNode: nil,
persisted: false,
}
// leaf node
require.Equal(t, 26, node.aminoSize())
// non-leaf node
node.height = 1
require.Equal(t, 57, node.aminoSize())
}
func BenchmarkNode_aminoSize(b *testing.B) {
node := &Node{
key: randBytes(25),
value: randBytes(100),
version: rand.Int63n(10000000),
height: 1,
size: rand.Int63n(10000000),
leftHash: randBytes(20),
rightHash: randBytes(20),
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
node.aminoSize()
}
}
func BenchmarkNode_WriteBytes(b *testing.B) {
node := &Node{
key: randBytes(25),
value: randBytes(100),
version: rand.Int63n(10000000),
height: 1,
size: rand.Int63n(10000000),
leftHash: randBytes(20),
rightHash: randBytes(20),
}
b.ResetTimer()
b.Run("NoPreAllocate", func(sub *testing.B) {
sub.ReportAllocs()
for i := 0; i < sub.N; i++ {
var buf bytes.Buffer
buf.Reset()
_ = node.writeBytes(&buf)
}
})
b.Run("PreAllocate", func(sub *testing.B) {
sub.ReportAllocs()
for i := 0; i < sub.N; i++ {
var buf bytes.Buffer
buf.Grow(node.aminoSize())
_ = node.writeBytes(&buf)
}
})
}