Skip to content

Commit d41dc92

Browse files
authored
core/state: using testing.B.Loop (#32658)
before: go test -run=^$ -bench=. ./core/state/... 120.85s user 7.96s system 129% cpu 1:39.13 tota after: go test -run=^$ -bench=. ./core/state/... 21.32s user 2.12s system 97% cpu 24.006 total
1 parent 64c6de7 commit d41dc92

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

core/state/snapshot/difflayer_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,7 @@ func BenchmarkSearch(b *testing.B) {
229229
layer = fill(layer)
230230
}
231231
key := crypto.Keccak256Hash([]byte{0x13, 0x38})
232-
b.ResetTimer()
233-
for i := 0; i < b.N; i++ {
232+
for b.Loop() {
234233
layer.AccountRLP(key)
235234
}
236235
}
@@ -269,8 +268,7 @@ func BenchmarkSearchSlot(b *testing.B) {
269268
for i := 0; i < 128; i++ {
270269
layer = fill(layer)
271270
}
272-
b.ResetTimer()
273-
for i := 0; i < b.N; i++ {
271+
for b.Loop() {
274272
layer.Storage(accountKey, storageKey)
275273
}
276274
}
@@ -300,9 +298,7 @@ func BenchmarkFlatten(b *testing.B) {
300298
}
301299
return newDiffLayer(parent, common.Hash{}, accounts, storage)
302300
}
303-
b.ResetTimer()
304-
for i := 0; i < b.N; i++ {
305-
b.StopTimer()
301+
for b.Loop() {
306302
var layer snapshot
307303
layer = emptyLayer()
308304
for i := 1; i < 128; i++ {
@@ -352,9 +348,7 @@ func BenchmarkJournal(b *testing.B) {
352348
for i := 1; i < 128; i++ {
353349
layer = fill(layer)
354350
}
355-
b.ResetTimer()
356-
357-
for i := 0; i < b.N; i++ {
351+
for b.Loop() {
358352
layer.Journal(new(bytes.Buffer))
359353
}
360354
}

core/state/snapshot/iterator_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ func BenchmarkAccountIteratorTraversal(b *testing.B) {
928928
head.(*diffLayer).newBinaryAccountIterator(common.Hash{})
929929

930930
b.Run("binary iterator keys", func(b *testing.B) {
931-
for i := 0; i < b.N; i++ {
931+
for b.Loop() {
932932
got := 0
933933
it := head.(*diffLayer).newBinaryAccountIterator(common.Hash{})
934934
for it.Next() {
@@ -940,7 +940,7 @@ func BenchmarkAccountIteratorTraversal(b *testing.B) {
940940
}
941941
})
942942
b.Run("binary iterator values", func(b *testing.B) {
943-
for i := 0; i < b.N; i++ {
943+
for b.Loop() {
944944
got := 0
945945
it := head.(*diffLayer).newBinaryAccountIterator(common.Hash{})
946946
for it.Next() {
@@ -953,7 +953,7 @@ func BenchmarkAccountIteratorTraversal(b *testing.B) {
953953
}
954954
})
955955
b.Run("fast iterator keys", func(b *testing.B) {
956-
for i := 0; i < b.N; i++ {
956+
for b.Loop() {
957957
it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
958958
defer it.Release()
959959

@@ -967,7 +967,7 @@ func BenchmarkAccountIteratorTraversal(b *testing.B) {
967967
}
968968
})
969969
b.Run("fast iterator values", func(b *testing.B) {
970-
for i := 0; i < b.N; i++ {
970+
for b.Loop() {
971971
it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
972972
defer it.Release()
973973

@@ -1025,7 +1025,7 @@ func BenchmarkAccountIteratorLargeBaselayer(b *testing.B) {
10251025
head.(*diffLayer).newBinaryAccountIterator(common.Hash{})
10261026

10271027
b.Run("binary iterator (keys)", func(b *testing.B) {
1028-
for i := 0; i < b.N; i++ {
1028+
for b.Loop() {
10291029
got := 0
10301030
it := head.(*diffLayer).newBinaryAccountIterator(common.Hash{})
10311031
for it.Next() {
@@ -1037,7 +1037,7 @@ func BenchmarkAccountIteratorLargeBaselayer(b *testing.B) {
10371037
}
10381038
})
10391039
b.Run("binary iterator (values)", func(b *testing.B) {
1040-
for i := 0; i < b.N; i++ {
1040+
for b.Loop() {
10411041
got := 0
10421042
it := head.(*diffLayer).newBinaryAccountIterator(common.Hash{})
10431043
for it.Next() {
@@ -1051,7 +1051,7 @@ func BenchmarkAccountIteratorLargeBaselayer(b *testing.B) {
10511051
}
10521052
})
10531053
b.Run("fast iterator (keys)", func(b *testing.B) {
1054-
for i := 0; i < b.N; i++ {
1054+
for b.Loop() {
10551055
it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
10561056
defer it.Release()
10571057

@@ -1065,7 +1065,7 @@ func BenchmarkAccountIteratorLargeBaselayer(b *testing.B) {
10651065
}
10661066
})
10671067
b.Run("fast iterator (values)", func(b *testing.B) {
1068-
for i := 0; i < b.N; i++ {
1068+
for b.Loop() {
10691069
it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
10701070
defer it.Release()
10711071

core/state/state_object_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ import (
2525

2626
func BenchmarkCutOriginal(b *testing.B) {
2727
value := common.HexToHash("0x01")
28-
for i := 0; i < b.N; i++ {
28+
for b.Loop() {
2929
bytes.TrimLeft(value[:], "\x00")
3030
}
3131
}
3232

3333
func BenchmarkCutsetterFn(b *testing.B) {
3434
value := common.HexToHash("0x01")
3535
cutSetFn := func(r rune) bool { return r == 0 }
36-
for i := 0; i < b.N; i++ {
36+
for b.Loop() {
3737
bytes.TrimLeftFunc(value[:], cutSetFn)
3838
}
3939
}
4040

4141
func BenchmarkCutCustomTrim(b *testing.B) {
4242
value := common.HexToHash("0x01")
43-
for i := 0; i < b.N; i++ {
43+
for b.Loop() {
4444
common.TrimLeftZeroes(value[:])
4545
}
4646
}

0 commit comments

Comments
 (0)