add benchmark test

This commit is contained in:
stevemilk 2024-09-19 00:42:59 +08:00
parent 7c25f135ef
commit b4db8bacdf
2 changed files with 41 additions and 1 deletions

View file

@ -222,7 +222,6 @@ func (c *committer) store(path []byte, n node) (node, *wrapNode) {
if c.collectLeaf { if c.collectLeaf {
if sn, ok := n.(*shortNode); ok { if sn, ok := n.(*shortNode); ok {
if val, ok := sn.Val.(valueNode); ok { if val, ok := sn.Val.(valueNode); ok {
c.nodes.AddLeaf(nhash, val)
wNode.leafHash = nhash wNode.leafHash = nhash
wNode.leafBlob = val wNode.leafBlob = val
} }

View file

@ -35,6 +35,7 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/internal/testrand"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie/trienode" "github.com/ethereum/go-ethereum/trie/trienode"
"github.com/holiman/uint256" "github.com/holiman/uint256"
@ -1206,3 +1207,43 @@ func FuzzTrie(f *testing.F) {
} }
}) })
} }
func BenchmarkCommit(b *testing.B) {
benchmarkCommit(b, 500)
benchmarkCommit(b, 1000)
benchmarkCommit(b, 2000)
benchmarkCommit(b, 5000)
}
func benchmarkCommit(b *testing.B, n int) {
b.Run(fmt.Sprintf("commit-%vnodes-single", n), func(b *testing.B) {
testCommit(b, n, false)
})
b.Run(fmt.Sprintf("commit-%vnodes-parallel", n), func(b *testing.B) {
testCommit(b, n, true)
})
}
func testCommit(b *testing.B, n int, parallel bool) {
// test 10 times to get a better average
N := 10
tries := make([]*Trie, N)
for i := 0; i < N; i++ {
tries[i] = NewEmpty(nil)
for j := 0; j < n; j++ {
key := testrand.Bytes(32)
val := testrand.Bytes(32)
tries[i].Update(key, val)
}
tries[i].Hash()
if !parallel {
tries[i].mutate = 0
}
}
b.ResetTimer()
for i := 0; i < 10; i++ {
tries[i].Commit(true)
}
}