From 21084915662b00ffa4a9353a207bf9c21727e9ad Mon Sep 17 00:00:00 2001 From: cuiweixie Date: Thu, 18 Sep 2025 00:03:40 +0800 Subject: [PATCH] core/types: using testing.B.Loop --- core/types/block_test.go | 3 +-- core/types/bloom9_test.go | 16 ++++++---------- core/types/hashing_test.go | 5 ++--- core/types/transaction_test.go | 8 ++++---- core/types/types_test.go | 4 ++-- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/core/types/block_test.go b/core/types/block_test.go index 2130a2fcf3b2..5fa4756a50e0 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -263,9 +263,8 @@ var benchBuffer = bytes.NewBuffer(make([]byte, 0, 32000)) func BenchmarkEncodeBlock(b *testing.B) { block := makeBenchBlock() - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { benchBuffer.Reset() if err := rlp.Encode(benchBuffer, block); err != nil { b.Fatal(err) diff --git a/core/types/bloom9_test.go b/core/types/bloom9_test.go index 07f6446a978a..ceb6797e1feb 100644 --- a/core/types/bloom9_test.go +++ b/core/types/bloom9_test.go @@ -78,7 +78,7 @@ func TestBloomExtensively(t *testing.T) { func BenchmarkBloom9(b *testing.B) { test := []byte("testestestest") - for i := 0; i < b.N; i++ { + for b.Loop() { Bloom9(test) } } @@ -86,7 +86,7 @@ func BenchmarkBloom9(b *testing.B) { func BenchmarkBloom9Lookup(b *testing.B) { toTest := []byte("testtest") bloom := new(Bloom) - for i := 0; i < b.N; i++ { + for b.Loop() { bloom.Test(toTest) } } @@ -128,7 +128,7 @@ func BenchmarkCreateBloom(b *testing.B) { } b.Run("small-createbloom", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { for _, receipt := range rSmall { receipt.Bloom = CreateBloom(receipt) } @@ -144,7 +144,7 @@ func BenchmarkCreateBloom(b *testing.B) { }) b.Run("large-createbloom", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { for _, receipt := range rLarge { receipt.Bloom = CreateBloom(receipt) } @@ -163,13 +163,11 @@ func BenchmarkCreateBloom(b *testing.B) { receipt.Bloom = CreateBloom(receipt) } b.ReportAllocs() - b.ResetTimer() var bl Bloom - for i := 0; i < b.N; i++ { + for b.Loop() { bl = MergeBloom(rSmall) } - b.StopTimer() var exp = common.HexToHash("c384c56ece49458a427c67b90fefe979ebf7104795be65dc398b280f24104949") got := crypto.Keccak256Hash(bl.Bytes()) @@ -182,13 +180,11 @@ func BenchmarkCreateBloom(b *testing.B) { receipt.Bloom = CreateBloom(receipt) } b.ReportAllocs() - b.ResetTimer() var bl Bloom - for i := 0; i < b.N; i++ { + for b.Loop() { bl = MergeBloom(rLarge) } - b.StopTimer() var exp = common.HexToHash("c384c56ece49458a427c67b90fefe979ebf7104795be65dc398b280f24104949") got := crypto.Keccak256Hash(bl.Bytes()) diff --git a/core/types/hashing_test.go b/core/types/hashing_test.go index c846ecd0c53d..54adbc73e8d0 100644 --- a/core/types/hashing_test.go +++ b/core/types/hashing_test.go @@ -84,9 +84,8 @@ func BenchmarkDeriveSha200(b *testing.B) { var exp common.Hash var got common.Hash b.Run("std_trie", func(b *testing.B) { - b.ResetTimer() b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { exp = types.DeriveSha(txs, trie.NewEmpty(triedb.NewDatabase(rawdb.NewMemoryDatabase(), nil))) } }) @@ -94,7 +93,7 @@ func BenchmarkDeriveSha200(b *testing.B) { b.Run("stack_trie", func(b *testing.B) { b.ResetTimer() b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { got = types.DeriveSha(txs, trie.NewStackTrie(nil)) } }) diff --git a/core/types/transaction_test.go b/core/types/transaction_test.go index cc41674dfdb1..2d854ae345fe 100644 --- a/core/types/transaction_test.go +++ b/core/types/transaction_test.go @@ -591,7 +591,7 @@ func BenchmarkHash(b *testing.B) { GasTipCap: big.NewInt(500), GasFeeCap: big.NewInt(500), }) - for i := 0; i < b.N; i++ { + for b.Loop() { signer.Hash(tx) } } @@ -614,7 +614,7 @@ func BenchmarkEffectiveGasTip(b *testing.B) { b.Run("Original", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { _, err := tx.EffectiveGasTip(baseFee.ToBig()) if err != nil { b.Fatal(err) @@ -625,7 +625,7 @@ func BenchmarkEffectiveGasTip(b *testing.B) { b.Run("IntoMethod", func(b *testing.B) { b.ReportAllocs() dst := new(uint256.Int) - for i := 0; i < b.N; i++ { + for b.Loop() { err := tx.calcEffectiveGasTip(dst, baseFee) if err != nil { b.Fatal(err) @@ -729,7 +729,7 @@ func BenchmarkEffectiveGasTipCmp(b *testing.B) { b.Run("Original", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { tx.EffectiveGasTipCmp(other, baseFee) } }) diff --git a/core/types/types_test.go b/core/types/types_test.go index 1fb386d5deef..96d044499473 100644 --- a/core/types/types_test.go +++ b/core/types/types_test.go @@ -126,7 +126,7 @@ func benchRLP(b *testing.B, encode bool) { b.Run(tc.name, func(b *testing.B) { b.ReportAllocs() var null = &devnull{} - for i := 0; i < b.N; i++ { + for b.Loop() { rlp.Encode(null, tc.obj) } b.SetBytes(int64(null.len / b.N)) @@ -136,7 +136,7 @@ func benchRLP(b *testing.B, encode bool) { // Test decoding b.Run(tc.name, func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { if err := rlp.DecodeBytes(data, tc.obj); err != nil { b.Fatal(err) }