mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
cmd/evm: make warm-up before bench-run
This commit is contained in:
parent
1f84917da0
commit
ca8beb9a73
1 changed files with 32 additions and 18 deletions
|
|
@ -81,32 +81,46 @@ type execStats struct {
|
||||||
GasUsed uint64 `json:"gasUsed"` // the amount of gas used during execution
|
GasUsed uint64 `json:"gasUsed"` // the amount of gas used during execution
|
||||||
}
|
}
|
||||||
|
|
||||||
func timedExec(bench bool, execFunc func() ([]byte, uint64, error)) (output []byte, stats execStats, err error) {
|
func timedExec(bench bool, execFunc func() ([]byte, uint64, error)) ([]byte, execStats, error) {
|
||||||
var gasUsed uint64
|
|
||||||
if bench {
|
if bench {
|
||||||
|
// Do one warm-up run
|
||||||
|
output, gasUsed, err := execFunc()
|
||||||
result := testing.Benchmark(func(b *testing.B) {
|
result := testing.Benchmark(func(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
output, gasUsed, err = execFunc()
|
haveOutput, haveGasUsed, haveErr := execFunc()
|
||||||
|
if !bytes.Equal(haveOutput, output) {
|
||||||
|
b.Fatalf("output differs, have\n%x\nwant%x\n", haveOutput, output)
|
||||||
|
}
|
||||||
|
if haveGasUsed != gasUsed {
|
||||||
|
b.Fatalf("gas differs, have %v want%v", haveGasUsed, gasUsed)
|
||||||
|
}
|
||||||
|
if haveErr != err {
|
||||||
|
b.Fatalf("err differs, have %v want%v", haveErr, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// Get the average execution time from the benchmarking result.
|
// Get the average execution time from the benchmarking result.
|
||||||
// There are other useful stats here that could be reported.
|
// There are other useful stats here that could be reported.
|
||||||
stats.Time = time.Duration(result.NsPerOp())
|
stats := execStats{
|
||||||
stats.Allocs = result.AllocsPerOp()
|
Time: time.Duration(result.NsPerOp()),
|
||||||
stats.BytesAllocated = result.AllocedBytesPerOp()
|
Allocs: result.AllocsPerOp(),
|
||||||
stats.GasUsed = gasUsed
|
BytesAllocated: result.AllocedBytesPerOp(),
|
||||||
} else {
|
GasUsed: gasUsed,
|
||||||
|
}
|
||||||
|
return output, stats, err
|
||||||
|
}
|
||||||
var memStatsBefore, memStatsAfter goruntime.MemStats
|
var memStatsBefore, memStatsAfter goruntime.MemStats
|
||||||
goruntime.ReadMemStats(&memStatsBefore)
|
goruntime.ReadMemStats(&memStatsBefore)
|
||||||
startTime := time.Now()
|
t0 := time.Now()
|
||||||
output, gasUsed, err = execFunc()
|
output, gasUsed, err := execFunc()
|
||||||
stats.Time = time.Since(startTime)
|
duration := time.Since(t0)
|
||||||
goruntime.ReadMemStats(&memStatsAfter)
|
goruntime.ReadMemStats(&memStatsAfter)
|
||||||
stats.Allocs = int64(memStatsAfter.Mallocs - memStatsBefore.Mallocs)
|
stats := execStats{
|
||||||
stats.BytesAllocated = int64(memStatsAfter.TotalAlloc - memStatsBefore.TotalAlloc)
|
Time: duration,
|
||||||
stats.GasUsed = gasUsed
|
Allocs: int64(memStatsAfter.Mallocs - memStatsBefore.Mallocs),
|
||||||
|
BytesAllocated: int64(memStatsAfter.TotalAlloc - memStatsBefore.TotalAlloc),
|
||||||
|
GasUsed: gasUsed,
|
||||||
}
|
}
|
||||||
|
|
||||||
return output, stats, err
|
return output, stats, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue