Skip to content

Commit d030d1e

Browse files
committed
add benchmarks
1 parent 60d09ef commit d030d1e

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

vm/vm_bench_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package vm_test
2+
3+
import (
4+
"runtime"
5+
"testing"
6+
7+
"github.com/expr-lang/expr"
8+
"github.com/expr-lang/expr/checker"
9+
"github.com/expr-lang/expr/compiler"
10+
"github.com/expr-lang/expr/conf"
11+
"github.com/expr-lang/expr/vm"
12+
)
13+
14+
func BenchmarkVM(b *testing.B) {
15+
cases := []struct {
16+
name, input string
17+
}{
18+
{"function calls", `
19+
func(
20+
func(
21+
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)),
22+
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)),
23+
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)),
24+
),
25+
func(
26+
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)),
27+
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)),
28+
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)),
29+
),
30+
func(
31+
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)),
32+
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)),
33+
func(func(a, 'a', 1, nil), func(a, 'a', 1, nil), func(a, 'a', 1, nil)),
34+
)
35+
)
36+
`},
37+
}
38+
39+
a := new(recursive)
40+
for i, b := 0, a; i < 40*4; i++ {
41+
b.Inner = new(recursive)
42+
b = b.Inner
43+
}
44+
45+
f := func(params ...any) (any, error) { return nil, nil }
46+
env := map[string]any{
47+
"a": a,
48+
"b": true,
49+
"func": f,
50+
}
51+
config := conf.New(env)
52+
expr.Function("func", f, f)(config)
53+
config.Check()
54+
55+
for _, c := range cases {
56+
tree, err := checker.ParseCheck(c.input, config)
57+
if err != nil {
58+
b.Fatal(c.input, "parse and check", err)
59+
}
60+
prog, err := compiler.Compile(tree, config)
61+
if err != nil {
62+
b.Fatal(c.input, "compile", err)
63+
}
64+
//b.Logf("disassembled:\n%s", prog.Disassemble())
65+
//b.FailNow()
66+
runtime.GC()
67+
68+
var vm vm.VM
69+
b.Run("name="+c.name, func(b *testing.B) {
70+
for i := 0; i < b.N; i++ {
71+
_, err = vm.Run(prog, env)
72+
}
73+
})
74+
if err != nil {
75+
b.Fatal(err)
76+
}
77+
}
78+
}
79+
80+
type recursive struct {
81+
Inner *recursive `expr:"a"`
82+
}

0 commit comments

Comments
 (0)