From e46e94ec7f7a34691cc442001a2a3c0cb61c9eb7 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 7 Oct 2020 14:55:27 -0700 Subject: [PATCH] Added benchmarks for proto2. --- BUILD | 1 + tests/benchmark.cc | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/BUILD b/BUILD index f6217178a8..5c1e424578 100644 --- a/BUILD +++ b/BUILD @@ -358,6 +358,7 @@ cc_binary( ":descriptor_upb_proto", ":descriptor_upbreflection", "@com_github_google_benchmark//:benchmark_main", + "@com_google_protobuf//:protobuf", ], ) diff --git a/tests/benchmark.cc b/tests/benchmark.cc index 453adebc3b..15f8871303 100644 --- a/tests/benchmark.cc +++ b/tests/benchmark.cc @@ -3,6 +3,7 @@ #include #include "google/protobuf/descriptor.upb.h" #include "google/protobuf/descriptor.upbdefs.h" +#include "google/protobuf/descriptor.pb.h" upb_strview descriptor = google_protobuf_descriptor_proto_upbdefinit.descriptor; @@ -63,6 +64,53 @@ static void BM_ParseDescriptor(benchmark::State& state) { } BENCHMARK(BM_ParseDescriptor); +static void BM_ParseDescriptorProto2NoArena(benchmark::State& state) { + size_t bytes = 0; + for (auto _ : state) { + google::protobuf::FileDescriptorProto proto; + bool ok = proto.ParseFromArray(descriptor.data, descriptor.size); + + if (!ok) { + printf("Failed to parse.\n"); + exit(1); + } + bytes += descriptor.size; + } + state.SetBytesProcessed(state.iterations() * descriptor.size); +} +BENCHMARK(BM_ParseDescriptorProto2NoArena); + +static void BM_ParseDescriptorProto2WithArena(benchmark::State& state) { + size_t bytes = 0; + for (auto _ : state) { + google::protobuf::Arena arena; + auto proto = + google::protobuf::Arena::Create( + &arena); + bool ok = proto->ParseFromArray(descriptor.data, descriptor.size); + + if (!ok) { + printf("Failed to parse.\n"); + exit(1); + } + bytes += descriptor.size; + } + state.SetBytesProcessed(state.iterations() * descriptor.size); +} +BENCHMARK(BM_ParseDescriptorProto2WithArena); + +static void BM_SerializeDescriptorProto2(benchmark::State& state) { + size_t bytes = 0; + google::protobuf::FileDescriptorProto proto; + proto.ParseFromArray(descriptor.data, descriptor.size); + for (auto _ : state) { + proto.SerializeToArray(buf, sizeof(buf)); + bytes += descriptor.size; + } + state.SetBytesProcessed(state.iterations() * descriptor.size); +} +BENCHMARK(BM_SerializeDescriptorProto2); + static void BM_SerializeDescriptor(benchmark::State& state) { int64_t total = 0; upb_arena* arena = upb_arena_new();