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 <lightclient@protonmail.com>
This commit is contained in:
cui 2025-09-20 07:06:55 +08:00 committed by GitHub
parent 0758a561d6
commit 79a4f76b03
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 13 additions and 30 deletions

View file

@ -65,21 +65,17 @@ func BenchmarkJumpdestAnalysis_1200k(bench *testing.B) {
// 1.4 ms // 1.4 ms
code := make([]byte, analysisCodeSize) code := make([]byte, analysisCodeSize)
bench.SetBytes(analysisCodeSize) bench.SetBytes(analysisCodeSize)
bench.ResetTimer() for bench.Loop() {
for i := 0; i < bench.N; i++ {
codeBitmap(code) codeBitmap(code)
} }
bench.StopTimer()
} }
func BenchmarkJumpdestHashing_1200k(bench *testing.B) { func BenchmarkJumpdestHashing_1200k(bench *testing.B) {
// 4 ms // 4 ms
code := make([]byte, analysisCodeSize) code := make([]byte, analysisCodeSize)
bench.SetBytes(analysisCodeSize) bench.SetBytes(analysisCodeSize)
bench.ResetTimer() for bench.Loop() {
for i := 0; i < bench.N; i++ {
crypto.Keccak256Hash(code) crypto.Keccak256Hash(code)
} }
bench.StopTimer()
} }
func BenchmarkJumpdestOpAnalysis(bench *testing.B) { func BenchmarkJumpdestOpAnalysis(bench *testing.B) {
@ -91,8 +87,7 @@ func BenchmarkJumpdestOpAnalysis(bench *testing.B) {
code[i] = byte(op) code[i] = byte(op)
} }
bits := make(BitVec, len(code)/8+1+4) bits := make(BitVec, len(code)/8+1+4)
b.ResetTimer() for b.Loop() {
for i := 0; i < b.N; i++ {
clear(bits) clear(bits)
codeBitmapInternal(code, bits) codeBitmapInternal(code, bits)
} }

View file

@ -167,12 +167,10 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) {
bench.Run(fmt.Sprintf("%s-Gas=%d", test.Name, reqGas), func(bench *testing.B) { bench.Run(fmt.Sprintf("%s-Gas=%d", test.Name, reqGas), func(bench *testing.B) {
bench.ReportAllocs() bench.ReportAllocs()
start := time.Now() start := time.Now()
bench.ResetTimer() for bench.Loop() {
for i := 0; i < bench.N; i++ {
copy(data, in) copy(data, in)
res, _, err = RunPrecompiledContract(p, data, reqGas, nil) res, _, err = RunPrecompiledContract(p, data, reqGas, nil)
} }
bench.StopTimer()
elapsed := uint64(time.Since(start)) elapsed := uint64(time.Since(start))
if elapsed < 1 { if elapsed < 1 {
elapsed = 1 elapsed = 1

View file

@ -291,15 +291,13 @@ func opBenchmark(bench *testing.B, op executionFunc, args ...string) {
intArgs[i] = new(uint256.Int).SetBytes(common.Hex2Bytes(arg)) intArgs[i] = new(uint256.Int).SetBytes(common.Hex2Bytes(arg))
} }
pc := uint64(0) pc := uint64(0)
bench.ResetTimer() for bench.Loop() {
for i := 0; i < bench.N; i++ {
for _, arg := range intArgs { for _, arg := range intArgs {
stack.push(arg) stack.push(arg)
} }
op(&pc, evm, scope) op(&pc, evm, scope)
stack.pop() stack.pop()
} }
bench.StopTimer()
for i, arg := range args { for i, arg := range args {
want := new(uint256.Int).SetBytes(common.Hex2Bytes(arg)) want := new(uint256.Int).SetBytes(common.Hex2Bytes(arg))
@ -551,8 +549,7 @@ func BenchmarkOpMstore(bench *testing.B) {
memStart := new(uint256.Int) memStart := new(uint256.Int)
value := new(uint256.Int).SetUint64(0x1337) value := new(uint256.Int).SetUint64(0x1337)
bench.ResetTimer() for bench.Loop() {
for i := 0; i < bench.N; i++ {
stack.push(value) stack.push(value)
stack.push(memStart) stack.push(memStart)
opMstore(&pc, evm, &ScopeContext{mem, stack, nil}) opMstore(&pc, evm, &ScopeContext{mem, stack, nil})
@ -609,8 +606,7 @@ func BenchmarkOpKeccak256(bench *testing.B) {
pc := uint64(0) pc := uint64(0)
start := new(uint256.Int) start := new(uint256.Int)
bench.ResetTimer() for bench.Loop() {
for i := 0; i < bench.N; i++ {
stack.push(uint256.NewInt(32)) stack.push(uint256.NewInt(32))
stack.push(start) stack.push(start)
opKeccak256(&pc, evm, &ScopeContext{mem, stack, nil}) opKeccak256(&pc, evm, &ScopeContext{mem, stack, nil})

View file

@ -90,8 +90,7 @@ func BenchmarkInterpreter(b *testing.B) {
stack.push(uint256.NewInt(123)) stack.push(uint256.NewInt(123))
stack.push(uint256.NewInt(123)) stack.push(uint256.NewInt(123))
gasSStoreEIP3529 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP3529) gasSStoreEIP3529 = makeGasSStoreFunc(params.SstoreClearsScheduleRefundEIP3529)
b.ResetTimer() for b.Loop() {
for i := 0; i < b.N; i++ {
gasSStoreEIP3529(evm, contract, stack, mem, 1234) gasSStoreEIP3529(evm, contract, stack, mem, 1234)
} }
} }

View file

@ -150,8 +150,7 @@ func BenchmarkCall(b *testing.B) {
b.Fatal(err) b.Fatal(err)
} }
b.ResetTimer() for b.Loop() {
for i := 0; i < b.N; i++ {
for j := 0; j < 400; j++ { for j := 0; j < 400; j++ {
Execute(code, cpurchase, nil) Execute(code, cpurchase, nil)
Execute(code, creceived, nil) Execute(code, creceived, nil)
@ -190,11 +189,9 @@ func benchmarkEVM_Create(bench *testing.B, code string) {
EVMConfig: vm.Config{}, EVMConfig: vm.Config{},
} }
// Warm up the intpools and stuff // Warm up the intpools and stuff
bench.ResetTimer() for bench.Loop() {
for i := 0; i < bench.N; i++ {
Call(receiver, []byte{}, &runtimeConfig) Call(receiver, []byte{}, &runtimeConfig)
} }
bench.StopTimer()
} }
func BenchmarkEVM_CREATE_500(bench *testing.B) { func BenchmarkEVM_CREATE_500(bench *testing.B) {
@ -233,8 +230,7 @@ func BenchmarkEVM_SWAP1(b *testing.B) {
b.Run("10k", func(b *testing.B) { b.Run("10k", func(b *testing.B) {
contractCode := swapContract(10_000) contractCode := swapContract(10_000)
state.SetCode(contractAddr, contractCode, tracing.CodeChangeUnspecified) state.SetCode(contractAddr, contractCode, tracing.CodeChangeUnspecified)
for b.Loop() {
for i := 0; i < b.N; i++ {
_, _, err := Call(contractAddr, []byte{}, &Config{State: state}) _, _, err := Call(contractAddr, []byte{}, &Config{State: state})
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
@ -264,8 +260,7 @@ func BenchmarkEVM_RETURN(b *testing.B) {
contractCode := returnContract(n) contractCode := returnContract(n)
state.SetCode(contractAddr, contractCode, tracing.CodeChangeUnspecified) state.SetCode(contractAddr, contractCode, tracing.CodeChangeUnspecified)
for b.Loop() {
for i := 0; i < b.N; i++ {
ret, _, err := Call(contractAddr, []byte{}, &Config{State: state}) ret, _, err := Call(contractAddr, []byte{}, &Config{State: state})
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
@ -432,7 +427,7 @@ func benchmarkNonModifyingCode(gas uint64, code []byte, name string, tracerCode
b.Run(name, func(b *testing.B) { b.Run(name, func(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for b.Loop() {
Call(destination, nil, cfg) Call(destination, nil, cfg)
} }
}) })