-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_filter_test.go
49 lines (40 loc) · 1.01 KB
/
eval_filter_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
package expressionevaluation
import (
"fmt"
"testing"
expr "github.com/skx/evalfilter/v2"
)
func BenchmarkEvalFilter(b *testing.B) {
for _, tt := range tests {
benchmarkEvalFilter(b, tt.name, tt.input, tt.expression)
}
}
func benchmarkEvalFilter(b *testing.B, prefix string, input Input, expression string) {
expression = fmt.Sprintf(`if ( %s ) { return true; } else { return false; }`, expression)
b.Run(testName(prefix, "Compile"), func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
programEvalFilter(b, input, expression)
}
b.StopTimer()
})
eval := programEvalFilter(b, input, expression)
b.Run(testName(prefix, "Evaluate"), func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
out, err := eval.Run(input)
if err != nil && !out {
b.Fatal(err)
}
}
b.StopTimer()
})
}
func programEvalFilter(b *testing.B, input Input, expression string) *expr.Eval {
eval := expr.New(expression)
err := eval.Prepare()
if err != nil {
b.Fatal(err)
}
return eval
}