| 30 | #include "google/protobuf/text_format.h" |
| 31 | |
| 32 | ABSL_FLAG(bool, enable_optimizations, false, "enable const folding opt"); |
| 33 | ABSL_FLAG(bool, enable_recursive_planning, false, "enable recursive planning"); |
| 34 | |
| 35 | namespace google { |
| 36 | namespace api { |
| 37 | namespace expr { |
| 38 | namespace runtime { |
| 39 | |
| 40 | namespace { |
| 41 | |
| 42 | using ::cel::expr::Expr; |
| 43 | using ::cel::expr::ParsedExpr; |
| 44 | using ::cel::expr::SourceInfo; |
| 45 | using ::google::rpc::context::AttributeContext; |
| 46 | |
| 47 | InterpreterOptions GetOptions(google::protobuf::Arena& arena) { |
| 48 | InterpreterOptions options; |
| 49 | |
| 50 | if (absl::GetFlag(FLAGS_enable_optimizations)) { |
| 51 | options.constant_arena = &arena; |
| 52 | options.constant_folding = true; |
| 53 | } |
| 54 | |
| 55 | if (absl::GetFlag(FLAGS_enable_recursive_planning)) { |
| 56 | options.max_recursion_depth = -1; |
| 57 | } |
| 58 | |
| 59 | return options; |
| 60 | } |
| 61 | |
| 62 | // Benchmark test |
| 63 | // Evaluates cel expression: |
| 64 | // '1 + 1 + 1 .... +1' |
| 65 | static void BM_Eval(benchmark::State& state) { |
| 66 | google::protobuf::Arena arena; |
| 67 | InterpreterOptions options = GetOptions(arena); |
| 68 | |
| 69 | auto builder = CreateCelExpressionBuilder(options); |
| 70 | ASSERT_OK(RegisterBuiltinFunctions(builder->GetRegistry(), options)); |
| 71 | |
| 72 | int len = state.range(0); |
| 73 | |
| 74 | Expr root_expr; |
| 75 | Expr* cur_expr = &root_expr; |
| 76 | |
| 77 | for (int i = 0; i < len; i++) { |
| 78 | Expr::Call* call = cur_expr->mutable_call_expr(); |
| 79 | call->set_function("_+_"); |
| 80 | call->add_args()->mutable_const_expr()->set_int64_value(1); |
| 81 | cur_expr = call->add_args(); |
| 82 | } |
| 83 | |
| 84 | cur_expr->mutable_const_expr()->set_int64_value(1); |
| 85 | |
| 86 | SourceInfo source_info; |
| 87 | ASSERT_OK_AND_ASSIGN(auto cel_expr, |
| 88 | builder->CreateExpression(&root_expr, &source_info)); |
| 89 |
nothing calls this directly
no test coverage detected