Skip to content

Commit 3cf8171

Browse files
committed
cmd/evm: Rework execution stats
- Dump stats also for --bench flag. - From memory stats only show number and size of allocations. This is what `test -bench` shows. I doubt others like number of GC runs are any useful, but can be added if requested. - Now the mem stats are for single execution in case of --bench.
1 parent 32d31c3 commit 3cf8171

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

cmd/evm/runner.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,13 @@ func readGenesis(genesisPath string) *core.Genesis {
7070
return genesis
7171
}
7272

73-
func timedExec(bench bool, execFunc func() ([]byte, uint64, error)) ([]byte, uint64, time.Duration, error) {
74-
var (
75-
output []byte
76-
gasLeft uint64
77-
execTime time.Duration
78-
err error
79-
)
73+
type execStats struct {
74+
time time.Duration // The execution time.
75+
allocs int64 // The number of heap allocations during execution.
76+
bytesAllocated int64 // The cumulative number of bytes allocated during execution.
77+
}
8078

79+
func timedExec(bench bool, execFunc func() ([]byte, uint64, error)) (output []byte, gasLeft uint64, stats execStats, err error) {
8180
if bench {
8281
result := testing.Benchmark(func(b *testing.B) {
8382
for i := 0; i < b.N; i++ {
@@ -87,14 +86,21 @@ func timedExec(bench bool, execFunc func() ([]byte, uint64, error)) ([]byte, uin
8786

8887
// Get the average execution time from the benchmarking result.
8988
// There are other useful stats here that could be reported.
90-
execTime = time.Duration(result.NsPerOp())
89+
stats.time = time.Duration(result.NsPerOp())
90+
stats.allocs = result.AllocsPerOp()
91+
stats.bytesAllocated = result.AllocedBytesPerOp()
9192
} else {
93+
var memStatsBefore, memStatsAfter goruntime.MemStats
94+
goruntime.ReadMemStats(&memStatsBefore)
9295
startTime := time.Now()
9396
output, gasLeft, err = execFunc()
94-
execTime = time.Since(startTime)
97+
stats.time = time.Since(startTime)
98+
goruntime.ReadMemStats(&memStatsAfter)
99+
stats.allocs = int64(memStatsAfter.Mallocs - memStatsBefore.Mallocs)
100+
stats.bytesAllocated = int64(memStatsAfter.TotalAlloc - memStatsBefore.TotalAlloc)
95101
}
96102

97-
return output, gasLeft, execTime, err
103+
return output, gasLeft, stats, err
98104
}
99105

100106
func runCmd(ctx *cli.Context) error {
@@ -256,7 +262,8 @@ func runCmd(ctx *cli.Context) error {
256262
}
257263
}
258264

259-
output, leftOverGas, execTime, err := timedExec(ctx.GlobalBool(BenchFlag.Name), execFunc)
265+
bench := ctx.GlobalBool(BenchFlag.Name)
266+
output, leftOverGas, stats, err := timedExec(bench, execFunc)
260267

261268
if ctx.GlobalBool(DumpFlag.Name) {
262269
statedb.Commit(true)
@@ -286,17 +293,12 @@ func runCmd(ctx *cli.Context) error {
286293
vm.WriteLogs(os.Stderr, statedb.Logs())
287294
}
288295

289-
if ctx.GlobalBool(StatDumpFlag.Name) {
290-
var mem goruntime.MemStats
291-
goruntime.ReadMemStats(&mem)
292-
fmt.Fprintf(os.Stderr, `evm execution time: %v
293-
heap objects: %d
294-
allocations: %d
295-
total allocations: %d
296-
GC calls: %d
297-
Gas used: %d
298-
299-
`, execTime, mem.HeapObjects, mem.Alloc, mem.TotalAlloc, mem.NumGC, initialGas-leftOverGas)
296+
if bench || ctx.GlobalBool(StatDumpFlag.Name) {
297+
fmt.Fprintf(os.Stderr, `EVM gas used: %d
298+
execution time: %v
299+
allocations: %d
300+
allocated bytes: %d
301+
`, initialGas-leftOverGas, stats.time, stats.allocs, stats.bytesAllocated)
300302
}
301303
if tracer == nil {
302304
fmt.Printf("0x%x\n", output)

0 commit comments

Comments
 (0)