Repository navigation
Expand file tree
/
Copy pathvectors_read.cpp
More file actions
265 lines (242 loc) · 8.35 KB
/
Copy pathvectors_read.cpp
File metadata and controls
265 lines (242 loc) · 8.35 KB
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
#include <benchmark/benchmark.h>
#include <cstddef>
#include <cstdint>
#include <rfl/avro.hpp>
#include <rfl/boost_serialization.hpp>
#include <rfl/bson.hpp>
#include <rfl/capnproto.hpp>
#include <rfl/cbor.hpp>
#include <rfl/cereal.hpp>
#include <rfl/flexbuf.hpp>
#include <rfl/json.hpp>
#include <rfl/msgpack.hpp>
#include <rfl/toml.hpp>
#include <rfl/ubjson.hpp>
#include <rfl/xml.hpp>
#include <rfl/yaml.hpp>
#include <rfl/yas.hpp>
#include <utility>
#include <vector>
namespace vectors_read {
struct FloatVector {
std::vector<float> values;
bool operator==(const FloatVector&) const = default;
};
struct Data {
std::vector<FloatVector> vectors;
};
static Data make_data(std::size_t count, std::size_t values_per_vector) {
Data data;
data.vectors.reserve(count);
for (std::size_t i = 0; i < count; ++i) {
FloatVector vector;
vector.values.reserve(values_per_vector);
for (std::size_t j = 0; j < values_per_vector; ++j) {
const auto index = i * values_per_vector + j;
const auto value = static_cast<std::int32_t>(index % 65536) - 32768;
vector.values.push_back(static_cast<float>(value) * 0.125f);
}
data.vectors.push_back(std::move(vector));
}
return data;
}
template <class Write, class Read>
static void run(benchmark::State& state, Write write, Read read) {
// Compare one large vector with many short vectors. The object wrappers
// allow the same workload to be used with BSON, TOML and XML as well.
const auto input = make_data(static_cast<std::size_t>(state.range(0)),
static_cast<std::size_t>(state.range(1)));
const auto data = write(input);
{
const auto result = read(data);
if (!result) {
state.SkipWithError(result.error().what());
return;
}
if (result->vectors != input.vectors) {
state.SkipWithError("Vector values changed during round trip");
return;
}
}
for (auto _ : state) {
auto result = read(data);
if (!result) {
state.SkipWithError(result.error().what());
break;
}
benchmark::DoNotOptimize(result);
}
state.SetItemsProcessed(state.iterations() * state.range(0) * state.range(1));
state.SetBytesProcessed(state.iterations() *
static_cast<std::int64_t>(data.size()));
state.counters["serialized_bytes"] = static_cast<double>(data.size());
}
static void BM_vectors_read_reflect_cpp_avro(benchmark::State& state) {
const auto schema = rfl::avro::to_schema<Data>();
run(
state,
[&schema](const Data& value) { return rfl::avro::write(value, schema); },
[&schema](const auto& data) {
return rfl::avro::read<Data>(data, schema);
});
}
BENCHMARK(BM_vectors_read_reflect_cpp_avro)
->Args({1, 1000})
->Args({1, 1000000})
->Args({10000, 64})
->ArgNames({"vectors", "values"})
->Unit(benchmark::kMillisecond);
static void BM_vectors_read_reflect_cpp_boost_serialization(
benchmark::State& state) {
run(
state,
[](const Data& value) { return rfl::boost_serialization::write(value); },
[](const auto& data) {
return rfl::boost_serialization::read<Data>(data);
});
}
BENCHMARK(BM_vectors_read_reflect_cpp_boost_serialization)
->Args({1, 1000})
->Args({1, 1000000})
->Args({10000, 64})
->ArgNames({"vectors", "values"})
->Unit(benchmark::kMillisecond);
static void BM_vectors_read_reflect_cpp_bson(benchmark::State& state) {
run(
state, [](const Data& value) { return rfl::bson::write(value); },
[](const auto& data) { return rfl::bson::read<Data>(data); });
}
BENCHMARK(BM_vectors_read_reflect_cpp_bson)
->Args({1, 1000})
->Args({1, 1000000})
->Args({10000, 64})
->ArgNames({"vectors", "values"})
->Unit(benchmark::kMillisecond);
static void BM_vectors_read_reflect_cpp_capnproto(benchmark::State& state) {
const auto schema = rfl::capnproto::to_schema<Data>();
run(
state,
[&schema](const Data& value) {
return rfl::capnproto::write(value, schema);
},
[&schema](const auto& data) {
return rfl::capnproto::read<Data>(data, schema);
});
}
BENCHMARK(BM_vectors_read_reflect_cpp_capnproto)
->Args({1, 1000})
->Args({1, 1000000})
->Args({10000, 64})
->ArgNames({"vectors", "values"})
->Unit(benchmark::kMillisecond);
static void BM_vectors_read_reflect_cpp_cbor(benchmark::State& state) {
run(
state, [](const Data& value) { return rfl::cbor::write(value); },
[](const auto& data) { return rfl::cbor::read<Data>(data); });
}
BENCHMARK(BM_vectors_read_reflect_cpp_cbor)
->Args({1, 1000})
->Args({1, 1000000})
->Args({10000, 64})
->ArgNames({"vectors", "values"})
->Unit(benchmark::kMillisecond);
static void BM_vectors_read_reflect_cpp_cereal(benchmark::State& state) {
run(
state, [](const Data& value) { return rfl::cereal::write(value); },
[](const auto& data) { return rfl::cereal::read<Data>(data); });
}
BENCHMARK(BM_vectors_read_reflect_cpp_cereal)
->Args({1, 1000})
->Args({1, 1000000})
->Args({10000, 64})
->ArgNames({"vectors", "values"})
->Unit(benchmark::kMillisecond);
static void BM_vectors_read_reflect_cpp_flexbuf(benchmark::State& state) {
run(
state, [](const Data& value) { return rfl::flexbuf::write(value); },
[](const auto& data) { return rfl::flexbuf::read<Data>(data); });
}
BENCHMARK(BM_vectors_read_reflect_cpp_flexbuf)
->Args({1, 1000})
->Args({1, 1000000})
->Args({10000, 64})
->ArgNames({"vectors", "values"})
->Unit(benchmark::kMillisecond);
static void BM_vectors_read_reflect_cpp_json(benchmark::State& state) {
run(
state, [](const Data& value) { return rfl::json::write(value); },
[](const auto& data) { return rfl::json::read<Data>(data); });
}
BENCHMARK(BM_vectors_read_reflect_cpp_json)
->Args({1, 1000})
->Args({1, 1000000})
->Args({10000, 64})
->ArgNames({"vectors", "values"})
->Unit(benchmark::kMillisecond);
static void BM_vectors_read_reflect_cpp_msgpack(benchmark::State& state) {
run(
state, [](const Data& value) { return rfl::msgpack::write(value); },
[](const auto& data) { return rfl::msgpack::read<Data>(data); });
}
BENCHMARK(BM_vectors_read_reflect_cpp_msgpack)
->Args({1, 1000})
->Args({1, 1000000})
->Args({10000, 64})
->ArgNames({"vectors", "values"})
->Unit(benchmark::kMillisecond);
static void BM_vectors_read_reflect_cpp_toml(benchmark::State& state) {
run(
state, [](const Data& value) { return rfl::toml::write(value); },
[](const auto& data) { return rfl::toml::read<Data>(data); });
}
BENCHMARK(BM_vectors_read_reflect_cpp_toml)
->Args({1, 1000})
->Args({1, 1000000})
->Args({10000, 64})
->ArgNames({"vectors", "values"})
->Unit(benchmark::kMillisecond);
static void BM_vectors_read_reflect_cpp_ubjson(benchmark::State& state) {
run(
state, [](const Data& value) { return rfl::ubjson::write(value); },
[](const auto& data) { return rfl::ubjson::read<Data>(data); });
}
BENCHMARK(BM_vectors_read_reflect_cpp_ubjson)
->Args({1, 1000})
->Args({1, 1000000})
->Args({10000, 64})
->ArgNames({"vectors", "values"})
->Unit(benchmark::kMillisecond);
static void BM_vectors_read_reflect_cpp_xml(benchmark::State& state) {
run(
state, [](const Data& value) { return rfl::xml::write(value); },
[](const auto& data) { return rfl::xml::read<Data>(data); });
}
BENCHMARK(BM_vectors_read_reflect_cpp_xml)
->Args({1, 1000})
->Args({1, 1000000})
->Args({10000, 64})
->ArgNames({"vectors", "values"})
->Unit(benchmark::kMillisecond);
static void BM_vectors_read_reflect_cpp_yaml(benchmark::State& state) {
run(
state, [](const Data& value) { return rfl::yaml::write(value); },
[](const auto& data) { return rfl::yaml::read<Data>(data); });
}
BENCHMARK(BM_vectors_read_reflect_cpp_yaml)
->Args({1, 1000})
->Args({1, 1000000})
->Args({10000, 64})
->ArgNames({"vectors", "values"})
->Unit(benchmark::kMillisecond);
static void BM_vectors_read_reflect_cpp_yas(benchmark::State& state) {
run(
state, [](const Data& value) { return rfl::yas::write(value); },
[](const auto& data) { return rfl::yas::read<Data>(data); });
}
BENCHMARK(BM_vectors_read_reflect_cpp_yas)
->Args({1, 1000})
->Args({1, 1000000})
->Args({10000, 64})
->ArgNames({"vectors", "values"})
->Unit(benchmark::kMillisecond);
} // namespace vectors_read