1load(
2    "//bazel:upb_proto_library.bzl",
3    "upb_proto_library",
4    "upb_proto_reflection_library",
5)
6load(
7    ":build_defs.bzl",
8    "tmpl_cc_binary",
9    "cc_optimizefor_proto_library",
10    "expand_suffixes",
11    "proto_library",
12)
13
14licenses(["notice"])
15
16proto_library(
17    name = "descriptor_proto",
18    srcs = ["descriptor.proto"],
19)
20
21upb_proto_library(
22    name = "benchmark_descriptor_upb_proto",
23    deps = [":descriptor_proto"],
24)
25
26upb_proto_reflection_library(
27    name = "benchmark_descriptor_upb_proto_reflection",
28    deps = [":descriptor_proto"],
29)
30
31upb_proto_reflection_library(
32    name = "ads_upb_proto_reflection",
33    deps = ["@com_google_googleapis//:ads_proto"],
34)
35
36cc_proto_library(
37    name = "benchmark_descriptor_cc_proto",
38    deps = [":descriptor_proto"],
39)
40
41proto_library(
42    name = "benchmark_descriptor_sv_proto",
43    srcs = ["descriptor_sv.proto"],
44)
45
46cc_proto_library(
47    name = "benchmark_descriptor_sv_cc_proto",
48    deps = [":benchmark_descriptor_sv_proto"],
49)
50
51cc_binary(
52    name = "benchmark",
53    testonly = 1,
54    srcs = ["benchmark.cc"],
55    deps = [
56        ":ads_upb_proto_reflection",
57        ":benchmark_descriptor_cc_proto",
58        ":benchmark_descriptor_sv_cc_proto",
59        ":benchmark_descriptor_upb_proto",
60        ":benchmark_descriptor_upb_proto_reflection",
61        "//:descriptor_upb_proto",
62        "//:reflection",
63        "@com_github_google_benchmark//:benchmark_main",
64        "@com_google_absl//absl/container:flat_hash_set",
65        "@com_google_protobuf//:protobuf",
66    ],
67)
68
69# Size benchmarks.
70
71SIZE_BENCHMARKS = {
72    "empty": "Empty",
73    "descriptor": "FileDescriptorSet",
74    "100_msgs": "Message100",
75    "200_msgs": "Message200",
76    "100_fields": "Message",
77    "200_fields": "Message",
78}
79
80py_binary(
81    name = "gen_synthetic_protos",
82    srcs = ["gen_synthetic_protos.py"],
83    python_version = "PY3",
84)
85
86py_binary(
87    name = "gen_upb_binary_c",
88    srcs = ["gen_upb_binary_c.py"],
89    python_version = "PY3",
90)
91
92py_binary(
93    name = "gen_protobuf_binary_cc",
94    srcs = ["gen_protobuf_binary_cc.py"],
95    python_version = "PY3",
96)
97
98genrule(
99    name = "do_gen_synthetic_protos",
100    tools = [":gen_synthetic_protos"],
101    outs = [
102        "100_msgs.proto",
103        "200_msgs.proto",
104        "100_fields.proto",
105        "200_fields.proto",
106    ],
107    cmd = "$(execpath :gen_synthetic_protos) $(RULEDIR)",
108)
109
110proto_library(
111    name = "100_msgs_proto",
112    srcs = ["100_msgs.proto"],
113)
114
115proto_library(
116    name = "200_msgs_proto",
117    srcs = ["200_msgs.proto"],
118)
119
120proto_library(
121    name = "100_fields_proto",
122    srcs = ["100_fields.proto"],
123)
124
125proto_library(
126    name = "200_fields_proto",
127    srcs = ["200_fields.proto"],
128)
129
130proto_library(
131    name = "empty_proto",
132    srcs = ["empty.proto"],
133)
134
135[(
136upb_proto_library(
137    name = k + "_upb_proto",
138    deps = [":" + k + "_proto"],
139),
140cc_proto_library(
141    name = k + "_cc_proto",
142    deps = [":" + k + "_proto"],
143),
144tmpl_cc_binary(
145    name = k + "_upb_binary",
146    testonly = 1,
147    gen = ":gen_upb_binary_c",
148    args = [
149        package_name() + "/" + k + ".upb.h",
150        "upb_benchmark_" + v,
151    ],
152    deps = [
153        ":" + k + "_upb_proto",
154    ],
155),
156tmpl_cc_binary(
157    name = k + "_protobuf_binary",
158    testonly = 1,
159    gen = ":gen_protobuf_binary_cc",
160    args = [
161        package_name() + "/" + k + ".pb.h",
162        "upb_benchmark::" + v,
163    ],
164    deps = [
165        ":" + k + "_cc_proto",
166    ],
167),
168cc_optimizefor_proto_library(
169    srcs = [k + ".proto"],
170    outs = [k + "_lite.proto"],
171    name = k + "_cc_lite_proto",
172    optimize_for = "LITE_RUNTIME",
173),
174tmpl_cc_binary(
175    name = k + "_lite_protobuf_binary",
176    testonly = 1,
177    gen = ":gen_protobuf_binary_cc",
178    args = [
179        package_name() + "/" + k + "_lite.pb.h",
180        "upb_benchmark::" + v,
181    ],
182    deps = [
183        ":" + k + "_cc_lite_proto",
184    ],
185),
186cc_optimizefor_proto_library(
187    srcs = [k + ".proto"],
188    outs = [k + "_codesize.proto"],
189    name = k + "_cc_codesize_proto",
190    optimize_for = "CODE_SIZE",
191),
192tmpl_cc_binary(
193    name = k + "_codesize_protobuf_binary",
194    testonly = 1,
195    gen = ":gen_protobuf_binary_cc",
196    args = [
197        package_name() + "/" + k + "_codesize.pb.h",
198        "upb_benchmark::" + v,
199    ],
200    deps = [
201        ":" + k + "_cc_codesize_proto",
202    ],
203)
204) for k, v in SIZE_BENCHMARKS.items()]
205
206genrule(
207    testonly = 1,
208    name = "size_data",
209    srcs = expand_suffixes(
210        SIZE_BENCHMARKS.keys(),
211        suffixes = ["_upb_binary", "_protobuf_binary", "_lite_protobuf_binary", "_codesize_protobuf_binary"],
212    ),
213    outs = ["size_data.txt"],
214    # We want --format=GNU which counts rodata with data, not text.
215    cmd = "size $$($$OSTYPE == 'linux-gnu' ? '--format=GNU -d' : '') $(SRCS) > $@",
216    # "size" sometimes isn't available remotely.
217    local = 1,
218)
219