From a5039f98b9377a5662fd2b6be30f30c3324a9fe0 Mon Sep 17 00:00:00 2001 From: yajianggroup Date: Tue, 23 Sep 2025 15:35:17 +0800 Subject: [PATCH] triedb: using testing.B.Loop Signed-off-by: yajianggroup --- triedb/pathdb/difflayer_test.go | 8 +++----- triedb/pathdb/history_index_block_test.go | 6 ++---- triedb/pathdb/iterator_test.go | 16 ++++++++-------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/triedb/pathdb/difflayer_test.go b/triedb/pathdb/difflayer_test.go index 0484fd9644..8d25ed85f2 100644 --- a/triedb/pathdb/difflayer_test.go +++ b/triedb/pathdb/difflayer_test.go @@ -83,13 +83,12 @@ func benchmarkSearch(b *testing.B, depth int, total int) { for i := 0; i < total; i++ { layer = fill(layer, i) } - b.ResetTimer() var ( have []byte err error ) - for i := 0; i < b.N; i++ { + for b.Loop() { have, _, _, err = layer.node(common.Hash{}, npath, 0) if err != nil { b.Fatal(err) @@ -120,7 +119,7 @@ func BenchmarkPersist(b *testing.B) { } return newDiffLayer(parent, common.Hash{}, 0, 0, NewNodeSetWithOrigin(nodes, nil), NewStateSetWithOrigin(nil, nil, nil, nil, false)) } - for i := 0; i < b.N; i++ { + for b.Loop() { b.StopTimer() var layer layer layer = emptyLayer() @@ -163,9 +162,8 @@ func BenchmarkJournal(b *testing.B) { for i := 0; i < 128; i++ { layer = fill(layer) } - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { layer.journal(new(bytes.Buffer)) } } diff --git a/triedb/pathdb/history_index_block_test.go b/triedb/pathdb/history_index_block_test.go index c251cea2ec..fbe3cc8710 100644 --- a/triedb/pathdb/history_index_block_test.go +++ b/triedb/pathdb/history_index_block_test.go @@ -224,8 +224,7 @@ func BenchmarkParseIndexBlock(b *testing.B) { } blob := bw.finish() - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { _, _, err := parseIndexBlock(blob) if err != nil { b.Fatalf("parseIndexBlock failed: %v", err) @@ -236,12 +235,11 @@ func BenchmarkParseIndexBlock(b *testing.B) { // BenchmarkBlockWriterAppend benchmarks the performance of indexblock.writer func BenchmarkBlockWriterAppend(b *testing.B) { b.ReportAllocs() - b.ResetTimer() desc := newIndexBlockDesc(0) writer, _ := newBlockWriter(nil, desc) - for i := 0; i < b.N; i++ { + for b.Loop() { if writer.full() { desc = newIndexBlockDesc(0) writer, _ = newBlockWriter(nil, desc) diff --git a/triedb/pathdb/iterator_test.go b/triedb/pathdb/iterator_test.go index adb534f47d..6dd6530754 100644 --- a/triedb/pathdb/iterator_test.go +++ b/triedb/pathdb/iterator_test.go @@ -1051,7 +1051,7 @@ func BenchmarkAccountIteratorTraversal(b *testing.B) { head.(*diffLayer).newBinaryAccountIterator(common.Hash{}) b.Run("binary iterator keys", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { got := 0 it := head.(*diffLayer).newBinaryAccountIterator(common.Hash{}) for it.Next() { @@ -1063,7 +1063,7 @@ func BenchmarkAccountIteratorTraversal(b *testing.B) { } }) b.Run("binary iterator values", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { got := 0 it := head.(*diffLayer).newBinaryAccountIterator(common.Hash{}) for it.Next() { @@ -1076,7 +1076,7 @@ func BenchmarkAccountIteratorTraversal(b *testing.B) { } }) b.Run("fast iterator keys", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { it, _ := db.AccountIterator(common.HexToHash("0x65"), common.Hash{}) defer it.Release() @@ -1090,7 +1090,7 @@ func BenchmarkAccountIteratorTraversal(b *testing.B) { } }) b.Run("fast iterator values", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { it, _ := db.AccountIterator(common.HexToHash("0x65"), common.Hash{}) defer it.Release() @@ -1142,7 +1142,7 @@ func BenchmarkAccountIteratorLargeBaselayer(b *testing.B) { head.(*diffLayer).newBinaryAccountIterator(common.Hash{}) b.Run("binary iterator (keys)", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { got := 0 it := head.(*diffLayer).newBinaryAccountIterator(common.Hash{}) for it.Next() { @@ -1154,7 +1154,7 @@ func BenchmarkAccountIteratorLargeBaselayer(b *testing.B) { } }) b.Run("binary iterator (values)", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { got := 0 it := head.(*diffLayer).newBinaryAccountIterator(common.Hash{}) for it.Next() { @@ -1168,7 +1168,7 @@ func BenchmarkAccountIteratorLargeBaselayer(b *testing.B) { } }) b.Run("fast iterator (keys)", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { it, _ := db.AccountIterator(common.HexToHash("0x65"), common.Hash{}) defer it.Release() @@ -1182,7 +1182,7 @@ func BenchmarkAccountIteratorLargeBaselayer(b *testing.B) { } }) b.Run("fast iterator (values)", func(b *testing.B) { - for i := 0; i < b.N; i++ { + for b.Loop() { it, _ := db.AccountIterator(common.HexToHash("0x65"), common.Hash{}) defer it.Release()