Skip to content

Commit e35c628

Browse files
authored
core/types: using testing.B.Loop (#32643)
before: go test -run=^$ -bench=. ./core/types 47.80s user 2.18s system 102% cpu 48.936 tota after: go test -run=^$ -bench=. ./core/types 42.42s user 2.27s system 112% cpu 39.593 total
1 parent 79a4f76 commit e35c628

File tree

5 files changed

+15
-21
lines changed

5 files changed

+15
-21
lines changed

core/types/block_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,8 @@ var benchBuffer = bytes.NewBuffer(make([]byte, 0, 32000))
263263

264264
func BenchmarkEncodeBlock(b *testing.B) {
265265
block := makeBenchBlock()
266-
b.ResetTimer()
267266

268-
for i := 0; i < b.N; i++ {
267+
for b.Loop() {
269268
benchBuffer.Reset()
270269
if err := rlp.Encode(benchBuffer, block); err != nil {
271270
b.Fatal(err)

core/types/bloom9_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ func TestBloomExtensively(t *testing.T) {
7878

7979
func BenchmarkBloom9(b *testing.B) {
8080
test := []byte("testestestest")
81-
for i := 0; i < b.N; i++ {
81+
for b.Loop() {
8282
Bloom9(test)
8383
}
8484
}
8585

8686
func BenchmarkBloom9Lookup(b *testing.B) {
8787
toTest := []byte("testtest")
8888
bloom := new(Bloom)
89-
for i := 0; i < b.N; i++ {
89+
for b.Loop() {
9090
bloom.Test(toTest)
9191
}
9292
}
@@ -128,7 +128,7 @@ func BenchmarkCreateBloom(b *testing.B) {
128128
}
129129
b.Run("small-createbloom", func(b *testing.B) {
130130
b.ReportAllocs()
131-
for i := 0; i < b.N; i++ {
131+
for b.Loop() {
132132
for _, receipt := range rSmall {
133133
receipt.Bloom = CreateBloom(receipt)
134134
}
@@ -144,7 +144,7 @@ func BenchmarkCreateBloom(b *testing.B) {
144144
})
145145
b.Run("large-createbloom", func(b *testing.B) {
146146
b.ReportAllocs()
147-
for i := 0; i < b.N; i++ {
147+
for b.Loop() {
148148
for _, receipt := range rLarge {
149149
receipt.Bloom = CreateBloom(receipt)
150150
}
@@ -163,13 +163,11 @@ func BenchmarkCreateBloom(b *testing.B) {
163163
receipt.Bloom = CreateBloom(receipt)
164164
}
165165
b.ReportAllocs()
166-
b.ResetTimer()
167166

168167
var bl Bloom
169-
for i := 0; i < b.N; i++ {
168+
for b.Loop() {
170169
bl = MergeBloom(rSmall)
171170
}
172-
b.StopTimer()
173171

174172
var exp = common.HexToHash("c384c56ece49458a427c67b90fefe979ebf7104795be65dc398b280f24104949")
175173
got := crypto.Keccak256Hash(bl.Bytes())
@@ -182,13 +180,11 @@ func BenchmarkCreateBloom(b *testing.B) {
182180
receipt.Bloom = CreateBloom(receipt)
183181
}
184182
b.ReportAllocs()
185-
b.ResetTimer()
186183

187184
var bl Bloom
188-
for i := 0; i < b.N; i++ {
185+
for b.Loop() {
189186
bl = MergeBloom(rLarge)
190187
}
191-
b.StopTimer()
192188

193189
var exp = common.HexToHash("c384c56ece49458a427c67b90fefe979ebf7104795be65dc398b280f24104949")
194190
got := crypto.Keccak256Hash(bl.Bytes())

core/types/hashing_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,16 @@ func BenchmarkDeriveSha200(b *testing.B) {
8484
var exp common.Hash
8585
var got common.Hash
8686
b.Run("std_trie", func(b *testing.B) {
87-
b.ResetTimer()
8887
b.ReportAllocs()
89-
for i := 0; i < b.N; i++ {
88+
for b.Loop() {
9089
exp = types.DeriveSha(txs, trie.NewEmpty(triedb.NewDatabase(rawdb.NewMemoryDatabase(), nil)))
9190
}
9291
})
9392

9493
b.Run("stack_trie", func(b *testing.B) {
9594
b.ResetTimer()
9695
b.ReportAllocs()
97-
for i := 0; i < b.N; i++ {
96+
for b.Loop() {
9897
got = types.DeriveSha(txs, trie.NewStackTrie(nil))
9998
}
10099
})

core/types/transaction_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ func BenchmarkHash(b *testing.B) {
591591
GasTipCap: big.NewInt(500),
592592
GasFeeCap: big.NewInt(500),
593593
})
594-
for i := 0; i < b.N; i++ {
594+
for b.Loop() {
595595
signer.Hash(tx)
596596
}
597597
}
@@ -614,7 +614,7 @@ func BenchmarkEffectiveGasTip(b *testing.B) {
614614

615615
b.Run("Original", func(b *testing.B) {
616616
b.ReportAllocs()
617-
for i := 0; i < b.N; i++ {
617+
for b.Loop() {
618618
_, err := tx.EffectiveGasTip(baseFee.ToBig())
619619
if err != nil {
620620
b.Fatal(err)
@@ -625,7 +625,7 @@ func BenchmarkEffectiveGasTip(b *testing.B) {
625625
b.Run("IntoMethod", func(b *testing.B) {
626626
b.ReportAllocs()
627627
dst := new(uint256.Int)
628-
for i := 0; i < b.N; i++ {
628+
for b.Loop() {
629629
err := tx.calcEffectiveGasTip(dst, baseFee)
630630
if err != nil {
631631
b.Fatal(err)
@@ -729,7 +729,7 @@ func BenchmarkEffectiveGasTipCmp(b *testing.B) {
729729

730730
b.Run("Original", func(b *testing.B) {
731731
b.ReportAllocs()
732-
for i := 0; i < b.N; i++ {
732+
for b.Loop() {
733733
tx.EffectiveGasTipCmp(other, baseFee)
734734
}
735735
})

core/types/types_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func benchRLP(b *testing.B, encode bool) {
126126
b.Run(tc.name, func(b *testing.B) {
127127
b.ReportAllocs()
128128
var null = &devnull{}
129-
for i := 0; i < b.N; i++ {
129+
for b.Loop() {
130130
rlp.Encode(null, tc.obj)
131131
}
132132
b.SetBytes(int64(null.len / b.N))
@@ -136,7 +136,7 @@ func benchRLP(b *testing.B, encode bool) {
136136
// Test decoding
137137
b.Run(tc.name, func(b *testing.B) {
138138
b.ReportAllocs()
139-
for i := 0; i < b.N; i++ {
139+
for b.Loop() {
140140
if err := rlp.DecodeBytes(data, tc.obj); err != nil {
141141
b.Fatal(err)
142142
}

0 commit comments

Comments
 (0)