|
| 1 | +#ifndef BENCHMARK_PARSER_INLINING_HPP_ |
| 2 | +#define BENCHMARK_PARSER_INLINING_HPP_ |
| 3 | +#include <benchmark/benchmark.h> |
| 4 | + |
| 5 | +#include <cstdint> |
| 6 | +#include <vector> |
| 7 | +namespace parser_inlining { |
| 8 | +struct Record2 { |
| 9 | + float field0 = 0; |
| 10 | + float field1 = 1; |
| 11 | + bool operator==(const Record2&) const = default; |
| 12 | +}; |
| 13 | +struct Record16 { |
| 14 | + float field0 = 0; |
| 15 | + float field1 = 1; |
| 16 | + float field2 = 2; |
| 17 | + float field3 = 3; |
| 18 | + float field4 = 4; |
| 19 | + float field5 = 5; |
| 20 | + float field6 = 6; |
| 21 | + float field7 = 7; |
| 22 | + float field8 = 8; |
| 23 | + float field9 = 9; |
| 24 | + float field10 = 10; |
| 25 | + float field11 = 11; |
| 26 | + float field12 = 12; |
| 27 | + float field13 = 13; |
| 28 | + float field14 = 14; |
| 29 | + float field15 = 15; |
| 30 | + bool operator==(const Record16&) const = default; |
| 31 | +}; |
| 32 | +template <class Record> |
| 33 | +struct Data { |
| 34 | + std::vector<Record> records; |
| 35 | + bool operator==(const Data&) const = default; |
| 36 | +}; |
| 37 | +template <class Record> |
| 38 | +auto make_data(std::size_t size) { |
| 39 | + Data<Record> data; |
| 40 | + data.records.resize(size); |
| 41 | + for (std::size_t i = 0; i < size; ++i) { |
| 42 | + data.records[i].field0 = static_cast<float>(i) * 0.5f; |
| 43 | + data.records[i].field1 = -static_cast<float>(i) * 0.25f; |
| 44 | + } |
| 45 | + return data; |
| 46 | +} |
| 47 | +template <class Record, class Write, class Read> |
| 48 | +void run(benchmark::State& state, Write write, Read read) { |
| 49 | + const auto input = make_data<Record>(state.range(0)); |
| 50 | + const auto bytes = write(input); |
| 51 | + { |
| 52 | + const auto result = read(bytes); |
| 53 | + if (!result) { |
| 54 | + state.SkipWithError(result.error().what()); |
| 55 | + return; |
| 56 | + } |
| 57 | + if (*result != input) { |
| 58 | + state.SkipWithError("Fields changed during round trip"); |
| 59 | + return; |
| 60 | + } |
| 61 | + } |
| 62 | + for (auto _ : state) { |
| 63 | + auto result = read(bytes); |
| 64 | + if (!result) { |
| 65 | + state.SkipWithError(result.error().what()); |
| 66 | + break; |
| 67 | + } |
| 68 | + benchmark::DoNotOptimize(result); |
| 69 | + } |
| 70 | + state.SetItemsProcessed(state.iterations() * state.range(0)); |
| 71 | + state.SetBytesProcessed(state.iterations() * |
| 72 | + static_cast<std::int64_t>(bytes.size())); |
| 73 | + state.counters["serialized_bytes"] = static_cast<double>(bytes.size()); |
| 74 | +} |
| 75 | +} // namespace parser_inlining |
| 76 | +#endif |
0 commit comments