| 1 | // Copyright 2023 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Package bench defines a structure for benchmarked tests against custom CEL environments. |
| 16 | package bench |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | "testing" |
| 21 | |
| 22 | "github.com/google/cel-go/cel" |
| 23 | "github.com/google/cel-go/common/types" |
| 24 | "github.com/google/cel-go/common/types/ref" |
| 25 | "github.com/google/cel-go/ext" |
| 26 | ) |
| 27 | |
| 28 | // Case represents a human-readable expression and an expected output given an input |
| 29 | type Case struct { |
| 30 | // Expr is a human-readable expression which is expected to compile. |
| 31 | Expr string |
| 32 | |
| 33 | // Options indicate additional pieces of configuration such as CEL libraries, variables, and functions. |
| 34 | Options []cel.EnvOption |
| 35 | |
| 36 | // In is expected to be a map[string]any or cel.Activation instance representing the input to the expression. |
| 37 | In any |
| 38 | |
| 39 | // Out is the expected CEL valued output. |
| 40 | Out ref.Val |
| 41 | } |
| 42 | |
| 43 | // DynamicEnvCase represents an expression compiled in a dynamic environment. |
| 44 | type DynamicEnvCase struct { |
| 45 | // Expr is a human-readable expression which is expected to compile. |
| 46 | Expr string |
| 47 | |
| 48 | // Options indicate additional pieces of configuration such as CEL libraries, variables, and functions. |
| 49 | Options func(b *testing.B) *cel.Env |
| 50 | |
| 51 | // In is expected to be a map[string]any or cel.Activation instance representing the input to the expression. |
| 52 | In any |
| 53 | |
| 54 | // Out is the expected CEL valued output. |
| 55 | Out ref.Val |
| 56 | } |
| 57 | |
| 58 | var ( |
| 59 | // ReferenceCases represent canonical CEL expressions for common use cases. |
| 60 | ReferenceCases = []*Case{ |
| 61 | { |
| 62 | Expr: `string_value == 'value'`, |
| 63 | Options: []cel.EnvOption{ |
| 64 | cel.Variable("string_value", cel.StringType), |
| 65 | }, |
| 66 | In: map[string]any{ |
| 67 | "string_value": "value", |
| 68 | }, |
| 69 | Out: types.True, |
| 70 | }, |
| 71 | { |
| 72 | Expr: `string_value != 'value'`, |
| 73 | Options: []cel.EnvOption{ |
| 74 | cel.Variable("string_value", cel.StringType), |
| 75 | }, |
| 76 | In: map[string]any{ |
| 77 | "string_value": "value", |
| 78 | }, |
| 79 | Out: types.False, |
| 80 | }, |
| 81 | { |
| 82 | Expr: `'value' in list_value`, |
| 83 | Options: []cel.EnvOption{ |
| 84 | cel.Variable("list_value", cel.ListType(cel.StringType)), |
| 85 | }, |
| 86 | In: map[string]any{ |
| 87 | "list_value": []string{"a", "b", "c", "value"}, |
| 88 | }, |
| 89 | Out: types.True, |
| 90 | }, |
| 91 | { |
| 92 | Expr: `!('value' in list_value)`, |
| 93 | Options: []cel.EnvOption{ |
| 94 | cel.Variable("list_value", cel.ListType(cel.StringType)), |
| 95 | }, |
| 96 | In: map[string]any{ |
| 97 | "list_value": []string{"a", "b", "c", "d"}, |
| 98 | }, |
| 99 | Out: types.True, |
| 100 | }, |
| 101 | { |
| 102 | Expr: `x in ['a', 'b', 'c', 'd']`, |
| 103 | Options: []cel.EnvOption{ |
| 104 | cel.Variable("x", cel.StringType), |
| 105 | }, |
| 106 | In: map[string]any{ |
| 107 | "x": "c", |
| 108 | }, |
| 109 | Out: types.True, |
| 110 | }, |
| 111 | { |
| 112 | Expr: `!(x in ['a', 'b', 'c', 'd'])`, |
| 113 | Options: []cel.EnvOption{ |
| 114 | cel.Variable("x", cel.StringType), |
| 115 | }, |
| 116 | In: map[string]any{ |
| 117 | "x": "e", |
| 118 | }, |
| 119 | Out: types.True, |
| 120 | }, |
| 121 | { |
| 122 | Expr: `x in list_value`, |
| 123 | Options: []cel.EnvOption{ |
| 124 | cel.Variable("x", cel.StringType), |
| 125 | cel.Variable("list_value", cel.ListType(cel.StringType)), |
| 126 | }, |
| 127 | In: map[string]any{ |
| 128 | "x": "c", |
| 129 | "list_value": []string{"a", "b", "c", "d"}, |
| 130 | }, |
| 131 | Out: types.True, |
| 132 | }, |
| 133 | { |
| 134 | Expr: `!(x in list_value)`, |
| 135 | Options: []cel.EnvOption{ |
| 136 | cel.Variable("x", cel.StringType), |
| 137 | cel.Variable("list_value", cel.ListType(cel.StringType)), |
| 138 | }, |
| 139 | In: map[string]any{ |
| 140 | "x": "e", |
| 141 | "list_value": []string{"a", "b", "c", "d"}, |
| 142 | }, |
| 143 | Out: types.True, |
| 144 | }, |
| 145 | { |
| 146 | Expr: `list_value.exists(e, e.contains('cd'))`, |
| 147 | Options: []cel.EnvOption{ |
| 148 | cel.Variable("list_value", cel.ListType(cel.StringType)), |
| 149 | }, |
| 150 | In: map[string]any{ |
| 151 | "list_value": []string{"abc", "bcd", "cde", "def"}, |
| 152 | }, |
| 153 | Out: types.True, |
| 154 | }, |
| 155 | { |
| 156 | Expr: `list_value.exists(e, e.startsWith('cd'))`, |
| 157 | Options: []cel.EnvOption{ |
| 158 | cel.Variable("list_value", cel.ListType(cel.StringType)), |
| 159 | }, |
| 160 | In: map[string]any{ |
| 161 | "list_value": []string{"abc", "bcd", "cde", "def"}, |
| 162 | }, |
| 163 | Out: types.True, |
| 164 | }, |
| 165 | { |
| 166 | Expr: `list_value.exists(e, e.matches('cd*'))`, |
| 167 | Options: []cel.EnvOption{ |
| 168 | cel.Variable("list_value", cel.ListType(cel.StringType)), |
| 169 | }, |
| 170 | In: map[string]any{ |
| 171 | "list_value": []string{"abc", "bcd", "cde", "def"}, |
| 172 | }, |
| 173 | Out: types.True, |
| 174 | }, |
| 175 | { |
| 176 | Expr: `list_value.filter(e, e.matches('^cd+')) == ['cde']`, |
| 177 | Options: []cel.EnvOption{ |
| 178 | cel.Variable("list_value", cel.ListType(cel.StringType)), |
| 179 | }, |
| 180 | In: map[string]any{ |
| 181 | "list_value": []string{"abc", "bcd", "cde", "def"}, |
| 182 | }, |
| 183 | Out: types.True, |
| 184 | }, |
| 185 | { |
| 186 | Expr: `'formatted list: %s, size: %d'.format([['abc', 'cde'], 2])`, |
| 187 | Options: []cel.EnvOption{ |
| 188 | ext.Strings(), |
| 189 | }, |
| 190 | In: map[string]any{}, |
| 191 | Out: types.String(`formatted list: ["abc", "cde"], size: 2`), |
| 192 | }, |
| 193 | } |
| 194 | // ReferenceDynamicEnvCases represent CEL expressions compiled in an extended environment. |
| 195 | ReferenceDynamicEnvCases = []*DynamicEnvCase{ |
| 196 | // Base case without extended environment. |
| 197 | { |
| 198 | Expr: `a+b`, |
| 199 | Options: func(b *testing.B) *cel.Env { |
| 200 | stdenv, err := cel.NewEnv(cel.Variable("a", cel.IntType), cel.Variable("b", cel.IntType)) |
| 201 | if err != nil { |
| 202 | b.Fatalf("cel.NewEnv() failed: %v", err) |
| 203 | } |
| 204 | return stdenv |
| 205 | }, |
| 206 | }, |
| 207 | { |
| 208 | Expr: `x == y && y == z`, |
| 209 | Options: func(b *testing.B) *cel.Env { |
| 210 | |
| 211 | stdenv, err := cel.NewEnv(cel.Variable("x", cel.IntType), cel.Variable("y", cel.IntType)) |
| 212 | if err != nil { |
| 213 | b.Fatalf("cel.NewEnv() failed: %v", err) |
| 214 | } |
| 215 | stdenv.Compile("x == y") |
| 216 | stdenv, err = stdenv.Extend(cel.Variable("z", cel.IntType)) |
| 217 | if err != nil { |
| 218 | b.Fatalf("cel.Extend() failed: %v", err) |
| 219 | } |
| 220 | return stdenv |
| 221 | }, |
| 222 | }, |
| 223 | { |
| 224 | Expr: `x + y + z`, |
| 225 | Options: func(b *testing.B) *cel.Env { |
| 226 | |
| 227 | stdenv, err := cel.NewEnv(cel.Variable("x", cel.IntType), cel.Variable("y", cel.IntType)) |
| 228 | if err != nil { |
| 229 | b.Fatalf("cel.NewEnv() failed: %v", err) |
| 230 | } |
| 231 | stdenv.Compile("x + y") |
| 232 | stdenv, err = stdenv.Extend(cel.Variable("z", cel.IntType)) |
| 233 | if err != nil { |
| 234 | b.Fatalf("cel.Extend() failed: %v", err) |
| 235 | } |
| 236 | return stdenv |
| 237 | }, |
| 238 | }, |
| 239 | } |
| 240 | ) |
| 241 | |
| 242 | // RunReferenceCases evaluates the set of ReferenceCases against a custom CEL environment. |
| 243 | // |
| 244 | // See: bench_test.go for an example. |
| 245 | func RunReferenceCases(b *testing.B, env *cel.Env) { |
| 246 | b.Helper() |
| 247 | for _, rc := range ReferenceCases { |
| 248 | RunCase(b, env, rc) |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // RunReferenceDynamicEnvCases evaluates the set of ReferenceDynamicEnvCases. |
| 253 | func RunReferenceDynamicEnvCases(b *testing.B) { |
| 254 | b.Helper() |
| 255 | for _, rc := range ReferenceDynamicEnvCases { |
| 256 | RunDynamicEnvCase(b, rc) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // RunCase evaluates a single test case against a custom environment, running three different |
| 261 | // variants of the expression: optimized, unoptimized, and trace. |
| 262 | // |
| 263 | // * `optimized` - applies the cel.EvalOptions(cel.OptOptimize) flag. |
| 264 | // * `unoptimized` - no optimization flags applied. |
| 265 | // * `trace` - observes the evaluation state of an expression. |
| 266 | // |
| 267 | // In many cases the evaluation times may be similar, but when running comparisons against the |
| 268 | // baseline CEL environment, it may be useful to characterize the performance of the custom |
| 269 | // environment against the baseline. |
| 270 | func RunCase(b *testing.B, env *cel.Env, bc *Case) { |
| 271 | b.Helper() |
| 272 | var err error |
| 273 | if len(bc.Options) > 0 { |
| 274 | env, err = env.Extend(bc.Options...) |
| 275 | if err != nil { |
| 276 | b.Fatalf("env.Extend() failed: %v", err) |
| 277 | } |
| 278 | } |
| 279 | ast, iss := env.Compile(bc.Expr) |
| 280 | if iss.Err() != nil { |
| 281 | b.Fatalf("env.Compile(%v) failed: %v", bc.Expr, iss.Err()) |
| 282 | } |
| 283 | opts := map[string][]cel.ProgramOption{ |
| 284 | "optimized": {cel.EvalOptions(cel.OptOptimize)}, |
| 285 | "unoptimized": {}, |
| 286 | "trace": {cel.EvalOptions(cel.OptTrackState)}, |
| 287 | } |
| 288 | optOrder := []string{"optimized", "unoptimized", "trace"} |
| 289 | for _, name := range optOrder { |
| 290 | opt := opts[name] |
| 291 | b.Run(fmt.Sprintf("%s/%s", bc.Expr, name), func(b *testing.B) { |
| 292 | prg, err := env.Program(ast, opt...) |
| 293 | if err != nil { |
| 294 | b.Fatalf("env.Program(%v) failed: %v", bc.Expr, err) |
| 295 | } |
| 296 | b.ResetTimer() |
| 297 | for i := 0; i < b.N; i++ { |
| 298 | var input any = cel.NoVars() |
| 299 | if bc.In != nil { |
| 300 | input = bc.In |
| 301 | } |
| 302 | out, _, err := prg.Eval(input) |
| 303 | if err != nil { |
| 304 | b.Fatalf("prg.Eval(%v) failed: %v", input, err) |
| 305 | } |
| 306 | if out.Equal(bc.Out) != types.True { |
| 307 | b.Fatalf("prg.Eval(%v) got %v, wanted %v", input, out, bc.Out) |
| 308 | } |
| 309 | } |
| 310 | }) |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // RunDynamicEnvCase runs a singular DynamicEnvCase. |
| 315 | func RunDynamicEnvCase(b *testing.B, bc *DynamicEnvCase) { |
| 316 | b.Helper() |
| 317 | b.Run(bc.Expr, func(b *testing.B) { |
| 318 | env := bc.Options(b) |
| 319 | b.ResetTimer() |
| 320 | for i := 0; i < b.N; i++ { |
| 321 | _, err := env.Compile(bc.Expr) |
| 322 | if err != nil { |
| 323 | b.Fatalf("env.Compile(%v) failed: %v", bc.Expr, err) |
| 324 | } |
| 325 | } |
| 326 | }) |
| 327 | } |