File: canada_read.cpp

package info (click to toggle)
reflect-cpp 0.21.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,128 kB
  • sloc: cpp: 50,336; python: 139; makefile: 30; sh: 3
file content (232 lines) | stat: -rw-r--r-- 6,995 bytes parent folder | download | duplicates (2)
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
#include <benchmark/benchmark.h>

#include <array>
#include <iostream>
#include <optional>
#include <rfl/avro.hpp>
#include <rfl/bson.hpp>
#include <rfl/capnproto.hpp>
#include <rfl/cbor.hpp>
#include <rfl/flexbuf.hpp>
#include <rfl/json.hpp>
#include <rfl/msgpack.hpp>
#include <rfl/toml.hpp>
#include <rfl/ubjson.hpp>
#include <rfl/yaml.hpp>
#include <type_traits>
#include <vector>

namespace canada_read {

// ----------------------------------------------------------------------------

struct Property {
  std::string name;
};

struct Geometry {
  rfl::Literal<"Polygon"> type;
  std::vector<std::vector<std::tuple<double, double>>> coordinates;
};

struct Feature {
  rfl::Literal<"Feature"> type;
  Property properties;
  Geometry geometry;
};

struct FeatureCollection {
  rfl::Literal<"FeatureCollection"> type;
  std::vector<Feature> features;
};

// ----------------------------------------------------------------------------

static FeatureCollection load_data() {
  return rfl::json::load<FeatureCollection>("benchmarks/data/canada.json")
      .value();
}

// ----------------------------------------------------------------------------

static void BM_canada_read_reflect_cpp_avro(benchmark::State &state) {
  const auto schema = rfl::avro::to_schema<FeatureCollection>();
  const auto data = rfl::avro::write(load_data(), schema);
  for (auto _ : state) {
    const auto res = rfl::avro::read<FeatureCollection>(data, schema);
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_canada_read_reflect_cpp_avro);

static void BM_canada_read_reflect_cpp_bson(benchmark::State &state) {
  const auto data = rfl::bson::write(load_data());
  for (auto _ : state) {
    const auto res = rfl::bson::read<FeatureCollection>(data);
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_canada_read_reflect_cpp_bson);

static void BM_canada_read_reflect_cpp_capnproto(benchmark::State &state) {
  const auto schema = rfl::capnproto::to_schema<FeatureCollection>();
  const auto data = rfl::capnproto::write(load_data(), schema);
  for (auto _ : state) {
    const auto res = rfl::capnproto::read<FeatureCollection>(data, schema);
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_canada_read_reflect_cpp_capnproto);

static void BM_canada_read_reflect_cpp_cbor(benchmark::State &state) {
  const auto data = rfl::cbor::write(load_data());
  for (auto _ : state) {
    const auto res = rfl::cbor::read<FeatureCollection>(data);
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_canada_read_reflect_cpp_cbor);

static void BM_canada_read_reflect_cpp_cbor_without_field_names(
    benchmark::State &state) {
  const auto data = rfl::cbor::write<rfl::NoFieldNames>(load_data());
  for (auto _ : state) {
    const auto res =
        rfl::cbor::read<FeatureCollection, rfl::NoFieldNames>(data);
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_canada_read_reflect_cpp_cbor_without_field_names);

static void BM_canada_read_reflect_cpp_flexbuf(benchmark::State &state) {
  const auto data = rfl::flexbuf::write(load_data());
  for (auto _ : state) {
    const auto res = rfl::flexbuf::read<FeatureCollection>(data);
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_canada_read_reflect_cpp_flexbuf);

static void BM_canada_read_reflect_cpp_flexbuf_without_field_names(
    benchmark::State &state) {
  const auto data = rfl::flexbuf::write<rfl::NoFieldNames>(load_data());
  for (auto _ : state) {
    const auto res =
        rfl::flexbuf::read<FeatureCollection, rfl::NoFieldNames>(data);
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_canada_read_reflect_cpp_flexbuf_without_field_names);

static void BM_canada_read_reflect_cpp_json(benchmark::State &state) {
  const auto data = rfl::json::write(load_data());
  for (auto _ : state) {
    const auto res = rfl::json::read<FeatureCollection>(data);
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_canada_read_reflect_cpp_json);

static void BM_canada_read_reflect_cpp_json_without_field_names(
    benchmark::State &state) {
  const auto data = rfl::json::write<rfl::NoFieldNames>(load_data());
  for (auto _ : state) {
    const auto res =
        rfl::json::read<FeatureCollection, rfl::NoFieldNames>(data);
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_canada_read_reflect_cpp_json_without_field_names);

static void BM_canada_read_reflect_cpp_msgpack(benchmark::State &state) {
  const auto data = rfl::msgpack::write(load_data());
  for (auto _ : state) {
    const auto res = rfl::msgpack::read<FeatureCollection>(data);
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_canada_read_reflect_cpp_msgpack);

static void BM_canada_read_reflect_cpp_msgpack_without_field_names(
    benchmark::State &state) {
  const auto data = rfl::msgpack::write<rfl::NoFieldNames>(load_data());
  for (auto _ : state) {
    const auto res =
        rfl::msgpack::read<FeatureCollection, rfl::NoFieldNames>(data);
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_canada_read_reflect_cpp_msgpack_without_field_names);

static void BM_canada_read_reflect_cpp_toml(benchmark::State &state) {
  const auto data = rfl::toml::write(load_data());
  for (auto _ : state) {
    const auto res = rfl::toml::read<FeatureCollection>(data);
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_canada_read_reflect_cpp_toml);

static void BM_canada_read_reflect_cpp_ubjson(benchmark::State &state) {
  const auto data = rfl::ubjson::write(load_data());
  for (auto _ : state) {
    const auto res = rfl::ubjson::read<FeatureCollection>(data);
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_canada_read_reflect_cpp_ubjson);

static void BM_canada_read_reflect_cpp_ubjson_without_field_names(
    benchmark::State &state) {
  const auto data = rfl::ubjson::write<rfl::NoFieldNames>(load_data());
  for (auto _ : state) {
    const auto res =
        rfl::ubjson::read<FeatureCollection, rfl::NoFieldNames>(data);
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_canada_read_reflect_cpp_ubjson_without_field_names);

static void BM_canada_read_reflect_cpp_yaml(benchmark::State &state) {
  const auto data = rfl::yaml::write(load_data());
  for (auto _ : state) {
    const auto res = rfl::yaml::read<FeatureCollection>(data);
    if (!res) {
      std::cout << res.error().what() << std::endl;
    }
  }
}
BENCHMARK(BM_canada_read_reflect_cpp_yaml);

// ----------------------------------------------------------------------------

}  // namespace canada_read