-
Notifications
You must be signed in to change notification settings - Fork 1
/
starlark_test.go
62 lines (52 loc) · 1.4 KB
/
starlark_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package expressionevaluation
import (
"strings"
"testing"
expr "go.starlark.net/starlark"
"go.starlark.net/syntax"
)
func BenchmarkStarlark(b *testing.B) {
for _, tt := range tests {
benchmarkStarlark(b, tt.name, tt.input, tt.expression)
}
}
func benchmarkStarlark(b *testing.B, prefix string, input Input, expression string) {
expression = strings.ReplaceAll(expression, "||", "and")
expression = strings.ReplaceAll(expression, "&&", "and")
b.Run(testName(prefix, "Compile"), func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
programStarlark(b, expression)
}
b.StopTimer()
})
thread := &expr.Thread{Name: "example"}
predeclared := expr.StringDict{
"ID": expr.MakeInt(input.ID),
"Name": expr.String(input.Name),
"City": expr.String(input.City),
"Country": expr.String(input.Country),
"Currency": expr.String(input.Currency),
}
eval := programStarlark(b, expression)
b.Run(testName(prefix, "Evaluate"), func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
out, err := expr.EvalExpr(thread, eval, predeclared)
if err != nil && !out.(expr.Bool).Truth() {
b.Fatal(err)
}
}
b.StopTimer()
})
}
func programStarlark(b *testing.B, expression string) syntax.Expr {
program, err := syntax.ParseExpr("example.star", expression, syntax.RetainComments)
if err != nil {
b.Fatal(err)
}
if err != nil {
b.Fatal(err)
}
return program
}