trie: using testing.B.Loop

Signed-off-by: yajianggroup <yajianggroup@outlook.com>
This commit is contained in:
yajianggroup 2025-09-23 15:31:09 +08:00
parent 2872242045
commit 9e8215faff
7 changed files with 29 additions and 38 deletions

View file

@ -112,35 +112,35 @@ func TestHexToCompactInPlaceRandom(t *testing.T) {
func BenchmarkHexToCompact(b *testing.B) {
testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/}
for i := 0; i < b.N; i++ {
for b.Loop() {
hexToCompact(testBytes)
}
}
func BenchmarkHexToCompactInPlace(b *testing.B) {
testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/}
for i := 0; i < b.N; i++ {
for b.Loop() {
hexToCompactInPlace(testBytes)
}
}
func BenchmarkCompactToHex(b *testing.B) {
testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/}
for i := 0; i < b.N; i++ {
for b.Loop() {
compactToHex(testBytes)
}
}
func BenchmarkKeybytesToHex(b *testing.B) {
testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
for i := 0; i < b.N; i++ {
for b.Loop() {
keybytesToHex(testBytes)
}
}
func BenchmarkHexToKeybytes(b *testing.B) {
testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
for i := 0; i < b.N; i++ {
for b.Loop() {
hexToKeybytes(testBytes)
}
}

View file

@ -1164,8 +1164,8 @@ func BenchmarkIterator(b *testing.B) {
diskDb, srcDb, tr, _ := makeTestTrie(rawdb.HashScheme)
root := tr.Hash()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
if err := checkTrieConsistency(diskDb, srcDb.Scheme(), root, false); err != nil {
b.Fatal(err)
}

View file

@ -104,10 +104,10 @@ func BenchmarkEncodeShortNode(b *testing.B) {
Key: []byte{0x1, 0x2},
Val: hashNode(randBytes(32)),
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
nodeToBytes(node)
}
}
@ -122,10 +122,10 @@ func BenchmarkEncodeFullNode(b *testing.B) {
for i := 0; i < 16; i++ {
node.Children[i] = hashNode(randBytes(32))
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
nodeToBytes(node)
}
}
@ -143,10 +143,9 @@ func BenchmarkDecodeShortNode(b *testing.B) {
blob := nodeToBytes(node)
hash := crypto.Keccak256(blob)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
mustDecodeNode(hash, blob)
}
}
@ -164,10 +163,9 @@ func BenchmarkDecodeShortNodeUnsafe(b *testing.B) {
blob := nodeToBytes(node)
hash := crypto.Keccak256(blob)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
mustDecodeNodeUnsafe(hash, blob)
}
}
@ -185,10 +183,9 @@ func BenchmarkDecodeFullNode(b *testing.B) {
blob := nodeToBytes(node)
hash := crypto.Keccak256(blob)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
mustDecodeNode(hash, blob)
}
}
@ -206,10 +203,9 @@ func BenchmarkDecodeFullNodeUnsafe(b *testing.B) {
blob := nodeToBytes(node)
hash := crypto.Keccak256(blob)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
mustDecodeNodeUnsafe(hash, blob)
}
}

View file

@ -837,8 +837,7 @@ func BenchmarkProve(b *testing.B) {
keys = append(keys, k)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
kv := vals[keys[i%len(keys)]]
proofs := memorydb.New()
if trie.Prove(kv.k, proofs); proofs.Len() == 0 {
@ -859,8 +858,7 @@ func BenchmarkVerifyProof(b *testing.B) {
proofs = append(proofs, proof)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
im := i % len(keys)
if _, err := VerifyProof(root, []byte(keys[im]), proofs[im]); err != nil {
b.Fatalf("key %x: %v", keys[im], err)
@ -897,8 +895,7 @@ func benchmarkVerifyRangeProof(b *testing.B, size int) {
values = append(values, entries[i].v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
_, err := VerifyRangeProof(trie.Hash(), keys[0], keys, values, proof)
if err != nil {
b.Fatalf("Case %d(%d->%d) expect no error, got %v", i, start, end-1, err)
@ -924,8 +921,8 @@ func benchmarkVerifyRangeNoProof(b *testing.B, size int) {
keys = append(keys, entry.k)
values = append(values, entry.v)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
_, err := VerifyRangeProof(trie.Hash(), keys[0], keys, values, nil)
if err != nil {
b.Fatalf("Expected no error, got %v", err)

View file

@ -406,7 +406,7 @@ func BenchmarkInsert100K(b *testing.B) {
var val = make([]byte, 20)
var hash common.Hash
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for b.Loop() {
s := NewStackTrie(nil)
var k uint64
for j := 0; j < num; j++ {

View file

@ -136,8 +136,8 @@ func benchmarkMerge(b *testing.B, count int) {
addNode(x)
addNode(y)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
// Store set x into a backup
z := NewNodeSet(common.Hash{})
z.Merge(x)

View file

@ -64,9 +64,8 @@ func BenchmarkTreeKey(b *testing.B) {
verkle.GetConfig()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
BasicDataKey([]byte{0x01})
}
}
@ -85,8 +84,8 @@ func BenchmarkTreeKeyWithEvaluation(b *testing.B) {
eval := evaluateAddressPoint(addr)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
BasicDataKeyWithEvaluatedAddress(eval)
}
}
@ -102,9 +101,8 @@ func BenchmarkStorageKey(b *testing.B) {
verkle.GetConfig()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
StorageSlotKey([]byte{0x01}, bytes.Repeat([]byte{0xff}, 32))
}
}
@ -123,8 +121,8 @@ func BenchmarkStorageKeyWithEvaluation(b *testing.B) {
eval := evaluateAddressPoint(addr)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
StorageSlotKeyWithEvaluatedAddress(eval, bytes.Repeat([]byte{0xff}, 32))
}
}