Skip to content

Commit f57f215

Browse files
jwasingerholiman
andcommitted
cmd/evm: benchmarking via statetest command + filter by name, index and fork (#30442)
When `evm statetest --bench` is specified, benchmark the execution similarly to `evm run`. Also adds the ability to filter tests by name, index and fork. --------- Co-authored-by: Martin Holst Swende <[email protected]>
1 parent b362c37 commit f57f215

File tree

3 files changed

+144
-61
lines changed

3 files changed

+144
-61
lines changed

cmd/evm/runner.go

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,36 +76,53 @@ func readGenesis(genesisPath string) *core.Genesis {
7676
}
7777

7878
type execStats struct {
79-
time time.Duration // The execution time.
80-
allocs int64 // The number of heap allocations during execution.
81-
bytesAllocated int64 // The cumulative number of bytes allocated during execution.
79+
Time time.Duration `json:"time"` // The execution Time.
80+
Allocs int64 `json:"allocs"` // The number of heap allocations during execution.
81+
BytesAllocated int64 `json:"bytesAllocated"` // The cumulative number of bytes allocated during execution.
82+
GasUsed uint64 `json:"gasUsed"` // the amount of gas used during execution
8283
}
8384

84-
func timedExec(bench bool, execFunc func() ([]byte, uint64, error)) (output []byte, gasLeft uint64, stats execStats, err error) {
85+
func timedExec(bench bool, execFunc func() ([]byte, uint64, error)) ([]byte, execStats, error) {
8586
if bench {
87+
// Do one warm-up run
88+
output, gasUsed, err := execFunc()
8689
result := testing.Benchmark(func(b *testing.B) {
8790
for i := 0; i < b.N; i++ {
88-
output, gasLeft, err = execFunc()
91+
haveOutput, haveGasUsed, haveErr := execFunc()
92+
if !bytes.Equal(haveOutput, output) {
93+
b.Fatalf("output differs, have\n%x\nwant%x\n", haveOutput, output)
94+
}
95+
if haveGasUsed != gasUsed {
96+
b.Fatalf("gas differs, have %v want%v", haveGasUsed, gasUsed)
97+
}
98+
if haveErr != err {
99+
b.Fatalf("err differs, have %v want%v", haveErr, err)
100+
}
89101
}
90102
})
91-
92103
// Get the average execution time from the benchmarking result.
93104
// There are other useful stats here that could be reported.
94-
stats.time = time.Duration(result.NsPerOp())
95-
stats.allocs = result.AllocsPerOp()
96-
stats.bytesAllocated = result.AllocedBytesPerOp()
97-
} else {
98-
var memStatsBefore, memStatsAfter goruntime.MemStats
99-
goruntime.ReadMemStats(&memStatsBefore)
100-
startTime := time.Now()
101-
output, gasLeft, err = execFunc()
102-
stats.time = time.Since(startTime)
103-
goruntime.ReadMemStats(&memStatsAfter)
104-
stats.allocs = int64(memStatsAfter.Mallocs - memStatsBefore.Mallocs)
105-
stats.bytesAllocated = int64(memStatsAfter.TotalAlloc - memStatsBefore.TotalAlloc)
105+
stats := execStats{
106+
Time: time.Duration(result.NsPerOp()),
107+
Allocs: result.AllocsPerOp(),
108+
BytesAllocated: result.AllocedBytesPerOp(),
109+
GasUsed: gasUsed,
110+
}
111+
return output, stats, err
106112
}
107-
108-
return output, gasLeft, stats, err
113+
var memStatsBefore, memStatsAfter goruntime.MemStats
114+
goruntime.ReadMemStats(&memStatsBefore)
115+
t0 := time.Now()
116+
output, gasUsed, err := execFunc()
117+
duration := time.Since(t0)
118+
goruntime.ReadMemStats(&memStatsAfter)
119+
stats := execStats{
120+
Time: duration,
121+
Allocs: int64(memStatsAfter.Mallocs - memStatsBefore.Mallocs),
122+
BytesAllocated: int64(memStatsAfter.TotalAlloc - memStatsBefore.TotalAlloc),
123+
GasUsed: gasUsed,
124+
}
125+
return output, stats, err
109126
}
110127

111128
func runCmd(ctx *cli.Context) error {
@@ -265,12 +282,13 @@ func runCmd(ctx *cli.Context) error {
265282
statedb.SetCode(receiver, code)
266283
}
267284
execFunc = func() ([]byte, uint64, error) {
268-
return runtime.Call(receiver, input, &runtimeConfig)
285+
output, gasLeft, err := runtime.Call(receiver, input, &runtimeConfig)
286+
return output, initialGas - gasLeft, err
269287
}
270288
}
271289

272290
bench := ctx.Bool(BenchFlag.Name)
273-
output, leftOverGas, stats, err := timedExec(bench, execFunc)
291+
output, stats, err := timedExec(bench, execFunc)
274292

275293
if ctx.Bool(DumpFlag.Name) {
276294
root, err := statedb.Commit(genesisConfig.Number, true)
@@ -300,7 +318,7 @@ func runCmd(ctx *cli.Context) error {
300318
execution time: %v
301319
allocations: %d
302320
allocated bytes: %d
303-
`, initialGas-leftOverGas, stats.time, stats.allocs, stats.bytesAllocated)
321+
`, stats.GasUsed, stats.Time, stats.Allocs, stats.BytesAllocated)
304322
}
305323
if tracer == nil {
306324
fmt.Printf("%#x\n", output)

cmd/evm/staterunner.go

Lines changed: 95 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,51 @@ import (
2727
"github.com/ethereum/go-ethereum/core/state"
2828
"github.com/ethereum/go-ethereum/core/vm"
2929
"github.com/ethereum/go-ethereum/eth/tracers/logger"
30+
"github.com/ethereum/go-ethereum/internal/flags"
3031
"github.com/ethereum/go-ethereum/tests"
3132
"github.com/urfave/cli/v2"
3233
)
3334

35+
var (
36+
forkFlag = &cli.StringFlag{
37+
Name: "statetest.fork",
38+
Usage: "The hard-fork to run the test against",
39+
Category: flags.VMCategory,
40+
}
41+
idxFlag = &cli.IntFlag{
42+
Name: "statetest.index",
43+
Usage: "The index of the subtest to run",
44+
Category: flags.VMCategory,
45+
Value: -1, // default to select all subtest indices
46+
}
47+
testNameFlag = &cli.StringFlag{
48+
Name: "statetest.name",
49+
Usage: "The name of the state test to run",
50+
Category: flags.VMCategory,
51+
}
52+
)
3453
var stateTestCommand = &cli.Command{
3554
Action: stateTestCmd,
3655
Name: "statetest",
3756
Usage: "Executes the given state tests. Filenames can be fed via standard input (batch mode) or as an argument (one-off execution).",
3857
ArgsUsage: "<file>",
58+
Flags: []cli.Flag{
59+
forkFlag,
60+
idxFlag,
61+
testNameFlag,
62+
},
3963
}
4064

4165
// StatetestResult contains the execution status after running a state test, any
4266
// error that might have occurred and a dump of the final state if requested.
4367
type StatetestResult struct {
44-
Name string `json:"name"`
45-
Pass bool `json:"pass"`
46-
Root *common.Hash `json:"stateRoot,omitempty"`
47-
Fork string `json:"fork"`
48-
Error string `json:"error,omitempty"`
49-
State *state.Dump `json:"state,omitempty"`
68+
Name string `json:"name"`
69+
Pass bool `json:"pass"`
70+
Root *common.Hash `json:"stateRoot,omitempty"`
71+
Fork string `json:"fork"`
72+
Error string `json:"error,omitempty"`
73+
State *state.Dump `json:"state,omitempty"`
74+
BenchStats *execStats `json:"benchStats,omitempty"`
5075
}
5176

5277
func stateTestCmd(ctx *cli.Context) error {
@@ -67,7 +92,7 @@ func stateTestCmd(ctx *cli.Context) error {
6792
}
6893
// Load the test content from the input file
6994
if len(ctx.Args().First()) != 0 {
70-
return runStateTest(ctx.Args().First(), cfg, ctx.Bool(DumpFlag.Name))
95+
return runStateTest(ctx, ctx.Args().First(), cfg, ctx.Bool(DumpFlag.Name), ctx.Bool(BenchFlag.Name))
7196
}
7297
// Read filenames from stdin and execute back-to-back
7398
scanner := bufio.NewScanner(os.Stdin)
@@ -76,15 +101,48 @@ func stateTestCmd(ctx *cli.Context) error {
76101
if len(fname) == 0 {
77102
return nil
78103
}
79-
if err := runStateTest(fname, cfg, ctx.Bool(DumpFlag.Name)); err != nil {
104+
if err := runStateTest(ctx, fname, cfg, ctx.Bool(DumpFlag.Name), ctx.Bool(BenchFlag.Name)); err != nil {
80105
return err
81106
}
82107
}
83108
return nil
84109
}
85110

111+
type stateTestCase struct {
112+
name string
113+
test tests.StateTest
114+
st tests.StateSubtest
115+
}
116+
117+
// collectMatchedSubtests returns test cases which match against provided filtering CLI parameters
118+
func collectMatchedSubtests(ctx *cli.Context, testsByName map[string]tests.StateTest) []stateTestCase {
119+
var res []stateTestCase
120+
subtestName := ctx.String(testNameFlag.Name)
121+
if subtestName != "" {
122+
if subtest, ok := testsByName[subtestName]; ok {
123+
testsByName := make(map[string]tests.StateTest)
124+
testsByName[subtestName] = subtest
125+
}
126+
}
127+
idx := ctx.Int(idxFlag.Name)
128+
fork := ctx.String(forkFlag.Name)
129+
130+
for key, test := range testsByName {
131+
for _, st := range test.Subtests() {
132+
if idx != -1 && st.Index != idx {
133+
continue
134+
}
135+
if fork != "" && st.Fork != fork {
136+
continue
137+
}
138+
res = append(res, stateTestCase{name: key, st: st, test: test})
139+
}
140+
}
141+
return res
142+
}
143+
86144
// runStateTest loads the state-test given by fname, and executes the test.
87-
func runStateTest(fname string, cfg vm.Config, dump bool) error {
145+
func runStateTest(ctx *cli.Context, fname string, cfg vm.Config, dump bool, bench bool) error {
88146
src, err := os.ReadFile(fname)
89147
if err != nil {
90148
return err
@@ -94,31 +152,38 @@ func runStateTest(fname string, cfg vm.Config, dump bool) error {
94152
return err
95153
}
96154

155+
matchingTests := collectMatchedSubtests(ctx, testsByName)
156+
97157
// Iterate over all the tests, run them and aggregate the results
98-
results := make([]StatetestResult, 0, len(testsByName))
99-
for key, test := range testsByName {
100-
for _, st := range test.Subtests() {
101-
// Run the test and aggregate the result
102-
result := &StatetestResult{Name: key, Fork: st.Fork, Pass: true}
103-
test.Run(st, cfg, false, rawdb.HashScheme, func(err error, tstate *tests.StateTestState) {
104-
var root common.Hash
105-
if tstate.StateDB != nil {
106-
root = tstate.StateDB.IntermediateRoot(false)
107-
result.Root = &root
108-
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%#x\"}\n", root)
109-
if dump { // Dump any state to aid debugging
110-
cpy, _ := state.New(root, tstate.StateDB.Database())
111-
dump := cpy.RawDump(nil)
112-
result.State = &dump
113-
}
114-
}
115-
if err != nil {
116-
// Test failed, mark as so
117-
result.Pass, result.Error = false, err.Error()
158+
var results []StatetestResult
159+
for _, test := range matchingTests {
160+
// Run the test and aggregate the result
161+
result := &StatetestResult{Name: test.name, Fork: test.st.Fork, Pass: true}
162+
test.test.Run(test.st, cfg, false, rawdb.HashScheme, func(err error, tstate *tests.StateTestState) {
163+
var root common.Hash
164+
if tstate.StateDB != nil {
165+
root = tstate.StateDB.IntermediateRoot(false)
166+
result.Root = &root
167+
fmt.Fprintf(os.Stderr, "{\"stateRoot\": \"%#x\"}\n", root)
168+
if dump { // Dump any state to aid debugging
169+
cpy, _ := state.New(root, tstate.StateDB.Database())
170+
dump := cpy.RawDump(nil)
171+
result.State = &dump
118172
}
173+
}
174+
if err != nil {
175+
// Test failed, mark as so
176+
result.Pass, result.Error = false, err.Error()
177+
}
178+
})
179+
if bench {
180+
_, stats, _ := timedExec(true, func() ([]byte, uint64, error) {
181+
_, _, gasUsed, _ := test.test.RunNoVerify(test.st, cfg, false, rawdb.HashScheme)
182+
return nil, gasUsed, nil
119183
})
120-
results = append(results, *result)
184+
result.BenchStats = &stats
121185
}
186+
results = append(results, *result)
122187
}
123188
out, _ := json.MarshalIndent(results, "", " ")
124189
fmt.Println(string(out))

tests/state_test_util.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (t *StateTest) checkError(subtest StateSubtest, err error) error {
196196

197197
// Run executes a specific subtest and verifies the post-state and logs
198198
func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config, snapshotter bool, scheme string, postCheck func(err error, st *StateTestState)) (result error) {
199-
st, root, err := t.RunNoVerify(subtest, vmconfig, snapshotter, scheme)
199+
st, root, _, err := t.RunNoVerify(subtest, vmconfig, snapshotter, scheme)
200200
// Invoke the callback at the end of function for further analysis.
201201
defer func() {
202202
postCheck(result, &st)
@@ -228,10 +228,10 @@ func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config, snapshotter bo
228228

229229
// RunNoVerify runs a specific subtest and returns the statedb and post-state root.
230230
// Remember to call state.Close after verifying the test result!
231-
func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapshotter bool, scheme string) (st StateTestState, root common.Hash, err error) {
231+
func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapshotter bool, scheme string) (st StateTestState, root common.Hash, gasUsed uint64, err error) {
232232
config, eips, err := GetChainConfig(subtest.Fork)
233233
if err != nil {
234-
return st, common.Hash{}, UnsupportedForkError{subtest.Fork}
234+
return st, common.Hash{}, 0, UnsupportedForkError{subtest.Fork}
235235
}
236236
vmconfig.ExtraEips = eips
237237

@@ -250,7 +250,7 @@ func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapsh
250250
post := t.json.Post[subtest.Fork][subtest.Index]
251251
msg, err := t.json.Tx.toMessage(post, baseFee)
252252
if err != nil {
253-
return st, common.Hash{}, err
253+
return st, common.Hash{}, 0, err
254254
}
255255

256256
{ // Blob transactions may be present after the Cancun fork.
@@ -260,7 +260,7 @@ func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapsh
260260
// Here, we just do this shortcut smaller fix, since state tests do not
261261
// utilize those codepaths
262262
if len(msg.BlobHashes)*params.BlobTxBlobGasPerBlob > params.MaxBlobGasPerBlock {
263-
return st, common.Hash{}, errors.New("blob gas exceeds maximum")
263+
return st, common.Hash{}, 0, errors.New("blob gas exceeds maximum")
264264
}
265265
}
266266

@@ -269,10 +269,10 @@ func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapsh
269269
var ttx types.Transaction
270270
err := ttx.UnmarshalBinary(post.TxBytes)
271271
if err != nil {
272-
return st, common.Hash{}, err
272+
return st, common.Hash{}, 0, err
273273
}
274274
if _, err := types.Sender(types.LatestSigner(config), &ttx); err != nil {
275-
return st, common.Hash{}, err
275+
return st, common.Hash{}, 0, err
276276
}
277277
}
278278

@@ -322,7 +322,7 @@ func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapsh
322322
receipt := &types.Receipt{GasUsed: vmRet.UsedGas}
323323
tracer.OnTxEnd(receipt, nil)
324324
}
325-
return st, root, err
325+
return st, root, vmRet.UsedGas, err
326326
}
327327

328328
func (t *StateTest) gasLimit(subtest StateSubtest) uint64 {

0 commit comments

Comments
 (0)