-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.cpp
297 lines (266 loc) · 10.9 KB
/
benchmark.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "benchmark_messages_proto2.pb.h"
#include "non_owning/benchmark_messages_proto2.pb.hpp"
#include "owning/benchmark_messages_proto2.pb.hpp"
#include <benchmark/benchmark.h>
#include <fstream>
#include <memory_resource>
#include <random>
#include "benchmark_messages_proto3.pb.h"
#include "non_owning/benchmark_messages_proto3.pb.hpp"
#include "owning/benchmark_messages_proto3.pb.hpp"
#include "non_owning/packed_repeated_message.pb.hpp"
#include "owning/packed_repeated_message.pb.hpp"
#include "packed_repeated_message.pb.h"
// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
inline void set_message_google(auto &message) {
message.set_field1("");
message.set_field2(8);
message.set_field3(2066379);
message.set_field4("3K+6)#");
message.set_field9(R"(10)2uiSuoXL1^)v}icF@>P(j<t#~tz\lg??S&(<hr7EVs'l{'5`Gohc_(=t eS s{_I?iCwaG]L'*Pu5(&w_:4{~Z)");
message.set_field12(true);
message.set_field14(true);
auto submsg = message.mutable_field15();
submsg->set_field1(25);
submsg->set_field2(36);
submsg->set_field15("\"?6PY4]L2c<}~2;\\TVF_w^[@YfbIc*v/N+Z-oYuaWZr4C;5ib|*s@RCBbuvrQ3g(k,N");
submsg->set_field21(2813090458170031956);
submsg->set_field22(38);
submsg->set_field23(true);
message.set_field18("{=Qwfe~#n{");
message.set_field67(1591432);
message.set_field100(31);
}
inline void set_message_hpp(auto &message) {
message.field1 = "";
message.field2 = 8;
message.field3 = 2066379;
message.field4 = "3K+6)#";
message.field9 = R"(10)2uiSuoXL1^)v}icF@>P(j<t#~tz\lg??S&(<hr7EVs'l{'5`Gohc_(=t eS s{_I?iCwaG]L'*Pu5(&w_:4{~Z)";
message.field12 = true;
message.field14 = true;
auto &submsg = message.field15.emplace();
submsg.field1 = 25;
submsg.field2 = 36;
submsg.field15 = "\"?6PY4]L2c<}~2;\\TVF_w^[@YfbIc*v/N+Z-oYuaWZr4C;5ib|*s@RCBbuvrQ3g(k,N";
submsg.field21 = 2813090458170031956;
submsg.field22 = 38;
submsg.field23 = true;
message.field18 = "{=Qwfe~#n{";
message.field67 = 1591432;
message.field100 = 31;
}
std::span<char> get_GoogleMessage1_data() {
static std::span<char> data;
if (data.empty()) {
benchmarks::proto2::GoogleMessage1 msg;
set_message_google(msg);
static std::string storage = msg.SerializeAsString();
data = std::span{storage};
}
return data;
}
std::span<char> get_data(benchmarks::proto2::GoogleMessage1 * /*unused*/) { return get_GoogleMessage1_data(); }
std::span<char> get_data(benchmarks::proto3::GoogleMessage1 * /*unused*/) { return get_GoogleMessage1_data(); }
std::span<char> get_data(owning::benchmarks::proto2::GoogleMessage1 * /*unused*/) { return get_GoogleMessage1_data(); }
std::span<char> get_data(owning::benchmarks::proto3::GoogleMessage1 * /*unused*/) { return get_GoogleMessage1_data(); }
std::span<char> get_data(non_owning::benchmarks::proto2::GoogleMessage1 * /*unused*/) {
return get_GoogleMessage1_data();
}
std::span<char> get_data(non_owning::benchmarks::proto3::GoogleMessage1 * /*unused*/) {
return get_GoogleMessage1_data();
}
std::span<char> get_packed_repeated_data() {
const int len = 1000;
const std::size_t max_value = 0x0100000000000000ULL;
static std::vector<char> data;
if (data.empty()) {
std::random_device rd;
std::mt19937 engine(rd()); // NOLINT(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
std::uniform_int_distribution<unsigned long long> dis(0, max_value);
repeated_packed::TestMessage msg;
auto *x = msg.mutable_x();
x->Resize(len, 0);
for (int i = 0; i < len; ++i) {
x->Set(i, dis(engine));
}
data.resize(msg.ByteSizeLong());
// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast)
msg.SerializeWithCachedSizesToArray(reinterpret_cast<uint8_t *>(data.data()));
// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
}
return data;
}
std::span<char> get_data(repeated_packed::TestMessage * /*unused*/) { return get_packed_repeated_data(); }
std::span<char> get_data(owning::repeated_packed::TestMessage * /*unused*/) { return get_packed_repeated_data(); }
std::span<char> get_data(non_owning::repeated_packed::TestMessage * /*unused*/) { return get_packed_repeated_data(); }
template <typename Message>
void google_deserialize_regular(benchmark::State &state) {
auto data = get_data((Message *)nullptr);
size_t total = 0;
for (auto _ : state) {
Message message;
auto r = message.ParseFromArray(data.data(), static_cast<int>(data.size()));
total += data.size();
benchmark::DoNotOptimize(r);
}
state.SetBytesProcessed(static_cast<int64_t>(total));
}
BENCHMARK(google_deserialize_regular<benchmarks::proto2::GoogleMessage1>);
BENCHMARK(google_deserialize_regular<benchmarks::proto3::GoogleMessage1>);
template <typename Message>
void google_deserialize_arena(benchmark::State &state) {
auto data = get_data((Message *)nullptr);
google::protobuf::Arena arena;
size_t total = 0;
for (auto _ : state) {
arena.Reset();
auto *message = google::protobuf::Arena::Create<Message>(&arena);
auto r = message->ParseFromArray(data.data(), static_cast<int>(data.size()));
total += data.size();
benchmark::DoNotOptimize(r);
}
state.SetBytesProcessed(static_cast<int64_t>(total));
}
BENCHMARK(google_deserialize_arena<benchmarks::proto2::GoogleMessage1>);
BENCHMARK(google_deserialize_arena<benchmarks::proto3::GoogleMessage1>);
template <typename Message>
void hpp_proto_deserialize_regular(benchmark::State &state) {
auto data = get_data((Message *)nullptr);
size_t total = 0;
for (auto _ : state) {
Message message;
auto r = hpp::proto::read_proto(message, data);
total += data.size();
benchmark::DoNotOptimize(r);
}
state.SetBytesProcessed(static_cast<int64_t>(total));
}
BENCHMARK(hpp_proto_deserialize_regular<owning::benchmarks::proto2::GoogleMessage1>);
BENCHMARK(hpp_proto_deserialize_regular<owning::benchmarks::proto3::GoogleMessage1>);
template <typename Message>
void hpp_proto_deserialize_nonowning(benchmark::State &state) {
auto data = get_data((Message *)nullptr);
std::vector<char> buf(16 * 1024ULL);
size_t total = 0;
for (auto _ : state) {
std::pmr::monotonic_buffer_resource memory_resource(buf.data(), buf.size());
Message message;
auto r = hpp::proto::read_proto(message, data, hpp::proto::strictly_alloc_from{memory_resource});
total += data.size();
benchmark::DoNotOptimize(r);
}
state.SetBytesProcessed(static_cast<int64_t>(total));
}
BENCHMARK(hpp_proto_deserialize_nonowning<non_owning::benchmarks::proto2::GoogleMessage1>);
BENCHMARK(hpp_proto_deserialize_nonowning<non_owning::benchmarks::proto3::GoogleMessage1>);
template <typename Message>
void google_set_message_regular(benchmark::State &state) {
for (auto _ : state) {
Message message;
set_message_google(message);
benchmark::DoNotOptimize(message);
}
}
BENCHMARK(google_set_message_regular<benchmarks::proto2::GoogleMessage1>);
BENCHMARK(google_set_message_regular<benchmarks::proto3::GoogleMessage1>);
template <typename Message>
void google_set_message_arena(benchmark::State &state) {
google::protobuf::Arena arena;
for (auto _ : state) {
arena.Reset();
auto *message = google::protobuf::Arena::Create<Message>(&arena);
set_message_google(*message);
benchmark::DoNotOptimize(message);
}
}
BENCHMARK(google_set_message_arena<benchmarks::proto2::GoogleMessage1>);
BENCHMARK(google_set_message_arena<benchmarks::proto3::GoogleMessage1>);
template <typename Message>
void hpp_proto_set_message_regular(benchmark::State &state) {
for (auto _ : state) {
Message message;
set_message_hpp(message);
benchmark::DoNotOptimize(message);
}
}
BENCHMARK(hpp_proto_set_message_regular<owning::benchmarks::proto2::GoogleMessage1>);
BENCHMARK(hpp_proto_set_message_regular<owning::benchmarks::proto3::GoogleMessage1>);
template <typename Message>
void google_set_message_and_serialize_regular(benchmark::State &state) {
size_t total = 0;
for (auto _ : state) {
Message message;
set_message_google(message);
std::string buffer;
auto r = message.SerializeToString(&buffer);
total += buffer.size();
benchmark::DoNotOptimize(r);
}
state.SetBytesProcessed(static_cast<int64_t>(total));
}
BENCHMARK(google_set_message_and_serialize_regular<benchmarks::proto2::GoogleMessage1>);
BENCHMARK(google_set_message_and_serialize_regular<benchmarks::proto3::GoogleMessage1>);
template <typename Message>
void google_set_message_and_serialize_arena(benchmark::State &state) {
google::protobuf::Arena arena;
size_t total = 0;
for (auto _ : state) {
arena.Reset();
auto *message = google::protobuf::Arena::Create<Message>(&arena);
set_message_google(*message);
std::string buffer;
auto r = message->SerializeToString(&buffer);
total += buffer.size();
benchmark::DoNotOptimize(r);
}
state.SetBytesProcessed(static_cast<int64_t>(total));
}
BENCHMARK(google_set_message_and_serialize_arena<benchmarks::proto2::GoogleMessage1>);
BENCHMARK(google_set_message_and_serialize_arena<benchmarks::proto3::GoogleMessage1>);
template <typename Message>
void hpp_proto_set_message_and_serialize_regular(benchmark::State &state) {
size_t total = 0;
for (auto _ : state) {
Message message;
set_message_hpp(message);
std::vector<char> buffer;
auto r = hpp::proto::write_proto(message, buffer);
total += buffer.size();
benchmark::DoNotOptimize(r);
}
state.SetBytesProcessed(static_cast<int64_t>(total));
}
BENCHMARK(hpp_proto_set_message_and_serialize_regular<owning::benchmarks::proto2::GoogleMessage1>);
BENCHMARK(hpp_proto_set_message_and_serialize_regular<owning::benchmarks::proto3::GoogleMessage1>);
template <typename Message>
void hpp_proto_set_message_and_serialize_nonowning(benchmark::State &state) {
size_t total = 0;
for (auto _ : state) {
Message message;
set_message_hpp(message);
std::vector<char> buffer;
auto r = hpp::proto::write_proto(message, buffer);
total += buffer.size();
benchmark::DoNotOptimize(r);
}
state.SetBytesProcessed(static_cast<int64_t>(total));
}
BENCHMARK(hpp_proto_set_message_and_serialize_nonowning<non_owning::benchmarks::proto2::GoogleMessage1>);
BENCHMARK(hpp_proto_set_message_and_serialize_nonowning<non_owning::benchmarks::proto3::GoogleMessage1>);
template <typename Message>
void hpp_proto_set_message_nonowning(benchmark::State &state) {
for (auto _ : state) {
Message message;
set_message_hpp(message);
benchmark::DoNotOptimize(message);
}
}
BENCHMARK(hpp_proto_set_message_nonowning<non_owning::benchmarks::proto2::GoogleMessage1>);
BENCHMARK(hpp_proto_set_message_nonowning<non_owning::benchmarks::proto3::GoogleMessage1>);
BENCHMARK(google_deserialize_regular<repeated_packed::TestMessage>);
BENCHMARK(google_deserialize_arena<repeated_packed::TestMessage>);
BENCHMARK(hpp_proto_deserialize_regular<owning::repeated_packed::TestMessage>);
BENCHMARK(hpp_proto_deserialize_nonowning<non_owning::repeated_packed::TestMessage>);
BENCHMARK_MAIN();
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)