| 282 | void BM_PolicySymbolic(benchmark::State& state) { |
| 283 | google::protobuf::Arena arena; |
| 284 | ASSERT_OK_AND_ASSIGN(ParsedExpr parsed_expr, parser::Parse(R"cel( |
| 285 | !(ip in ["10.0.1.4", "10.0.1.5", "10.0.1.6"]) && |
| 286 | ((path.startsWith("v1") && token in ["v1", "v2", "admin"]) || |
| 287 | (path.startsWith("v2") && token in ["v2", "admin"]) || |
| 288 | (path.startsWith("/admin") && token == "admin" && ip in [ |
| 289 | "10.0.1.1", "10.0.1.2", "10.0.1.3" |
| 290 | ]) |
| 291 | ))cel")); |
| 292 | |
| 293 | InterpreterOptions options = GetOptions(arena); |
| 294 | options.constant_folding = true; |
| 295 | options.constant_arena = &arena; |
| 296 | |
| 297 | auto builder = CreateCelExpressionBuilder(options); |
| 298 | ASSERT_OK(RegisterBuiltinFunctions(builder->GetRegistry(), options)); |
| 299 | |
| 300 | SourceInfo source_info; |
| 301 | ASSERT_OK_AND_ASSIGN(auto cel_expr, builder->CreateExpression( |
| 302 | &parsed_expr.expr(), &source_info)); |
| 303 | |
| 304 | Activation activation; |
| 305 | activation.InsertValue("ip", CelValue::CreateStringView(kIP)); |
| 306 | activation.InsertValue("path", CelValue::CreateStringView(kPath)); |
| 307 | activation.InsertValue("token", CelValue::CreateStringView(kToken)); |
| 308 | |
| 309 | for (auto _ : state) { |
| 310 | ASSERT_OK_AND_ASSIGN(CelValue result, |
| 311 | cel_expr->Evaluate(activation, &arena)); |
| 312 | ASSERT_TRUE(result.BoolOrDie()); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | BENCHMARK(BM_PolicySymbolic); |
| 317 | |
| 318 | class RequestMap : public CelMap { |
| 319 | public: |
| 320 | absl::optional<CelValue> operator[](CelValue key) const override { |
| 321 | if (!key.IsString()) { |
| 322 | return {}; |
| 323 | } |
| 324 | auto value = key.StringOrDie().value(); |
| 325 | if (value == "ip") { |
| 326 | return CelValue::CreateStringView(kIP); |
| 327 | } else if (value == "path") { |
| 328 | return CelValue::CreateStringView(kPath); |
| 329 | } else if (value == "token") { |
| 330 | return CelValue::CreateStringView(kToken); |
| 331 | } |
| 332 | return {}; |
| 333 | } |
| 334 | int size() const override { return 3; } |
| 335 | absl::StatusOr<const CelList*> ListKeys() const override { |
| 336 | return absl::UnimplementedError("CelMap::ListKeys is not implemented"); |
| 337 | } |
| 338 | }; |
| 339 | |
| 340 | // Uses a lazily constructed map container for "ip", "path", and "token". |
| 341 | void BM_PolicySymbolicMap(benchmark::State& state) { |
no test coverage detected