diff --git a/BUILD b/BUILD index 90aa7a7919..a7d106bfc4 100644 --- a/BUILD +++ b/BUILD @@ -357,6 +357,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..15544afe1b 100644 --- a/tests/benchmark.cc +++ b/tests/benchmark.cc @@ -3,8 +3,10 @@ #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; +namespace protobuf = ::google::protobuf; /* A buffer big enough to parse descriptor.proto without going to heap. */ char buf[65535]; @@ -27,7 +29,7 @@ static void BM_ArenaInitialBlockOneAlloc(benchmark::State& state) { } BENCHMARK(BM_ArenaInitialBlockOneAlloc); -static void BM_ParseDescriptorNoHeap(benchmark::State& state) { +static void BM_ParseDescriptor_Upb_LargeInitialBlock(benchmark::State& state) { size_t bytes = 0; for (auto _ : state) { upb_arena* arena = upb_arena_init(buf, sizeof(buf), NULL); @@ -43,9 +45,9 @@ static void BM_ParseDescriptorNoHeap(benchmark::State& state) { } state.SetBytesProcessed(state.iterations() * descriptor.size); } -BENCHMARK(BM_ParseDescriptorNoHeap); +BENCHMARK(BM_ParseDescriptor_Upb_LargeInitialBlock); -static void BM_ParseDescriptor(benchmark::State& state) { +static void BM_ParseDescriptor_Upb(benchmark::State& state) { size_t bytes = 0; for (auto _ : state) { upb_arena* arena = upb_arena_new(); @@ -61,9 +63,78 @@ static void BM_ParseDescriptor(benchmark::State& state) { } state.SetBytesProcessed(state.iterations() * descriptor.size); } -BENCHMARK(BM_ParseDescriptor); +BENCHMARK(BM_ParseDescriptor_Upb); -static void BM_SerializeDescriptor(benchmark::State& state) { +static void BM_ParseDescriptor_Proto2_NoArena(benchmark::State& state) { + size_t bytes = 0; + for (auto _ : state) { + protobuf::FileDescriptorProto proto; + protobuf::StringPiece input(descriptor.data,descriptor.size); + bool ok = proto.ParseFrom(input); + if (!ok) { + printf("Failed to parse.\n"); + exit(1); + } + bytes += descriptor.size; + } + state.SetBytesProcessed(state.iterations() * descriptor.size); +} +BENCHMARK(BM_ParseDescriptor_Proto2_NoArena); + +static void BM_ParseDescriptor_Proto2_Arena(benchmark::State& state) { + size_t bytes = 0; + for (auto _ : state) { + protobuf::Arena arena; + protobuf::StringPiece input(descriptor.data,descriptor.size); + auto proto = protobuf::Arena::CreateMessage( + &arena); + bool ok = proto->ParseFrom(input); + + if (!ok) { + printf("Failed to parse.\n"); + exit(1); + } + bytes += descriptor.size; + } + state.SetBytesProcessed(state.iterations() * descriptor.size); +} +BENCHMARK(BM_ParseDescriptor_Proto2_Arena); + +static void BM_ParseDescriptor_Proto2_Arena_LargeInitialBlock(benchmark::State& state) { + size_t bytes = 0; + protobuf::ArenaOptions opts; + opts.initial_block = buf; + opts.initial_block_size = sizeof(buf); + for (auto _ : state) { + protobuf::Arena arena(opts); + protobuf::StringPiece input(descriptor.data,descriptor.size); + auto proto = protobuf::Arena::CreateMessage( + &arena); + bool ok = proto->ParseFrom(input); + + if (!ok) { + printf("Failed to parse.\n"); + exit(1); + } + bytes += descriptor.size; + } + state.SetBytesProcessed(state.iterations() * descriptor.size); +} +BENCHMARK(BM_ParseDescriptor_Proto2_Arena_LargeInitialBlock); + +static void BM_SerializeDescriptor_Proto2(benchmark::State& state) { + size_t bytes = 0; + protobuf::FileDescriptorProto proto; + proto.ParseFromArray(descriptor.data, descriptor.size); + for (auto _ : state) { + proto.SerializePartialToArray(buf, sizeof(buf)); + bytes += descriptor.size; + } + state.SetBytesProcessed(state.iterations() * descriptor.size); +} +BENCHMARK(BM_SerializeDescriptor_Proto2); + +static void BM_SerializeDescriptor_Upb(benchmark::State& state) { int64_t total = 0; upb_arena* arena = upb_arena_new(); google_protobuf_FileDescriptorProto* set = @@ -85,4 +156,4 @@ static void BM_SerializeDescriptor(benchmark::State& state) { } state.SetBytesProcessed(total); } -BENCHMARK(BM_SerializeDescriptor); +BENCHMARK(BM_SerializeDescriptor_Upb);