-
Notifications
You must be signed in to change notification settings - Fork 4
/
bench_utils_test.go
112 lines (102 loc) · 2.41 KB
/
bench_utils_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
//go:build benchmark
package smt
import (
"crypto/sha256"
"encoding/binary"
"strconv"
"testing"
"github.com/stretchr/testify/require"
"github.com/pokt-network/smt"
"github.com/pokt-network/smt/kvstore/simplemap"
)
var (
updSMT = func(s *smt.SMT, b []byte) error {
return s.Update(b, b)
}
getSMT = func(s *smt.SMT, b []byte) error {
_, err := s.Get(b)
return err
}
proSMT = func(s *smt.SMT, b []byte) error {
_, err := s.Prove(b)
return err
}
delSMT = func(s *smt.SMT, b []byte) error {
return s.Delete(b)
}
updSMST = func(s *smt.SMST, i uint64) error {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, i)
return s.Update(b, b, i)
}
getSMST = func(s *smt.SMST, i uint64) error {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, i)
_, _, _, err := s.Get(b)
return err
}
proSMST = func(s *smt.SMST, i uint64) error {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, i)
_, err := s.Prove(b)
return err
}
delSMST = func(s *smt.SMST, i uint64) error {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, i)
return s.Delete(b)
}
)
func setupSMT(b *testing.B, numLeaves int) *smt.SMT {
b.Helper()
nodes := simplemap.NewSimpleMap()
trie := smt.NewSparseMerkleTrie(nodes, sha256.New())
for i := 0; i < numLeaves; i++ {
s := strconv.Itoa(i)
require.NoError(b, trie.Update([]byte(s), []byte(s)))
}
require.NoError(b, trie.Commit())
b.Cleanup(func() {
require.NoError(b, nodes.ClearAll())
})
return trie
}
func benchmarkSMT(b *testing.B, trie *smt.SMT, commit bool, fn func(*smt.SMT, []byte) error) {
b.ResetTimer()
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
s := strconv.Itoa(i)
_ = fn(trie, []byte(s))
}
if commit {
require.NoError(b, trie.Commit())
}
b.StopTimer()
}
func setupSMST(b *testing.B, numLeaves int) *smt.SMST {
b.Helper()
nodes := simplemap.NewSimpleMap()
trie := smt.NewSparseMerkleSumTrie(nodes, sha256.New())
for i := 0; i < numLeaves; i++ {
s := strconv.Itoa(i)
require.NoError(b, trie.Update([]byte(s), []byte(s), uint64(i)))
}
require.NoError(b, trie.Commit())
b.Cleanup(func() {
require.NoError(b, nodes.ClearAll())
})
return trie
}
func benchmarkSMST(b *testing.B, trie *smt.SMST, commit bool, fn func(*smt.SMST, uint64) error) {
b.ResetTimer()
b.StartTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = fn(trie, uint64(i))
}
if commit {
require.NoError(b, trie.Commit())
}
b.StopTimer()
}