forked from Knetic/govaluate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarks_test.go
194 lines (156 loc) · 4.88 KB
/
benchmarks_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package govaluate
import (
"testing"
)
/*
Serves as a "water test" to give an idea of the general overhead of parsing
*/
func BenchmarkSingleParse(bench *testing.B) {
for i := 0; i < bench.N; i++ {
NewEvaluableExpression("1")
}
}
/*
The most common use case, a single variable, modified slightly, compared to a constant.
This is the "expected" use case of govaluate.
*/
func BenchmarkSimpleParse(bench *testing.B) {
for i := 0; i < bench.N; i++ {
NewEvaluableExpression("(requests_made * requests_succeeded / 100) >= 90")
}
}
/*
Benchmarks all syntax possibilities in one expression.
*/
func BenchmarkFullParse(bench *testing.B) {
var expression string
// represents all the major syntax possibilities.
expression = "2 > 1 &&" +
"'something' != 'nothing' || " +
"'2014-01-20' < 'Wed Jul 8 23:07:35 MDT 2015' && " +
"[escapedVariable name with spaces] <= unescaped\\-variableName &&" +
"modifierTest + 1000 / 2 > (80 * 100 % 2)"
for i := 0; i < bench.N; i++ {
NewEvaluableExpression(expression)
}
}
/*
Benchmarks the bare-minimum evaluation time
*/
func BenchmarkEvaluationSingle(bench *testing.B) {
expression, _ := NewEvaluableExpression("1")
bench.ResetTimer()
for i := 0; i < bench.N; i++ {
expression.Evaluate(nil)
}
}
/*
Benchmarks evaluation times of literals (no variables, no modifiers)
*/
func BenchmarkEvaluationNumericLiteral(bench *testing.B) {
expression, _ := NewEvaluableExpression("(2) > (1)")
bench.ResetTimer()
for i := 0; i < bench.N; i++ {
expression.Evaluate(nil)
}
}
/*
Benchmarks evaluation times of literals with modifiers
*/
func BenchmarkEvaluationLiteralModifiers(bench *testing.B) {
expression, _ := NewEvaluableExpression("(2) + (2) == (4)")
bench.ResetTimer()
for i := 0; i < bench.N; i++ {
expression.Evaluate(nil)
}
}
/*
Benchmarks evaluation times of parameters
*/
func BenchmarkEvaluationParameters(bench *testing.B) {
expression, _ := NewEvaluableExpression("requests_made > requests_succeeded")
parameters := map[string]interface{}{
"requests_made": 99.0,
"requests_succeeded": 90.0,
}
bench.ResetTimer()
for i := 0; i < bench.N; i++ {
expression.Evaluate(parameters)
}
}
/*
Benchmarks evaluation times of parameters + literals with modifiers
*/
func BenchmarkEvaluationParametersModifiers(bench *testing.B) {
expression, _ := NewEvaluableExpression("(requests_made * requests_succeeded / 100) >= 90")
parameters := map[string]interface{}{
"requests_made": 99.0,
"requests_succeeded": 90.0,
}
bench.ResetTimer()
for i := 0; i < bench.N; i++ {
expression.Evaluate(parameters)
}
}
/*
Benchmarks the ludicrously-unlikely worst-case expression,
one which uses all features.
This is largely a canary benchmark to make sure that any syntax additions don't
unnecessarily bloat the evaluation time.
*/
func BenchmarkComplexExpression(bench *testing.B) {
var expressionString string
expressionString = "2 > 1 &&" +
"'something' != 'nothing' || " +
"'2014-01-20' < 'Wed Jul 8 23:07:35 MDT 2015' && " +
"[escapedVariable name with spaces] <= unescaped\\-variableName &&" +
"modifierTest + 1000 / 2 > (80 * 100 % 2)"
expression, _ := NewEvaluableExpression(expressionString)
parameters := map[string]interface{}{
"escapedVariable name with spaces": 99.0,
"unescaped\\-variableName": 90.0,
"modifierTest": 5.0,
}
bench.ResetTimer()
for i := 0; i < bench.N; i++ {
expression.Evaluate(parameters)
}
}
/*
Benchmarks uncompiled parameter regex operators, which are the most expensive of the lot.
Note that regex compilation times are unpredictable and wily things. The regex engine has a lot of edge cases
and possible performance pitfalls. This test doesn't aim to be comprehensive against all possible regex scenarios,
it is primarily concerned with tracking how much longer it takes to compile a regex at evaluation-time than during parse-time.
*/
func BenchmarkRegexExpression(bench *testing.B) {
var expressionString string
expressionString = "(foo !~ bar) && (foobar =~ oba)"
expression, _ := NewEvaluableExpression(expressionString)
parameters := map[string]interface{}{
"foo": "foo",
"bar": "bar",
"baz": "baz",
"oba": ".*oba.*",
}
bench.ResetTimer()
for i := 0; i < bench.N; i++ {
expression.Evaluate(parameters)
}
}
/*
Benchmarks pre-compilable regex patterns. Meant to serve as a sanity check that constant strings used as regex patterns
are actually being precompiled.
Also demonstrates that (generally) compiling a regex at evaluation-time takes an order of magnitude more time than pre-compiling.
*/
func BenchmarkConstantRegexExpression(bench *testing.B) {
expressionString := "(foo !~ '[bB]az') && (bar =~ '[bB]ar')"
expression, _ := NewEvaluableExpression(expressionString)
parameters := map[string]interface{}{
"foo": "foo",
"bar": "bar",
}
bench.ResetTimer()
for i := 0; i < bench.N; i++ {
expression.Evaluate(parameters)
}
}