From ba4cbf978d8c2fb8a0583cbd72a7fa3227ce49bf Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Tue, 6 Sep 2022 19:39:49 +0100 Subject: [PATCH 1/5] test(state): report mgas/s metric in EVM benchmark --- tests/state_test.go | 17 ++++++++++++++++- tests/testdata | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/state_test.go b/tests/state_test.go index 965ef71ba40e..386fb164ad24 100644 --- a/tests/state_test.go +++ b/tests/state_test.go @@ -26,6 +26,7 @@ import ( "reflect" "strings" "testing" + "time" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb" @@ -242,12 +243,26 @@ func runBenchmark(b *testing.B, t *StateTest) { for n := 0; n < b.N; n++ { // Execute the message. snapshot := statedb.Snapshot() - _, _, err = evm.Call(sender, *msg.To(), msg.Data(), msg.Gas(), msg.Value()) + start := time.Now() + + b.StartTimer() + _, leftOverGas, err := evm.Call(sender, *msg.To(), msg.Data(), msg.Gas(), msg.Value()) if err != nil { b.Error(err) return } + b.StopTimer() + statedb.RevertToSnapshot(snapshot) + + elapsed := uint64(time.Since(start)) + if elapsed < 1 { + elapsed = 1 + } + gasUsed := msg.Gas() - leftOverGas + // Keep it as uint64, multiply 100 to get two digit float later + mgasps := (100 * 1000 * gasUsed) / elapsed + b.ReportMetric(float64(mgasps)/100, "mgas/s") } }) } diff --git a/tests/testdata b/tests/testdata index a380655e5ffa..092a8834dc44 160000 --- a/tests/testdata +++ b/tests/testdata @@ -1 +1 @@ -Subproject commit a380655e5ffab1a5ea0f4d860224bdb19013f06a +Subproject commit 092a8834dc445e683103689d6f0e75a5d380a190 From d60e3f383418c51c5cb939a2d20f554ef07717d9 Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Wed, 7 Sep 2022 12:44:58 +0100 Subject: [PATCH 2/5] revert testdata submodule update --- tests/testdata | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testdata b/tests/testdata index 092a8834dc44..a380655e5ffa 160000 --- a/tests/testdata +++ b/tests/testdata @@ -1 +1 @@ -Subproject commit 092a8834dc445e683103689d6f0e75a5d380a190 +Subproject commit a380655e5ffab1a5ea0f4d860224bdb19013f06a From 8cae0cc4d876f6414b27581ef9201c7b9a0faf05 Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Thu, 8 Sep 2022 12:25:14 +0100 Subject: [PATCH 3/5] aggregate mgas/s results --- tests/state_test.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/state_test.go b/tests/state_test.go index 386fb164ad24..8265b3c8d5e9 100644 --- a/tests/state_test.go +++ b/tests/state_test.go @@ -239,31 +239,31 @@ func runBenchmark(b *testing.B, t *StateTest) { sender := vm.NewContract(vm.AccountRef(msg.From()), vm.AccountRef(msg.From()), nil, 0) + var gasUsed uint64 + start := time.Now() + b.ResetTimer() for n := 0; n < b.N; n++ { - // Execute the message. snapshot := statedb.Snapshot() - start := time.Now() - b.StartTimer() + // Execute the message. _, leftOverGas, err := evm.Call(sender, *msg.To(), msg.Data(), msg.Gas(), msg.Value()) if err != nil { b.Error(err) return } b.StopTimer() - statedb.RevertToSnapshot(snapshot) + gasUsed += msg.Gas() - leftOverGas + } - elapsed := uint64(time.Since(start)) - if elapsed < 1 { - elapsed = 1 - } - gasUsed := msg.Gas() - leftOverGas - // Keep it as uint64, multiply 100 to get two digit float later - mgasps := (100 * 1000 * gasUsed) / elapsed - b.ReportMetric(float64(mgasps)/100, "mgas/s") + elapsed := uint64(time.Since(start)) + if elapsed < 1 { + elapsed = 1 } + // Keep it as uint64, multiply 100 to get two digit float later + mgasps := (100 * 1000 * gasUsed) / elapsed + b.ReportMetric(float64(mgasps)/100, "mgas/s") }) } } From cfdda8cae2370cee236a73d0f2a1e8d8568d67c7 Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Thu, 8 Sep 2022 12:37:25 +0100 Subject: [PATCH 4/5] calculate elapsed time better --- tests/state_test.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/state_test.go b/tests/state_test.go index 8265b3c8d5e9..240ded57be24 100644 --- a/tests/state_test.go +++ b/tests/state_test.go @@ -240,24 +240,29 @@ func runBenchmark(b *testing.B, t *StateTest) { nil, 0) var gasUsed uint64 - start := time.Now() + var elapsed uint64 b.ResetTimer() for n := 0; n < b.N; n++ { snapshot := statedb.Snapshot() + b.StartTimer() + start := time.Now() + // Execute the message. _, leftOverGas, err := evm.Call(sender, *msg.To(), msg.Data(), msg.Gas(), msg.Value()) if err != nil { b.Error(err) return } + b.StopTimer() - statedb.RevertToSnapshot(snapshot) + elapsed += uint64(time.Since(start)) gasUsed += msg.Gas() - leftOverGas + + statedb.RevertToSnapshot(snapshot) } - elapsed := uint64(time.Since(start)) if elapsed < 1 { elapsed = 1 } From 30d843fa90e891658dd0d69a41f63722355bce0b Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Wed, 28 Sep 2022 08:39:55 +0200 Subject: [PATCH 5/5] tests: benchmarks - handle access list + take refund into account --- tests/state_test.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/tests/state_test.go b/tests/state_test.go index 240ded57be24..4317bd31a42f 100644 --- a/tests/state_test.go +++ b/tests/state_test.go @@ -193,12 +193,14 @@ func runBenchmark(b *testing.B, t *StateTest) { b.Error(err) return } + var rules = config.Rules(new(big.Int), false) + vmconfig.ExtraEips = eips block := t.genesis(config).ToBlock(nil) _, statedb := MakePreState(rawdb.NewMemoryDatabase(), t.json.Pre, false) var baseFee *big.Int - if config.IsLondon(new(big.Int)) { + if rules.IsLondon { baseFee = t.json.Env.BaseFee if baseFee == nil { // Retesteth uses `0x10` for genesis baseFee. Therefore, it defaults to @@ -239,13 +241,17 @@ func runBenchmark(b *testing.B, t *StateTest) { sender := vm.NewContract(vm.AccountRef(msg.From()), vm.AccountRef(msg.From()), nil, 0) - var gasUsed uint64 - var elapsed uint64 - + var ( + gasUsed uint64 + elapsed uint64 + refund uint64 + ) b.ResetTimer() for n := 0; n < b.N; n++ { snapshot := statedb.Snapshot() - + if rules.IsBerlin { + statedb.PrepareAccessList(msg.From(), msg.To(), vm.ActivePrecompiles(rules), msg.AccessList()) + } b.StartTimer() start := time.Now() @@ -258,16 +264,16 @@ func runBenchmark(b *testing.B, t *StateTest) { b.StopTimer() elapsed += uint64(time.Since(start)) + refund += statedb.GetRefund() gasUsed += msg.Gas() - leftOverGas statedb.RevertToSnapshot(snapshot) } - if elapsed < 1 { elapsed = 1 } // Keep it as uint64, multiply 100 to get two digit float later - mgasps := (100 * 1000 * gasUsed) / elapsed + mgasps := (100 * 1000 * (gasUsed - refund)) / elapsed b.ReportMetric(float64(mgasps)/100, "mgas/s") }) }