2020-08-23 23:07:11 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/parse"
|
2020-08-23 23:07:11 +08:00
|
|
|
)
|
|
|
|
|
2021-12-02 08:20:20 +08:00
|
|
|
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()
|
|
|
|
}
|
2020-08-23 23:07:11 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|