forked from google/cel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.go
243 lines (233 loc) · 6.51 KB
/
bench.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package bench defines a structure for benchmarked tests against custom CEL environments.
package bench
import (
"fmt"
"testing"
"github.com/authzed/cel-go/cel"
"github.com/authzed/cel-go/common/types"
"github.com/authzed/cel-go/common/types/ref"
"github.com/authzed/cel-go/ext"
)
// Case represents a human-readable expression and an expected output given an input
type Case struct {
// Expr is a human-readable expression which is expected to compile.
Expr string
// Options indicate additional pieces of configuration such as CEL libraries, variables, and functions.
Options []cel.EnvOption
// In is expected to be a map[string]any or interpreter.Activation instance representing the input to the expression.
In any
// Out is the expected CEL valued output.
Out ref.Val
}
var (
// ReferenceCases represent canonical CEL expressions for common use cases.
ReferenceCases = []*Case{
{
Expr: `string_value == 'value'`,
Options: []cel.EnvOption{
cel.Variable("string_value", cel.StringType),
},
In: map[string]any{
"string_value": "value",
},
Out: types.True,
},
{
Expr: `string_value != 'value'`,
Options: []cel.EnvOption{
cel.Variable("string_value", cel.StringType),
},
In: map[string]any{
"string_value": "value",
},
Out: types.False,
},
{
Expr: `'value' in list_value`,
Options: []cel.EnvOption{
cel.Variable("list_value", cel.ListType(cel.StringType)),
},
In: map[string]any{
"list_value": []string{"a", "b", "c", "value"},
},
Out: types.True,
},
{
Expr: `!('value' in list_value)`,
Options: []cel.EnvOption{
cel.Variable("list_value", cel.ListType(cel.StringType)),
},
In: map[string]any{
"list_value": []string{"a", "b", "c", "d"},
},
Out: types.True,
},
{
Expr: `x in ['a', 'b', 'c', 'd']`,
Options: []cel.EnvOption{
cel.Variable("x", cel.StringType),
},
In: map[string]any{
"x": "c",
},
Out: types.True,
},
{
Expr: `!(x in ['a', 'b', 'c', 'd'])`,
Options: []cel.EnvOption{
cel.Variable("x", cel.StringType),
},
In: map[string]any{
"x": "e",
},
Out: types.True,
},
{
Expr: `x in list_value`,
Options: []cel.EnvOption{
cel.Variable("x", cel.StringType),
cel.Variable("list_value", cel.ListType(cel.StringType)),
},
In: map[string]any{
"x": "c",
"list_value": []string{"a", "b", "c", "d"},
},
Out: types.True,
},
{
Expr: `!(x in list_value)`,
Options: []cel.EnvOption{
cel.Variable("x", cel.StringType),
cel.Variable("list_value", cel.ListType(cel.StringType)),
},
In: map[string]any{
"x": "e",
"list_value": []string{"a", "b", "c", "d"},
},
Out: types.True,
},
{
Expr: `list_value.exists(e, e.contains('cd'))`,
Options: []cel.EnvOption{
cel.Variable("list_value", cel.ListType(cel.StringType)),
},
In: map[string]any{
"list_value": []string{"abc", "bcd", "cde", "def"},
},
Out: types.True,
},
{
Expr: `list_value.exists(e, e.startsWith('cd'))`,
Options: []cel.EnvOption{
cel.Variable("list_value", cel.ListType(cel.StringType)),
},
In: map[string]any{
"list_value": []string{"abc", "bcd", "cde", "def"},
},
Out: types.True,
},
{
Expr: `list_value.exists(e, e.matches('cd*'))`,
Options: []cel.EnvOption{
cel.Variable("list_value", cel.ListType(cel.StringType)),
},
In: map[string]any{
"list_value": []string{"abc", "bcd", "cde", "def"},
},
Out: types.True,
},
{
Expr: `list_value.filter(e, e.matches('^cd+')) == ['cde']`,
Options: []cel.EnvOption{
cel.Variable("list_value", cel.ListType(cel.StringType)),
},
In: map[string]any{
"list_value": []string{"abc", "bcd", "cde", "def"},
},
Out: types.True,
},
{
Expr: `'formatted list: %s, size: %d'.format([['abc', 'cde'], 2])`,
Options: []cel.EnvOption{
ext.Strings(),
},
In: map[string]any{},
Out: types.String(`formatted list: ["abc", "cde"], size: 2`),
},
}
)
// RunReferenceCases evaluates the set of ReferenceCases against a custom CEL environment.
//
// See: bench_test.go for an example.
func RunReferenceCases(b *testing.B, env *cel.Env) {
b.Helper()
for _, rc := range ReferenceCases {
RunCase(b, env, rc)
}
}
// RunCase evaluates a single test case against a custom environment, running three different
// variants of the expression: optimized, unoptimized, and trace.
//
// * `optimized` - applies the cel.EvalOptions(cel.OptOptimize) flag.
// * `unoptimized` - no optimization flags applied.
// * `trace` - observes the evaluation state of an expression.
//
// In many cases the evaluation times may be similar, but when running comparisons against the
// baseline CEL environment, it may be useful to characterize the performance of the custom
// environment against the baseline.
func RunCase(b *testing.B, env *cel.Env, bc *Case) {
b.Helper()
var err error
if len(bc.Options) > 0 {
env, err = env.Extend(bc.Options...)
if err != nil {
b.Fatalf("env.Extend() failed: %v", err)
}
}
ast, iss := env.Compile(bc.Expr)
if iss.Err() != nil {
b.Fatalf("env.Compile(%v) failed: %v", bc.Expr, iss.Err())
}
opts := map[string][]cel.ProgramOption{
"optimized": {cel.EvalOptions(cel.OptOptimize)},
"unoptimized": {},
"trace": {cel.EvalOptions(cel.OptTrackState)},
}
optOrder := []string{"optimized", "unoptimized", "trace"}
for _, name := range optOrder {
opt := opts[name]
b.Run(fmt.Sprintf("%s/%s", bc.Expr, name), func(b *testing.B) {
prg, err := env.Program(ast, opt...)
if err != nil {
b.Fatalf("env.Program(%v) failed: %v", bc.Expr, err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
var input any = cel.NoVars()
if bc.In != nil {
input = bc.In
}
out, _, err := prg.Eval(input)
if err != nil {
b.Fatalf("prg.Eval(%v) failed: %v", input, err)
}
if out.Equal(bc.Out) != types.True {
b.Fatalf("prg.Eval(%v) got %v, wanted %v", input, out, bc.Out)
}
}
})
}
}