Refactored proto2 benchmarks and added StringView benchmark.

pull/13171/head
Joshua Haberman 4 years ago
parent 52721eadc7
commit de1b6b0718
  1. 11
      BUILD
  2. 98
      tests/benchmark.cc

11
BUILD

@ -365,12 +365,23 @@ cc_proto_library(
deps = [":benchmark_descriptor_proto"], deps = [":benchmark_descriptor_proto"],
) )
proto_library(
name = "benchmark_descriptor_sv_proto",
srcs = ["tests/descriptor_sv.proto"],
)
cc_proto_library(
name = "benchmark_descriptor_sv_cc_proto",
deps = [":benchmark_descriptor_sv_proto"],
)
cc_binary( cc_binary(
name = "benchmark", name = "benchmark",
testonly = 1, testonly = 1,
srcs = ["tests/benchmark.cc"], srcs = ["tests/benchmark.cc"],
deps = [ deps = [
":benchmark_descriptor_cc_proto", ":benchmark_descriptor_cc_proto",
":benchmark_descriptor_sv_cc_proto",
":benchmark_descriptor_upb_proto", ":benchmark_descriptor_upb_proto",
":benchmark_descriptor_upb_proto_reflection", ":benchmark_descriptor_upb_proto_reflection",
":descriptor_upb_proto", ":descriptor_upb_proto",

@ -6,6 +6,7 @@
#include "tests/descriptor.pb.h" #include "tests/descriptor.pb.h"
#include "tests/descriptor.upb.h" #include "tests/descriptor.upb.h"
#include "tests/descriptor.upbdefs.h" #include "tests/descriptor.upbdefs.h"
#include "tests/descriptor_sv.pb.h"
// For for benchmarks of building descriptors. // For for benchmarks of building descriptors.
#include "google/protobuf/descriptor.upb.h" #include "google/protobuf/descriptor.upb.h"
@ -108,55 +109,58 @@ static void BM_ParseDescriptor_Upb_LargeInitialBlock(benchmark::State& state) {
} }
BENCHMARK(BM_ParseDescriptor_Upb_LargeInitialBlock); BENCHMARK(BM_ParseDescriptor_Upb_LargeInitialBlock);
static void BM_ParseDescriptor_Proto2_NoArena(benchmark::State& state) { template <class P>
size_t bytes = 0; struct NoArena {
for (auto _ : state) { public:
upb_benchmark::FileDescriptorProto proto; P* GetProto() { return &proto_; }
protobuf::StringPiece input(descriptor.data,descriptor.size);
bool ok = proto.ParseFrom<protobuf::MessageLite::kMergePartial>(input); private:
if (!ok) { P proto_;
printf("Failed to parse.\n"); };
exit(1);
} template <class P>
bytes += descriptor.size; struct WithArena {
public:
P* GetProto() { return protobuf::Arena::CreateMessage<P>(&arena_); }
private:
protobuf::Arena arena_;
};
template <class P>
struct WithArenaInitialBlock {
public:
WithArenaInitialBlock() : arena_(GetOptions()) {}
P* GetProto() { return protobuf::Arena::CreateMessage<P>(&arena_); }
private:
protobuf::ArenaOptions GetOptions() {
protobuf::ArenaOptions opts;
opts.initial_block = buf;
opts.initial_block_size = sizeof(buf);
return opts;
} }
state.SetBytesProcessed(state.iterations() * descriptor.size);
}
BENCHMARK(BM_ParseDescriptor_Proto2_NoArena);
static void BM_ParseDescriptor_Proto2_Arena(benchmark::State& state) { protobuf::Arena arena_;
size_t bytes = 0; };
for (auto _ : state) {
protobuf::Arena arena;
protobuf::StringPiece input(descriptor.data,descriptor.size);
auto proto =
protobuf::Arena::CreateMessage<upb_benchmark::FileDescriptorProto>(
&arena);
bool ok = proto->ParseFrom<protobuf::MessageLite::kMergePartial>(input);
if (!ok) { using FileDescriptor = ::upb_benchmark::FileDescriptorProto;
printf("Failed to parse.\n"); using FileDescriptorSV = ::upb_benchmark::sv::FileDescriptorProto;
exit(1);
} const protobuf::MessageLite::ParseFlags kMergePartial =
bytes += descriptor.size; protobuf::MessageLite::ParseFlags::kMergePartial;
} const protobuf::MessageLite::ParseFlags kMergePartialWithAliasing =
state.SetBytesProcessed(state.iterations() * descriptor.size); protobuf::MessageLite::ParseFlags::kMergePartialWithAliasing;
}
BENCHMARK(BM_ParseDescriptor_Proto2_Arena);
static void BM_ParseDescriptor_Proto2_Arena_LargeInitialBlock(benchmark::State& state) { template <class P, template <class> class Factory,
protobuf::MessageLite::ParseFlags kParseFlags = kMergePartial>
void BM_Parse_Proto2(benchmark::State& state) {
size_t bytes = 0; size_t bytes = 0;
protobuf::ArenaOptions opts;
opts.initial_block = buf;
opts.initial_block_size = sizeof(buf);
for (auto _ : state) { for (auto _ : state) {
protobuf::Arena arena(opts); Factory<P> proto_factory;
auto proto = proto_factory.GetProto();
protobuf::StringPiece input(descriptor.data,descriptor.size); protobuf::StringPiece input(descriptor.data,descriptor.size);
auto proto = bool ok = proto->template ParseFrom<kParseFlags>(input);
protobuf::Arena::CreateMessage<upb_benchmark::FileDescriptorProto>(
&arena);
bool ok = proto->ParseFrom<protobuf::MessageLite::kMergePartial>(input);
if (!ok) { if (!ok) {
printf("Failed to parse.\n"); printf("Failed to parse.\n");
exit(1); exit(1);
@ -165,7 +169,15 @@ static void BM_ParseDescriptor_Proto2_Arena_LargeInitialBlock(benchmark::State&
} }
state.SetBytesProcessed(state.iterations() * descriptor.size); state.SetBytesProcessed(state.iterations() * descriptor.size);
} }
BENCHMARK(BM_ParseDescriptor_Proto2_Arena_LargeInitialBlock); BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDescriptor, NoArena);
BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDescriptor, WithArena);
BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDescriptor, WithArenaInitialBlock);
//BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDescriptorSV, NoArena);
//BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDescriptorSV, WithArena);
BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDescriptorSV, WithArenaInitialBlock);
//BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDescriptorSV, NoArena, kMergePartialWithAliasing);
//BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDescriptorSV, WithArena, kMergePartialWithAliasing);
BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDescriptorSV, WithArenaInitialBlock, kMergePartialWithAliasing);
static void BM_SerializeDescriptor_Proto2(benchmark::State& state) { static void BM_SerializeDescriptor_Proto2(benchmark::State& state) {
size_t bytes = 0; size_t bytes = 0;

Loading…
Cancel
Save