Repository navigation
Expand file tree
/
Copy pathbulk_numeric_vectors.cpp
More file actions
244 lines (221 loc) · 8.29 KB
/
Copy pathbulk_numeric_vectors.cpp
File metadata and controls
244 lines (221 loc) · 8.29 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
#include <benchmark/benchmark.h>
#include <cstddef>
#include <cstdint>
#include <rfl/boost_serialization.hpp>
#include <rfl/cereal.hpp>
#include <rfl/yas.hpp>
#include <sstream>
#include <string>
#include <vector>
namespace bulk_numeric_vectors {
struct Point2 {
double x;
double y;
bool operator==(const Point2&) const = default;
};
struct Camera {
std::vector<Point2> keypoints;
std::vector<std::uint64_t> landmark_ids;
bool operator==(const Camera&) const = default;
};
struct World {
std::vector<Camera> cameras;
bool operator==(const World&) const = default;
};
static Point2 make_point(std::size_t index) {
return {
static_cast<double>(static_cast<std::int64_t>(index % 65536) - 32768) *
0.125,
static_cast<double>(static_cast<std::int64_t>((index * 17) % 65536) -
32768) *
0.25};
}
static std::uint64_t make_id(std::size_t index) {
return static_cast<std::uint64_t>(index) * UINT64_C(0x9e3779b97f4a7c15) +
UINT64_C(0x123456789abcdef0);
}
static std::vector<std::uint64_t> make_ids(std::size_t count) {
std::vector<std::uint64_t> values(count);
for (std::size_t i = 0; i < count; ++i) values[i] = make_id(i);
return values;
}
static std::vector<Point2> make_points(std::size_t count) {
std::vector<Point2> values(count);
for (std::size_t i = 0; i < count; ++i) values[i] = make_point(i);
return values;
}
static World make_world(std::size_t cameras, std::size_t points_per_camera) {
World world;
world.cameras.resize(cameras);
for (std::size_t i = 0; i < cameras; ++i) {
auto& camera = world.cameras[i];
camera.keypoints.resize(points_per_camera);
camera.landmark_ids.resize(points_per_camera);
for (std::size_t j = 0; j < points_per_camera; ++j) {
const auto index = i * points_per_camera + j;
camera.keypoints[j] = make_point(index);
camera.landmark_ids[j] = make_id(index);
}
}
return world;
}
enum class Format { cereal, yas, boost_serialization };
template <Format F, class T>
void write(const T& input, std::ostream& stream) {
if constexpr (F == Format::cereal)
rfl::cereal::write(input, stream);
else if constexpr (F == Format::yas)
rfl::yas::write(input, stream);
else
rfl::boost_serialization::write(input, stream);
}
template <Format F, class T>
auto read(const std::string& bytes) {
if constexpr (F == Format::yas) {
return rfl::yas::read<T>(bytes);
} else {
std::istringstream stream(bytes);
if constexpr (F == Format::cereal)
return rfl::cereal::read<T>(stream);
else
return rfl::boost_serialization::read<T>(stream);
}
}
template <bool Read, Format F, class T>
static void run(benchmark::State& state, const T& input, std::int64_t items) {
std::ostringstream output;
write<F>(input, output);
const auto bytes = output.str();
{
const auto result = read<F, T>(bytes);
if (!result) {
state.SkipWithError(result.error().what());
return;
}
if (*result != input) {
state.SkipWithError("Binary round trip changed the input");
return;
}
}
// Stream construction, serialization, and destruction are timed. Fixture
// generation, output extraction, and equality checks are outside the loop.
for (auto _ : state) {
if constexpr (Read) {
auto result = read<F, T>(bytes);
if (!result) {
state.SkipWithError(result.error().what());
break;
}
benchmark::DoNotOptimize(result);
} else {
std::ostringstream stream;
write<F>(input, stream);
benchmark::DoNotOptimize(stream);
}
}
state.SetItemsProcessed(state.iterations() * items);
state.SetBytesProcessed(state.iterations() *
static_cast<std::int64_t>(bytes.size()));
state.counters["serialized_bytes"] = static_cast<double>(bytes.size());
}
template <bool Read, Format F>
static void BM_bulk_numeric_vectors_ids(benchmark::State& state) {
const auto input = make_ids(static_cast<std::size_t>(state.range(0)));
run<Read, F>(state, input, state.range(0));
}
template <bool Read, Format F>
static void BM_bulk_numeric_vectors_points(benchmark::State& state) {
const auto input = make_points(static_cast<std::size_t>(state.range(0)));
run<Read, F>(state, input, state.range(0));
}
template <bool Read, Format F>
static void BM_bulk_numeric_vectors_complete(benchmark::State& state) {
const auto input = make_world(static_cast<std::size_t>(state.range(0)),
static_cast<std::size_t>(state.range(1)));
run<Read, F>(state, input, state.range(0) * state.range(1));
}
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_ids, false, Format::cereal)
->Name("bulk_numeric_vectors/cereal/ids/write")
->Arg(1500000)
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_ids, true, Format::cereal)
->Name("bulk_numeric_vectors/cereal/ids/read")
->Arg(1500000)
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_points, false, Format::cereal)
->Name("bulk_numeric_vectors/cereal/points/write")
->Arg(1500000)
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_points, true, Format::cereal)
->Name("bulk_numeric_vectors/cereal/points/read")
->Arg(1500000)
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_complete, false, Format::cereal)
->Name("bulk_numeric_vectors/cereal/complete/write")
->Args({500, 3000})
->ArgNames({"cameras", "keypoints"})
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_complete, true, Format::cereal)
->Name("bulk_numeric_vectors/cereal/complete/read")
->Args({500, 3000})
->ArgNames({"cameras", "keypoints"})
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_ids, false, Format::yas)
->Name("bulk_numeric_vectors/yas/ids/write")
->Arg(1500000)
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_ids, true, Format::yas)
->Name("bulk_numeric_vectors/yas/ids/read")
->Arg(1500000)
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_points, false, Format::yas)
->Name("bulk_numeric_vectors/yas/points/write")
->Arg(1500000)
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_points, true, Format::yas)
->Name("bulk_numeric_vectors/yas/points/read")
->Arg(1500000)
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_complete, false, Format::yas)
->Name("bulk_numeric_vectors/yas/complete/write")
->Args({500, 3000})
->ArgNames({"cameras", "keypoints"})
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_complete, true, Format::yas)
->Name("bulk_numeric_vectors/yas/complete/read")
->Args({500, 3000})
->ArgNames({"cameras", "keypoints"})
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_ids, false,
Format::boost_serialization)
->Name("bulk_numeric_vectors/boost_serialization/ids/write")
->Arg(1500000)
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_ids, true,
Format::boost_serialization)
->Name("bulk_numeric_vectors/boost_serialization/ids/read")
->Arg(1500000)
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_points, false,
Format::boost_serialization)
->Name("bulk_numeric_vectors/boost_serialization/points/write")
->Arg(1500000)
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_points, true,
Format::boost_serialization)
->Name("bulk_numeric_vectors/boost_serialization/points/read")
->Arg(1500000)
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_complete, false,
Format::boost_serialization)
->Name("bulk_numeric_vectors/boost_serialization/complete/write")
->Args({500, 3000})
->ArgNames({"cameras", "keypoints"})
->Unit(benchmark::kMillisecond);
BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_complete, true,
Format::boost_serialization)
->Name("bulk_numeric_vectors/boost_serialization/complete/read")
->Args({500, 3000})
->ArgNames({"cameras", "keypoints"})
->Unit(benchmark::kMillisecond);
} // namespace bulk_numeric_vectors