Skip to content

Commit 2ac5691

Browse files
authored
Add benchmark for getting a node (filecoin-project#97)
1 parent 801910a commit 2ac5691

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

hamt_bench_test.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
cbor "github.com/ipfs/go-ipld-cbor"
1212
"github.com/stretchr/testify/require"
13-
cbg "github.com/whyrusleeping/cbor-gen"
1413
)
1514

1615
var debugHistogram = false
@@ -25,10 +24,10 @@ func (r *rander) randString() string {
2524
return hex.EncodeToString(buf)
2625
}
2726

28-
func (r *rander) randValue() *cbg.Deferred {
29-
buf := make([]byte, 30)
27+
func (r *rander) randValue() *CborByteArray {
28+
buf := CborByteArray(make([]byte, 30))
3029
rand.Read(buf)
31-
return &cbg.Deferred{Raw: buf}
30+
return &buf
3231
}
3332

3433
func BenchmarkSerializeNode(b *testing.B) {
@@ -52,6 +51,31 @@ func BenchmarkSerializeNode(b *testing.B) {
5251
}
5352
}
5453

54+
func BenchmarkGetNode(b *testing.B) {
55+
r := rander{rand.New(rand.NewSource(1234))}
56+
57+
cs := cbor.NewCborStore(newMockBlocks())
58+
n, err := NewNode(cs)
59+
require.NoError(b, err)
60+
61+
for i := 0; i < 100000; i++ {
62+
err := n.Set(context.Background(), r.randString(), r.randValue())
63+
require.NoError(b, err)
64+
}
65+
66+
c, err := cs.Put(context.Background(), n)
67+
require.NoError(b, err)
68+
69+
b.ResetTimer()
70+
b.ReportAllocs()
71+
72+
for i := 0; i < b.N; i++ {
73+
var n Node
74+
err := cs.Get(context.Background(), c, &n)
75+
require.NoError(b, err)
76+
}
77+
}
78+
5579
type benchSetCase struct {
5680
kcount int
5781
bitwidth int
@@ -68,7 +92,7 @@ func init() {
6892
100,
6993
500,
7094
1000, // aka 1M
71-
//10000, // aka 10M -- you'll need a lot of RAM for this. Also, some patience.
95+
// 10000, // aka 10M -- you'll need a lot of RAM for this. Also, some patience.
7296
}
7397
bitwidths := []int{
7498
3,
@@ -119,7 +143,7 @@ func BenchmarkFill(b *testing.B) {
119143
blockstore := newMockBlocks()
120144
n, err := NewNode(cbor.NewCborStore(blockstore), UseTreeBitWidth(t.bitwidth))
121145
require.NoError(b, err)
122-
//b.ResetTimer()
146+
// b.ResetTimer()
123147
for j := 0; j < t.kcount*1000; j++ {
124148
err := n.Set(context.Background(), r.randString(), r.randValue())
125149
require.NoError(b, err)

0 commit comments

Comments
 (0)