From 4bd34da105b5390489b2df750858dd31cb5d3328 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 4 Nov 2020 20:16:47 -0800 Subject: [PATCH] WIP. --- benchmarks/BUILD | 87 +++++++++++++++---- benchmarks/build_defs.bzl | 44 ++++++++++ benchmarks/empty.proto | 6 ++ ...obuf_binary.cc => protobuf_binary.cc.tmpl} | 4 +- benchmarks/upb_binary.c | 12 --- benchmarks/upb_binary.c.tmpl | 12 +++ 6 files changed, 132 insertions(+), 33 deletions(-) create mode 100644 benchmarks/build_defs.bzl create mode 100644 benchmarks/empty.proto rename benchmarks/{protobuf_binary.cc => protobuf_binary.cc.tmpl} (57%) delete mode 100644 benchmarks/upb_binary.c create mode 100644 benchmarks/upb_binary.c.tmpl diff --git a/benchmarks/BUILD b/benchmarks/BUILD index 449c2b5e7f..b0e598c6c0 100644 --- a/benchmarks/BUILD +++ b/benchmarks/BUILD @@ -3,6 +3,11 @@ load( "upb_proto_library", "upb_proto_reflection_library", ) +load( + ":build_defs.bzl", + "tmpl_cc_binary", + "cc_lite_proto_library", +) licenses(["notice"]) @@ -60,32 +65,76 @@ cc_binary( # Size benchmarks. -upb_proto_library( - name = "empty_upb_proto", - deps = ["@com_google_protobuf//:empty_proto"], +SIZE_BENCHMARKS = { + "empty": "Empty", + "descriptor": "FileDescriptorSet", + "100_msgs": "Message99", +} + +py_binary( + name = "gen_benchmark_proto", + srcs = ["gen_benchmark_proto.py"], ) -cc_proto_library( - name = "empty_cc_proto", - deps = ["@com_google_protobuf//:empty_proto"], +genrule( + name = "gen_100_msgs", + tools = [":gen_benchmark_proto"], + outs = ["100_msgs.proto"], + cmd = "$(execpath :gen_benchmark_proto) $@", ) -cc_binary( - name = "upb_binary", +[( +proto_library( + name = k + "_proto", + srcs = [k + ".proto"], +), +upb_proto_library( + name = k + "_upb_proto", + deps = [":" + k + "_proto"], +), +cc_proto_library( + name = k + "_cc_proto", + deps = [":" + k + "_proto"], +), +tmpl_cc_binary( + name = k + "_upb_binary", testonly = 1, - srcs = ["upb_binary.c"], + srcs = ["upb_binary.c.tmpl"], + replacements = { + "PROTO": "upb_benchmark_" + v, + "INCLUDE": "benchmarks/" + k + ".upb.h", + }, deps = [ - ":empty_upb_proto", + ":" + k + "_upb_proto", ], - #features = ["fully_static_link"], -) - -cc_binary( - name = "protobuf_binary", +), +tmpl_cc_binary( + name = k + "_protobuf_binary", testonly = 1, - srcs = ["protobuf_binary.cc"], + srcs = ["protobuf_binary.cc.tmpl"], + replacements = { + "PROTO": "upb_benchmark::" + v, + "INCLUDE": "benchmarks/" + k + ".pb.h", + }, deps = [ - ":empty_cc_proto", + ":" + k + "_cc_proto", ], - #features = ["fully_static_link"], -) +), +cc_lite_proto_library( + srcs = [k + ".proto"], + outs = [k + "_lite.proto"], + name = k + "_cc_lite_proto", +), +tmpl_cc_binary( + name = k + "_lite_protobuf_binary", + testonly = 1, + srcs = ["protobuf_binary.cc.tmpl"], + replacements = { + "PROTO": "upb_benchmark::" + v, + "INCLUDE": "benchmarks/" + k + "_lite.pb.h", + }, + deps = [ + ":" + k + "_cc_lite_proto", + ], +)) for k, v in SIZE_BENCHMARKS.items()] + diff --git a/benchmarks/build_defs.bzl b/benchmarks/build_defs.bzl new file mode 100644 index 0000000000..402a523db2 --- /dev/null +++ b/benchmarks/build_defs.bzl @@ -0,0 +1,44 @@ + +def tmpl_cc_binary(name, srcs, replacements = [], **kwargs): + if len(srcs) != 1: + fail("Currently srcs must have exactly 1 element") + src = srcs[0] + if not src.endswith(".tmpl"): + fail("srcs of tmpl_cc_binary must end with .tmpl") + outs = [name + "_" + src[:-5]] + sed_cmds = ["s,{},{},g".format(k, v) for k, v in replacements.items()] + cmd = "sed -e '{}' $< > $@".format("; ".join(sed_cmds)) + + native.genrule( + name = name + "_gen_srcs", + srcs = [src], + outs = outs, + cmd = cmd, + ) + + native.cc_binary( + name = name, + srcs = outs, + **kwargs, + ) + +def cc_lite_proto_library(name, srcs, outs): + if len(srcs) != 1: + fail("Currently srcs must have exactly 1 element") + + native.genrule( + name = name + "_gen_proto", + srcs = srcs, + outs = outs, + cmd = "cp $< $@ && chmod a+w $@ && echo 'option optimize_for = LITE_RUNTIME;' >> $@", + ) + + native.proto_library( + name = name + "_proto", + srcs = outs, + ) + + native.cc_proto_library( + name = name, + deps = [":" + name + "_proto"], + ) diff --git a/benchmarks/empty.proto b/benchmarks/empty.proto new file mode 100644 index 0000000000..bcccaf9abe --- /dev/null +++ b/benchmarks/empty.proto @@ -0,0 +1,6 @@ + +syntax = "proto3"; + +package upb_benchmark; + +message Empty {} diff --git a/benchmarks/protobuf_binary.cc b/benchmarks/protobuf_binary.cc.tmpl similarity index 57% rename from benchmarks/protobuf_binary.cc rename to benchmarks/protobuf_binary.cc.tmpl index b585aa6454..139ad7433c 100644 --- a/benchmarks/protobuf_binary.cc +++ b/benchmarks/protobuf_binary.cc.tmpl @@ -1,10 +1,10 @@ -#include "google/protobuf/empty.pb.h" +#include "INCLUDE" char buf[1]; int main() { - google::protobuf::Empty proto; + PROTO proto; proto.ParseFromArray(buf, 1); proto.SerializeToArray(buf, 1); } diff --git a/benchmarks/upb_binary.c b/benchmarks/upb_binary.c deleted file mode 100644 index 8e2d86b904..0000000000 --- a/benchmarks/upb_binary.c +++ /dev/null @@ -1,12 +0,0 @@ - -#include "google/protobuf/empty.upb.h" - -char buf[1]; - -int main() { - upb_arena *arena = upb_arena_new(); - size_t size; - google_protobuf_Empty *proto = google_protobuf_Empty_parse(buf, 1, arena); - google_protobuf_Empty_serialize(proto, arena, &size); - return 0; -} diff --git a/benchmarks/upb_binary.c.tmpl b/benchmarks/upb_binary.c.tmpl new file mode 100644 index 0000000000..6f2904bbd6 --- /dev/null +++ b/benchmarks/upb_binary.c.tmpl @@ -0,0 +1,12 @@ + +#include "INCLUDE" + +char buf[1]; + +int main() { + upb_arena *arena = upb_arena_new(); + size_t size; + PROTO *proto = PROTO_parse(buf, 1, arena); + PROTO_serialize(proto, arena, &size); + return 0; +}