|
13 | 13 | #include "eval/public/cel_expression.h" |
14 | 14 | #include "eval/public/cel_value.h" |
15 | 15 | #include "eval/public/containers/container_backed_list_impl.h" |
| 16 | +#include "eval/public/containers/container_backed_map_impl.h" |
16 | 17 | #include "eval/public/structs/cel_proto_wrapper.h" |
17 | 18 | #include "eval/tests/request_context.pb.h" |
18 | 19 | #include "base/status_macros.h" |
@@ -827,6 +828,91 @@ void BM_Comprehension(benchmark::State& state) { |
827 | 828 |
|
828 | 829 | BENCHMARK(BM_Comprehension)->Range(1, 1 << 20); |
829 | 830 |
|
| 831 | +// has(request.path) && !has(request.ip) |
| 832 | +constexpr char kHas[] = R"( |
| 833 | +call_expr: < |
| 834 | + function: "_&&_" |
| 835 | + args: < |
| 836 | + select_expr: < |
| 837 | + operand: < |
| 838 | + ident_expr: < |
| 839 | + name: "request" |
| 840 | + > |
| 841 | + > |
| 842 | + field: "path" |
| 843 | + test_only: true |
| 844 | + > |
| 845 | + > |
| 846 | + args: < |
| 847 | + call_expr: < |
| 848 | + function: "!_" |
| 849 | + args: < |
| 850 | + select_expr: < |
| 851 | + operand: < |
| 852 | + ident_expr: < |
| 853 | + name: "request" |
| 854 | + > |
| 855 | + > |
| 856 | + field: "ip" |
| 857 | + test_only: true |
| 858 | + > |
| 859 | + > |
| 860 | + > |
| 861 | + > |
| 862 | +>)"; |
| 863 | + |
| 864 | +void BM_HasMap(benchmark::State& state) { |
| 865 | + google::protobuf::Arena arena; |
| 866 | + Expr expr; |
| 867 | + Activation activation; |
| 868 | + ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(kHas, &expr)); |
| 869 | + auto builder = CreateCelExpressionBuilder(); |
| 870 | + ASSERT_OK(RegisterBuiltinFunctions(builder->GetRegistry())); |
| 871 | + auto expr_plan = builder->CreateExpression(&expr, nullptr); |
| 872 | + |
| 873 | + std::vector<std::pair<CelValue, CelValue>> map_pairs{ |
| 874 | + {CelValue::CreateStringView("path"), CelValue::CreateStringView("path")}}; |
| 875 | + auto cel_map = |
| 876 | + CreateContainerBackedMap(absl::Span<std::pair<CelValue, CelValue>>( |
| 877 | + map_pairs.data(), map_pairs.size())); |
| 878 | + |
| 879 | + activation.InsertValue("request", CelValue::CreateMap((*cel_map).get())); |
| 880 | + ASSERT_OK(expr_plan.status()); |
| 881 | + for (auto _ : state) { |
| 882 | + auto result = expr_plan.value()->Evaluate(activation, &arena); |
| 883 | + ASSERT_OK(result.status()); |
| 884 | + ASSERT_TRUE(result->IsBool()); |
| 885 | + ASSERT_TRUE(result->BoolOrDie()); |
| 886 | + } |
| 887 | +} |
| 888 | + |
| 889 | +BENCHMARK(BM_HasMap); |
| 890 | + |
| 891 | +void BM_HasProto(benchmark::State& state) { |
| 892 | + google::protobuf::Arena arena; |
| 893 | + Expr expr; |
| 894 | + Activation activation; |
| 895 | + ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(kHas, &expr)); |
| 896 | + auto builder = CreateCelExpressionBuilder(); |
| 897 | + ASSERT_OK(RegisterBuiltinFunctions(builder->GetRegistry())); |
| 898 | + auto expr_plan = builder->CreateExpression(&expr, nullptr); |
| 899 | + |
| 900 | + RequestContext request; |
| 901 | + request.set_path(kPath); |
| 902 | + request.set_token(kToken); |
| 903 | + activation.InsertValue("request", |
| 904 | + CelProtoWrapper::CreateMessage(&request, &arena)); |
| 905 | + ASSERT_OK(expr_plan.status()); |
| 906 | + for (auto _ : state) { |
| 907 | + auto result = expr_plan.value()->Evaluate(activation, &arena); |
| 908 | + ASSERT_OK(result.status()); |
| 909 | + ASSERT_TRUE(result->IsBool()); |
| 910 | + ASSERT_TRUE(result->BoolOrDie()); |
| 911 | + } |
| 912 | +} |
| 913 | + |
| 914 | +BENCHMARK(BM_HasProto); |
| 915 | + |
830 | 916 | // Sum a square with a nested comprehension |
831 | 917 | constexpr char kNestedListSum[] = R"( |
832 | 918 | id: 1 |
|
0 commit comments