| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 29 |
type Case struct { |
| 30 |
|
| 31 |
Expr string |
| 32 |
|
| 33 |
|
| 34 |
Options []cel.EnvOption |
| 35 |
|
| 36 |
|
| 37 |
In any |
| 38 |
|
| 39 |
|
| 40 |
Out ref.Val |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
type DynamicEnvCase struct { |
| 45 |
|
| 46 |
Expr string |
| 47 |
|
| 48 |
|
| 49 |
Options func(b *testing.B) *cel.Env |
| 50 |
|
| 51 |
|
| 52 |
In any |
| 53 |
|
| 54 |
|
| 55 |
Out ref.Val |
| 56 |
} |
| 57 |
|
| 58 |
var ( |
| 59 |
|
| 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 |
|
| 195 |
ReferenceDynamicEnvCases = []*DynamicEnvCase{ |
| 196 |
|
| 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 |
|
| 243 |
|
| 244 |
|
| 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 |
|
| 253 |
func RunReferenceDynamicEnvCases(b *testing.B) { |
| 254 |
b.Helper() |
| 255 |
for _, rc := range ReferenceDynamicEnvCases { |
| 256 |
RunDynamicEnvCase(b, rc) |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 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 |
|
| 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 |
} |