elvish/pkg/eval/benchmarks_test.go
Qi Xiao 9e6555a4fb pkg/eval: Reorganize benchmarks.
Use b.Run to run benchmarks. Also remove the dedicated tests for output capture.
2021-12-02 00:30:01 +00:00

49 lines
937 B
Go

package eval
import (
"testing"
"src.elv.sh/pkg/parse"
)
var benchmarks = []struct {
name string
code string
}{
{"empty", ""},
{"nop", "nop"},
{"nop-nop", "nop | nop"},
{"put-x", "put x"},
{"for-100", "for x [(range 100)] { }"},
{"range-100", "range 100 | each {|_| }"},
{"read-local", "var x = val; nop $x"},
{"read-upval", "var x = val; { nop $x }"},
}
func BenchmarkEval(b *testing.B) {
for _, bench := range benchmarks {
b.Run(bench.name, func(b *testing.B) {
ev := NewEvaler()
src := parse.Source{Name: "[benchmark]", Code: bench.code}
tree, err := parse.Parse(src, parse.Config{})
if err != nil {
panic(err)
}
op, err := ev.compile(tree, ev.Global(), nil)
if err != nil {
panic(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
fm, cleanup := ev.prepareFrame(src, EvalCfg{Global: ev.Global()})
_, exec := op.prepare(fm)
_ = exec()
cleanup()
}
})
}
}