pkg/eval: Reorganize benchmarks.

Use b.Run to run benchmarks. Also remove the dedicated tests for output capture.
This commit is contained in:
Qi Xiao 2021-12-02 00:20:20 +00:00
parent 9dfda4c753
commit 9e6555a4fb

View File

@ -6,89 +6,43 @@ import (
"src.elv.sh/pkg/parse"
)
func BenchmarkEval_Empty(b *testing.B) {
benchmarkEval(b, "")
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_NopCommand(b *testing.B) {
benchmarkEval(b, "nop")
}
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}
func BenchmarkEval_PutCommand(b *testing.B) {
benchmarkEval(b, "put x")
}
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)
}
func BenchmarkEval_ForLoop100WithEmptyBody(b *testing.B) {
benchmarkEval(b, "for x [(range 100)] { }")
}
b.ResetTimer()
func BenchmarkEval_EachLoop100WithEmptyBody(b *testing.B) {
benchmarkEval(b, "range 100 | each {|_| }")
}
func BenchmarkEval_LocalVariableAccess(b *testing.B) {
benchmarkEval(b, "x = val; nop $x")
}
func BenchmarkEval_UpVariableAccess(b *testing.B) {
benchmarkEval(b, "x = val; { nop $x }")
}
func benchmarkEval(b *testing.B, code string) {
ev := NewEvaler()
src := parse.Source{Name: "[benchmark]", Code: 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()
}
}
func BenchmarkOutputCapture_Overhead(b *testing.B) {
benchmarkOutputCapture(b.N, func(fm *Frame) {})
}
func BenchmarkOutputCapture_Values(b *testing.B) {
benchmarkOutputCapture(b.N, func(fm *Frame) {
fm.ValueOutput().Put("test")
})
}
func BenchmarkOutputCapture_Bytes(b *testing.B) {
bytesToWrite := []byte("test")
benchmarkOutputCapture(b.N, func(fm *Frame) {
fm.ByteOutput().Write(bytesToWrite)
})
}
func BenchmarkOutputCapture_Mixed(b *testing.B) {
bytesToWrite := []byte("test")
benchmarkOutputCapture(b.N, func(fm *Frame) {
fm.ValueOutput().Put(false)
fm.ByteOutput().Write(bytesToWrite)
})
}
func benchmarkOutputCapture(n int, f func(*Frame)) {
ev := NewEvaler()
fm := &Frame{Evaler: ev, local: ev.Global(), up: new(Ns), ports: DummyPorts}
for i := 0; i < n; i++ {
fm.CaptureOutput(func(fm *Frame) error {
f(fm)
return nil
for i := 0; i < b.N; i++ {
fm, cleanup := ev.prepareFrame(src, EvalCfg{Global: ev.Global()})
_, exec := op.prepare(fm)
_ = exec()
cleanup()
}
})
}
}