|
| 1 | +#include <benchmark/benchmark.h> |
| 2 | + |
| 3 | +#include <cstddef> |
| 4 | +#include <cstdint> |
| 5 | +#include <rfl/boost_serialization.hpp> |
| 6 | +#include <rfl/cereal.hpp> |
| 7 | +#include <rfl/yas.hpp> |
| 8 | +#include <sstream> |
| 9 | +#include <string> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +namespace bulk_numeric_vectors { |
| 13 | + |
| 14 | +struct Point2 { |
| 15 | + double x; |
| 16 | + double y; |
| 17 | + |
| 18 | + bool operator==(const Point2&) const = default; |
| 19 | +}; |
| 20 | + |
| 21 | +struct Camera { |
| 22 | + std::vector<Point2> keypoints; |
| 23 | + std::vector<std::uint64_t> landmark_ids; |
| 24 | + |
| 25 | + bool operator==(const Camera&) const = default; |
| 26 | +}; |
| 27 | + |
| 28 | +struct World { |
| 29 | + std::vector<Camera> cameras; |
| 30 | + |
| 31 | + bool operator==(const World&) const = default; |
| 32 | +}; |
| 33 | + |
| 34 | +static Point2 make_point(std::size_t index) { |
| 35 | + return { |
| 36 | + static_cast<double>(static_cast<std::int64_t>(index % 65536) - 32768) * |
| 37 | + 0.125, |
| 38 | + static_cast<double>(static_cast<std::int64_t>((index * 17) % 65536) - |
| 39 | + 32768) * |
| 40 | + 0.25}; |
| 41 | +} |
| 42 | + |
| 43 | +static std::uint64_t make_id(std::size_t index) { |
| 44 | + return static_cast<std::uint64_t>(index) * UINT64_C(0x9e3779b97f4a7c15) + |
| 45 | + UINT64_C(0x123456789abcdef0); |
| 46 | +} |
| 47 | + |
| 48 | +static std::vector<std::uint64_t> make_ids(std::size_t count) { |
| 49 | + std::vector<std::uint64_t> values(count); |
| 50 | + for (std::size_t i = 0; i < count; ++i) values[i] = make_id(i); |
| 51 | + return values; |
| 52 | +} |
| 53 | + |
| 54 | +static std::vector<Point2> make_points(std::size_t count) { |
| 55 | + std::vector<Point2> values(count); |
| 56 | + for (std::size_t i = 0; i < count; ++i) values[i] = make_point(i); |
| 57 | + return values; |
| 58 | +} |
| 59 | + |
| 60 | +static World make_world(std::size_t cameras, std::size_t points_per_camera) { |
| 61 | + World world; |
| 62 | + world.cameras.resize(cameras); |
| 63 | + for (std::size_t i = 0; i < cameras; ++i) { |
| 64 | + auto& camera = world.cameras[i]; |
| 65 | + camera.keypoints.resize(points_per_camera); |
| 66 | + camera.landmark_ids.resize(points_per_camera); |
| 67 | + for (std::size_t j = 0; j < points_per_camera; ++j) { |
| 68 | + const auto index = i * points_per_camera + j; |
| 69 | + camera.keypoints[j] = make_point(index); |
| 70 | + camera.landmark_ids[j] = make_id(index); |
| 71 | + } |
| 72 | + } |
| 73 | + return world; |
| 74 | +} |
| 75 | + |
| 76 | +enum class Format { cereal, yas, boost_serialization }; |
| 77 | + |
| 78 | +template <Format F, class T> |
| 79 | +void write(const T& input, std::ostream& stream) { |
| 80 | + if constexpr (F == Format::cereal) |
| 81 | + rfl::cereal::write(input, stream); |
| 82 | + else if constexpr (F == Format::yas) |
| 83 | + rfl::yas::write(input, stream); |
| 84 | + else |
| 85 | + rfl::boost_serialization::write(input, stream); |
| 86 | +} |
| 87 | + |
| 88 | +template <Format F, class T> |
| 89 | +auto read(const std::string& bytes) { |
| 90 | + if constexpr (F == Format::yas) { |
| 91 | + return rfl::yas::read<T>(bytes); |
| 92 | + } else { |
| 93 | + std::istringstream stream(bytes); |
| 94 | + if constexpr (F == Format::cereal) |
| 95 | + return rfl::cereal::read<T>(stream); |
| 96 | + else |
| 97 | + return rfl::boost_serialization::read<T>(stream); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +template <bool Read, Format F, class T> |
| 102 | +static void run(benchmark::State& state, const T& input, std::int64_t items) { |
| 103 | + std::ostringstream output; |
| 104 | + write<F>(input, output); |
| 105 | + const auto bytes = output.str(); |
| 106 | + { |
| 107 | + const auto result = read<F, T>(bytes); |
| 108 | + if (!result) { |
| 109 | + state.SkipWithError(result.error().what()); |
| 110 | + return; |
| 111 | + } |
| 112 | + if (*result != input) { |
| 113 | + state.SkipWithError("Binary round trip changed the input"); |
| 114 | + return; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // Stream construction, serialization, and destruction are timed. Fixture |
| 119 | + // generation, output extraction, and equality checks are outside the loop. |
| 120 | + for (auto _ : state) { |
| 121 | + if constexpr (Read) { |
| 122 | + auto result = read<F, T>(bytes); |
| 123 | + if (!result) { |
| 124 | + state.SkipWithError(result.error().what()); |
| 125 | + break; |
| 126 | + } |
| 127 | + benchmark::DoNotOptimize(result); |
| 128 | + } else { |
| 129 | + std::ostringstream stream; |
| 130 | + write<F>(input, stream); |
| 131 | + benchmark::DoNotOptimize(stream); |
| 132 | + } |
| 133 | + } |
| 134 | + state.SetItemsProcessed(state.iterations() * items); |
| 135 | + state.SetBytesProcessed(state.iterations() * |
| 136 | + static_cast<std::int64_t>(bytes.size())); |
| 137 | + state.counters["serialized_bytes"] = static_cast<double>(bytes.size()); |
| 138 | +} |
| 139 | + |
| 140 | +template <bool Read, Format F> |
| 141 | +static void BM_bulk_numeric_vectors_ids(benchmark::State& state) { |
| 142 | + const auto input = make_ids(static_cast<std::size_t>(state.range(0))); |
| 143 | + run<Read, F>(state, input, state.range(0)); |
| 144 | +} |
| 145 | + |
| 146 | +template <bool Read, Format F> |
| 147 | +static void BM_bulk_numeric_vectors_points(benchmark::State& state) { |
| 148 | + const auto input = make_points(static_cast<std::size_t>(state.range(0))); |
| 149 | + run<Read, F>(state, input, state.range(0)); |
| 150 | +} |
| 151 | + |
| 152 | +template <bool Read, Format F> |
| 153 | +static void BM_bulk_numeric_vectors_complete(benchmark::State& state) { |
| 154 | + const auto input = make_world(static_cast<std::size_t>(state.range(0)), |
| 155 | + static_cast<std::size_t>(state.range(1))); |
| 156 | + run<Read, F>(state, input, state.range(0) * state.range(1)); |
| 157 | +} |
| 158 | + |
| 159 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_ids, false, Format::cereal) |
| 160 | + ->Name("bulk_numeric_vectors/cereal/ids/write") |
| 161 | + ->Arg(1500000) |
| 162 | + ->Unit(benchmark::kMillisecond); |
| 163 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_ids, true, Format::cereal) |
| 164 | + ->Name("bulk_numeric_vectors/cereal/ids/read") |
| 165 | + ->Arg(1500000) |
| 166 | + ->Unit(benchmark::kMillisecond); |
| 167 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_points, false, Format::cereal) |
| 168 | + ->Name("bulk_numeric_vectors/cereal/points/write") |
| 169 | + ->Arg(1500000) |
| 170 | + ->Unit(benchmark::kMillisecond); |
| 171 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_points, true, Format::cereal) |
| 172 | + ->Name("bulk_numeric_vectors/cereal/points/read") |
| 173 | + ->Arg(1500000) |
| 174 | + ->Unit(benchmark::kMillisecond); |
| 175 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_complete, false, Format::cereal) |
| 176 | + ->Name("bulk_numeric_vectors/cereal/complete/write") |
| 177 | + ->Args({500, 3000}) |
| 178 | + ->ArgNames({"cameras", "keypoints"}) |
| 179 | + ->Unit(benchmark::kMillisecond); |
| 180 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_complete, true, Format::cereal) |
| 181 | + ->Name("bulk_numeric_vectors/cereal/complete/read") |
| 182 | + ->Args({500, 3000}) |
| 183 | + ->ArgNames({"cameras", "keypoints"}) |
| 184 | + ->Unit(benchmark::kMillisecond); |
| 185 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_ids, false, Format::yas) |
| 186 | + ->Name("bulk_numeric_vectors/yas/ids/write") |
| 187 | + ->Arg(1500000) |
| 188 | + ->Unit(benchmark::kMillisecond); |
| 189 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_ids, true, Format::yas) |
| 190 | + ->Name("bulk_numeric_vectors/yas/ids/read") |
| 191 | + ->Arg(1500000) |
| 192 | + ->Unit(benchmark::kMillisecond); |
| 193 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_points, false, Format::yas) |
| 194 | + ->Name("bulk_numeric_vectors/yas/points/write") |
| 195 | + ->Arg(1500000) |
| 196 | + ->Unit(benchmark::kMillisecond); |
| 197 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_points, true, Format::yas) |
| 198 | + ->Name("bulk_numeric_vectors/yas/points/read") |
| 199 | + ->Arg(1500000) |
| 200 | + ->Unit(benchmark::kMillisecond); |
| 201 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_complete, false, Format::yas) |
| 202 | + ->Name("bulk_numeric_vectors/yas/complete/write") |
| 203 | + ->Args({500, 3000}) |
| 204 | + ->ArgNames({"cameras", "keypoints"}) |
| 205 | + ->Unit(benchmark::kMillisecond); |
| 206 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_complete, true, Format::yas) |
| 207 | + ->Name("bulk_numeric_vectors/yas/complete/read") |
| 208 | + ->Args({500, 3000}) |
| 209 | + ->ArgNames({"cameras", "keypoints"}) |
| 210 | + ->Unit(benchmark::kMillisecond); |
| 211 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_ids, false, |
| 212 | + Format::boost_serialization) |
| 213 | + ->Name("bulk_numeric_vectors/boost_serialization/ids/write") |
| 214 | + ->Arg(1500000) |
| 215 | + ->Unit(benchmark::kMillisecond); |
| 216 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_ids, true, |
| 217 | + Format::boost_serialization) |
| 218 | + ->Name("bulk_numeric_vectors/boost_serialization/ids/read") |
| 219 | + ->Arg(1500000) |
| 220 | + ->Unit(benchmark::kMillisecond); |
| 221 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_points, false, |
| 222 | + Format::boost_serialization) |
| 223 | + ->Name("bulk_numeric_vectors/boost_serialization/points/write") |
| 224 | + ->Arg(1500000) |
| 225 | + ->Unit(benchmark::kMillisecond); |
| 226 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_points, true, |
| 227 | + Format::boost_serialization) |
| 228 | + ->Name("bulk_numeric_vectors/boost_serialization/points/read") |
| 229 | + ->Arg(1500000) |
| 230 | + ->Unit(benchmark::kMillisecond); |
| 231 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_complete, false, |
| 232 | + Format::boost_serialization) |
| 233 | + ->Name("bulk_numeric_vectors/boost_serialization/complete/write") |
| 234 | + ->Args({500, 3000}) |
| 235 | + ->ArgNames({"cameras", "keypoints"}) |
| 236 | + ->Unit(benchmark::kMillisecond); |
| 237 | +BENCHMARK_TEMPLATE(BM_bulk_numeric_vectors_complete, true, |
| 238 | + Format::boost_serialization) |
| 239 | + ->Name("bulk_numeric_vectors/boost_serialization/complete/read") |
| 240 | + ->Args({500, 3000}) |
| 241 | + ->ArgNames({"cameras", "keypoints"}) |
| 242 | + ->Unit(benchmark::kMillisecond); |
| 243 | + |
| 244 | +} // namespace bulk_numeric_vectors |
0 commit comments