mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-05 03:17:50 +08:00
9e6555a4fb
Use b.Run to run benchmarks. Also remove the dedicated tests for output capture.
49 lines
937 B
Go
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()
|
|
}
|
|
})
|
|
}
|
|
}
|