Skip to content

Commit 79a4f76

Browse files
core/vm: using testing.B.Loop (#32660)
before: go test -run=^$ -bench=. ./core/vm/... -timeout=1h 1841.87s user 40.96s system 124% cpu 25:15.76 total after: go test -run=^$ -bench=. ./core/vm/... -timeout=1h 1588.65s user 33.79s system 123% cpu 21:53.25 total --------- Co-authored-by: lightclient <[email protected]>
1 parent 0758a56 commit 79a4f76

File tree

5 files changed

+13
-30
lines changed

5 files changed

+13
-30
lines changed

core/vm/analysis_legacy_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,17 @@ func BenchmarkJumpdestAnalysis_1200k(bench *testing.B) {
6565
// 1.4 ms
6666
code := make([]byte, analysisCodeSize)
6767
bench.SetBytes(analysisCodeSize)
68-
bench.ResetTimer()
69-
for i := 0; i < bench.N; i++ {
68+
for bench.Loop() {
7069
codeBitmap(code)
7170
}
72-
bench.StopTimer()
7371
}
7472
func BenchmarkJumpdestHashing_1200k(bench *testing.B) {
7573
// 4 ms
7674
code := make([]byte, analysisCodeSize)
7775
bench.SetBytes(analysisCodeSize)
78-
bench.ResetTimer()
79-
for i := 0; i < bench.N; i++ {
76+
for bench.Loop() {
8077
crypto.Keccak256Hash(code)
8178
}
82-
bench.StopTimer()
8379
}
8480

8581
func BenchmarkJumpdestOpAnalysis(bench *testing.B) {
@@ -91,8 +87,7 @@ func BenchmarkJumpdestOpAnalysis(bench *testing.B) {
9187
code[i] = byte(op)
9288
}
9389
bits := make(BitVec, len(code)/8+1+4)
94-
b.ResetTimer()
95-
for i := 0; i < b.N; i++ {
90+
for b.Loop() {
9691
clear(bits)
9792
codeBitmapInternal(code, bits)
9893
}

core/vm/contracts_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,10 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) {
167167
bench.Run(fmt.Sprintf("%s-Gas=%d", test.Name, reqGas), func(bench *testing.B) {
168168
bench.ReportAllocs()
169169
start := time.Now()
170-
bench.ResetTimer()
171-
for i := 0; i < bench.N; i++ {
170+
for bench.Loop() {
172171
copy(data, in)
173172
res, _, err = RunPrecompiledContract(p, data, reqGas, nil)
174173
}
175-
bench.StopTimer()
176174
elapsed := uint64(time.Since(start))
177175
if elapsed < 1 {
178176
elapsed = 1

core/vm/instructions_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,13 @@ func opBenchmark(bench *testing.B, op executionFunc, args ...string) {
291291
intArgs[i] = new(uint256.Int).SetBytes(common.Hex2Bytes(arg))
292292
}
293293
pc := uint64(0)
294-
bench.ResetTimer()
295-
for i := 0; i < bench.N; i++ {
294+
for bench.Loop() {
296295
for _, arg := range intArgs {
297296
stack.push(arg)
298297
}
299298
op(&pc, evm, scope)
300299
stack.pop()
301300
}
302-
bench.StopTimer()
303301

304302
for i, arg := range args {
305303
want := new(uint256.Int).SetBytes(common.Hex2Bytes(arg))
@@ -551,8 +549,7 @@ func BenchmarkOpMstore(bench *testing.B) {
551549
memStart := new(uint256.Int)
552550
value := new(uint256.Int).SetUint64(0x1337)
553551

554-
bench.ResetTimer()
555-
for i := 0; i < bench.N; i++ {
552+
for bench.Loop() {
556553
stack.push(value)
557554
stack.push(memStart)
558555
opMstore(&pc, evm, &ScopeContext{mem, stack, nil})
@@ -609,8 +606,7 @@ func BenchmarkOpKeccak256(bench *testing.B) {
609606
pc := uint64(0)
610607
start := new(uint256.Int)
611608

612-
bench.ResetTimer()
613-
for i := 0; i < bench.N; i++ {
609+
for bench.Loop() {
614610
stack.push(uint256.NewInt(32))
615611
stack.push(start)
616612
opKeccak256(&pc, evm, &ScopeContext{mem, stack, nil})

core/vm/interpreter_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ func BenchmarkInterpreter(b *testing.B) {
9090
stack.push(uint256.NewInt(123))
9191
stack.push(uint256.NewInt(123))
9292
gasSStoreEIP3529 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP3529)
93-
b.ResetTimer()
94-
for i := 0; i < b.N; i++ {
93+
for b.Loop() {
9594
gasSStoreEIP3529(evm, contract, stack, mem, 1234)
9695
}
9796
}

core/vm/runtime/runtime_test.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ func BenchmarkCall(b *testing.B) {
150150
b.Fatal(err)
151151
}
152152

153-
b.ResetTimer()
154-
for i := 0; i < b.N; i++ {
153+
for b.Loop() {
155154
for j := 0; j < 400; j++ {
156155
Execute(code, cpurchase, nil)
157156
Execute(code, creceived, nil)
@@ -190,11 +189,9 @@ func benchmarkEVM_Create(bench *testing.B, code string) {
190189
EVMConfig: vm.Config{},
191190
}
192191
// Warm up the intpools and stuff
193-
bench.ResetTimer()
194-
for i := 0; i < bench.N; i++ {
192+
for bench.Loop() {
195193
Call(receiver, []byte{}, &runtimeConfig)
196194
}
197-
bench.StopTimer()
198195
}
199196

200197
func BenchmarkEVM_CREATE_500(bench *testing.B) {
@@ -233,8 +230,7 @@ func BenchmarkEVM_SWAP1(b *testing.B) {
233230
b.Run("10k", func(b *testing.B) {
234231
contractCode := swapContract(10_000)
235232
state.SetCode(contractAddr, contractCode, tracing.CodeChangeUnspecified)
236-
237-
for i := 0; i < b.N; i++ {
233+
for b.Loop() {
238234
_, _, err := Call(contractAddr, []byte{}, &Config{State: state})
239235
if err != nil {
240236
b.Fatal(err)
@@ -264,8 +260,7 @@ func BenchmarkEVM_RETURN(b *testing.B) {
264260

265261
contractCode := returnContract(n)
266262
state.SetCode(contractAddr, contractCode, tracing.CodeChangeUnspecified)
267-
268-
for i := 0; i < b.N; i++ {
263+
for b.Loop() {
269264
ret, _, err := Call(contractAddr, []byte{}, &Config{State: state})
270265
if err != nil {
271266
b.Fatal(err)
@@ -432,7 +427,7 @@ func benchmarkNonModifyingCode(gas uint64, code []byte, name string, tracerCode
432427

433428
b.Run(name, func(b *testing.B) {
434429
b.ReportAllocs()
435-
for i := 0; i < b.N; i++ {
430+
for b.Loop() {
436431
Call(destination, nil, cfg)
437432
}
438433
})

0 commit comments

Comments
 (0)