Skip to content

Commit a5fcaa5

Browse files
committed
trie: make hasher benchmark meaningful post-caches
1 parent 0ed4d76 commit a5fcaa5

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

trie/trie_test.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
"fmt"
2424
"io/ioutil"
25+
"math/big"
2526
"math/rand"
2627
"os"
2728
"reflect"
@@ -30,7 +31,9 @@ import (
3031

3132
"github.com/davecgh/go-spew/spew"
3233
"github.com/ethereum/go-ethereum/common"
34+
"github.com/ethereum/go-ethereum/crypto"
3335
"github.com/ethereum/go-ethereum/ethdb"
36+
"github.com/ethereum/go-ethereum/rlp"
3437
)
3538

3639
func init() {
@@ -505,8 +508,6 @@ func BenchmarkGet(b *testing.B) { benchGet(b, false) }
505508
func BenchmarkGetDB(b *testing.B) { benchGet(b, true) }
506509
func BenchmarkUpdateBE(b *testing.B) { benchUpdate(b, binary.BigEndian) }
507510
func BenchmarkUpdateLE(b *testing.B) { benchUpdate(b, binary.LittleEndian) }
508-
func BenchmarkHashBE(b *testing.B) { benchHash(b, binary.BigEndian) }
509-
func BenchmarkHashLE(b *testing.B) { benchHash(b, binary.LittleEndian) }
510511

511512
const benchElemCount = 20000
512513

@@ -549,18 +550,39 @@ func benchUpdate(b *testing.B, e binary.ByteOrder) *Trie {
549550
return trie
550551
}
551552

552-
func benchHash(b *testing.B, e binary.ByteOrder) {
553+
// Benchmarks the trie hashing. Since the trie caches the result of any operation,
554+
// we cannot use b.N as the number of hashing rouns, since all rounds apart from
555+
// the first one will be NOOP. As such, we'll use b.N as the number of account to
556+
// insert into the trie before measuring the hashing.
557+
func BenchmarkHash(b *testing.B) {
558+
// Make the random benchmark deterministic
559+
random := rand.New(rand.NewSource(0))
560+
561+
// Create a realistic account trie to hash
562+
addresses := make([][20]byte, b.N)
563+
for i := 0; i < len(addresses); i++ {
564+
for j := 0; j < len(addresses[i]); j++ {
565+
addresses[i][j] = byte(random.Intn(256))
566+
}
567+
}
568+
accounts := make([][]byte, len(addresses))
569+
for i := 0; i < len(accounts); i++ {
570+
var (
571+
nonce = uint64(random.Int63())
572+
balance = new(big.Int).Rand(random, new(big.Int).Exp(common.Big2, common.Big256, nil))
573+
root = emptyRoot
574+
code = crypto.Keccak256(nil)
575+
)
576+
accounts[i], _ = rlp.EncodeToBytes([]interface{}{nonce, balance, root, code})
577+
}
578+
// Insert the accounts into the trie and hash it
553579
trie := newEmpty()
554-
k := make([]byte, 32)
555-
for i := 0; i < benchElemCount; i++ {
556-
e.PutUint64(k, uint64(i))
557-
trie.Update(k, k)
580+
for i := 0; i < len(addresses); i++ {
581+
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i])
558582
}
559-
560583
b.ResetTimer()
561-
for i := 0; i < b.N; i++ {
562-
trie.Hash()
563-
}
584+
b.ReportAllocs()
585+
trie.Hash()
564586
}
565587

566588
func tempDB() (string, Database) {

0 commit comments

Comments
 (0)