Skip to content

Commit 683c244

Browse files
TristonianJoneskyessenov
authored andcommitted
Add microbenchmarks for has() macro.
PiperOrigin-RevId: 368237226
1 parent 6faa5a0 commit 683c244

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

eval/tests/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ cc_test(
2424
"//eval/public:cel_expression",
2525
"//eval/public:cel_value",
2626
"//eval/public/containers:container_backed_list_impl",
27+
"//eval/public/containers:container_backed_map_impl",
2728
"//eval/public/structs:cel_proto_wrapper",
2829
"@com_github_google_benchmark//:benchmark",
2930
"@com_github_google_benchmark//:benchmark_main",

eval/tests/benchmark_test.cc

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "eval/public/cel_expression.h"
1414
#include "eval/public/cel_value.h"
1515
#include "eval/public/containers/container_backed_list_impl.h"
16+
#include "eval/public/containers/container_backed_map_impl.h"
1617
#include "eval/public/structs/cel_proto_wrapper.h"
1718
#include "eval/tests/request_context.pb.h"
1819
#include "base/status_macros.h"
@@ -827,6 +828,91 @@ void BM_Comprehension(benchmark::State& state) {
827828

828829
BENCHMARK(BM_Comprehension)->Range(1, 1 << 20);
829830

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+
830916
// Sum a square with a nested comprehension
831917
constexpr char kNestedListSum[] = R"(
832918
id: 1

0 commit comments

Comments
 (0)