github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/eval/benchmarks_test.go (about) 1 package eval 2 3 import ( 4 "testing" 5 6 "src.elv.sh/pkg/parse" 7 ) 8 9 func BenchmarkEval_Empty(b *testing.B) { 10 benchmarkEval(b, "") 11 } 12 13 func BenchmarkEval_NopCommand(b *testing.B) { 14 benchmarkEval(b, "nop") 15 } 16 17 func BenchmarkEval_PutCommand(b *testing.B) { 18 benchmarkEval(b, "put x") 19 } 20 21 func BenchmarkEval_ForLoop100WithEmptyBody(b *testing.B) { 22 benchmarkEval(b, "for x [(range 100)] { }") 23 } 24 25 func BenchmarkEval_EachLoop100WithEmptyBody(b *testing.B) { 26 benchmarkEval(b, "range 100 | each [x]{ }") 27 } 28 29 func BenchmarkEval_LocalVariableAccess(b *testing.B) { 30 benchmarkEval(b, "x = val; nop $x") 31 } 32 33 func BenchmarkEval_UpVariableAccess(b *testing.B) { 34 benchmarkEval(b, "x = val; { nop $x }") 35 } 36 37 func benchmarkEval(b *testing.B, code string) { 38 ev := NewEvaler() 39 src := parse.Source{Name: "[benchmark]", Code: code} 40 41 tree, err := parse.Parse(src, parse.Config{}) 42 if err != nil { 43 panic(err) 44 } 45 op, err := ev.compile(tree, ev.Global(), nil) 46 if err != nil { 47 panic(err) 48 } 49 50 b.ResetTimer() 51 52 for i := 0; i < b.N; i++ { 53 fm, cleanup := ev.prepareFrame(src, EvalCfg{Global: ev.Global()}) 54 _, exec := op.prepare(fm) 55 _ = exec() 56 cleanup() 57 } 58 } 59 60 func BenchmarkOutputCapture_Overhead(b *testing.B) { 61 benchmarkOutputCapture(b.N, func(fm *Frame) {}) 62 } 63 64 func BenchmarkOutputCapture_Values(b *testing.B) { 65 benchmarkOutputCapture(b.N, func(fm *Frame) { 66 fm.OutputChan() <- "test" 67 }) 68 } 69 70 func BenchmarkOutputCapture_Bytes(b *testing.B) { 71 bytesToWrite := []byte("test") 72 benchmarkOutputCapture(b.N, func(fm *Frame) { 73 fm.OutputFile().Write(bytesToWrite) 74 }) 75 } 76 77 func BenchmarkOutputCapture_Mixed(b *testing.B) { 78 bytesToWrite := []byte("test") 79 benchmarkOutputCapture(b.N, func(fm *Frame) { 80 fm.OutputChan() <- false 81 fm.OutputFile().Write(bytesToWrite) 82 }) 83 } 84 85 func benchmarkOutputCapture(n int, f func(*Frame)) { 86 ev := NewEvaler() 87 fm := &Frame{Evaler: ev, local: ev.Global(), up: new(Ns)} 88 for i := 0; i < n; i++ { 89 fm.CaptureOutput(func(fm *Frame) error { 90 f(fm) 91 return nil 92 }) 93 } 94 }