File: generic_benchmark.cpp

package info (click to toggle)
glaze 7.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,316 kB
  • sloc: cpp: 170,219; sh: 109; ansic: 26; makefile: 12
file content (367 lines) | stat: -rw-r--r-- 11,226 bytes parent folder | download
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Benchmark: glz::generic (ordered_small_map) vs glz::generic with std::map
// Tests read_json, write_json, and key lookup performance

#include <map>
#include <string>

#include "bencher/bencher.hpp"
#include "bencher/diagnostics.hpp"
#include "glaze/json.hpp"

// User-defined std::map adapter for generic_json
template <class T>
using std_map = std::map<std::string, T, std::less<>>;

using generic_default = glz::generic; // ordered_small_map
using generic_std_map = glz::generic_json<glz::num_mode::f64, std_map>;

// Small JSON object (8 keys)
inline const std::string small_json = R"({
   "id": 12345,
   "name": "Alice Smith",
   "email": "alice@example.com",
   "age": 30,
   "active": true,
   "score": 98.5,
   "role": "admin",
   "verified": false
})";

// Medium JSON object (nested, ~20 keys total)
inline const std::string medium_json = R"({
   "user": {
      "id": 12345,
      "name": "Alice Smith",
      "email": "alice@example.com",
      "age": 30,
      "active": true
   },
   "settings": {
      "theme": "dark",
      "language": "en",
      "notifications": true,
      "timezone": "UTC",
      "currency": "USD"
   },
   "scores": [95, 87, 92, 88, 91],
   "metadata": {
      "created": "2024-01-15",
      "updated": "2024-06-20",
      "version": 3,
      "source": "api",
      "format": "json"
   },
   "tags": ["premium", "verified", "active"]
})";

// Generate a large JSON array of objects
inline std::string generate_large_json(size_t count)
{
   std::string json = R"({"users":[)";
   for (size_t i = 0; i < count; ++i) {
      if (i > 0) json += ",";
      json += R"({"id":)";
      json += std::to_string(i);
      json += R"(,"name":"User )";
      json += std::to_string(i);
      json += R"(","email":"user)";
      json += std::to_string(i);
      json += R"(@test.com","age":)";
      json += std::to_string(20 + (i % 50));
      json += R"(,"active":)";
      json += (i % 2 == 0) ? "true" : "false";
      json += R"(,"score":)";
      json += std::to_string(i * 10);
      json += "}";
   }
   json += R"(],"count":)";
   json += std::to_string(count);
   json += "}";
   return json;
}

int main()
{
   const auto large_json = generate_large_json(100);

   // ========================================================================
   // Benchmark: read_json (small object)
   // ========================================================================
   {
      bencher::stage stage;
      stage.name = "read_json (small, 8 keys)";

      stage.run("ordered_small_map", [&] {
         generic_default json{};
         auto ec = glz::read_json(json, small_json);
         if (ec) std::abort();
         bencher::do_not_optimize(json);
         return small_json.size();
      });

      stage.run("std::map", [&] {
         generic_std_map json{};
         auto ec = glz::read_json(json, small_json);
         if (ec) std::abort();
         bencher::do_not_optimize(json);
         return small_json.size();
      });

      bencher::print_results(stage);
   }

   // ========================================================================
   // Benchmark: read_json (medium object)
   // ========================================================================
   {
      bencher::stage stage;
      stage.name = "read_json (medium, nested)";

      stage.run("ordered_small_map", [&] {
         generic_default json{};
         auto ec = glz::read_json(json, medium_json);
         if (ec) std::abort();
         bencher::do_not_optimize(json);
         return medium_json.size();
      });

      stage.run("std::map", [&] {
         generic_std_map json{};
         auto ec = glz::read_json(json, medium_json);
         if (ec) std::abort();
         bencher::do_not_optimize(json);
         return medium_json.size();
      });

      bencher::print_results(stage);
   }

   // ========================================================================
   // Benchmark: read_json (large, 100 objects)
   // ========================================================================
   {
      bencher::stage stage;
      stage.name = "read_json (large, 100 objects)";

      stage.run("ordered_small_map", [&] {
         generic_default json{};
         auto ec = glz::read_json(json, large_json);
         if (ec) std::abort();
         bencher::do_not_optimize(json);
         return large_json.size();
      });

      stage.run("std::map", [&] {
         generic_std_map json{};
         auto ec = glz::read_json(json, large_json);
         if (ec) std::abort();
         bencher::do_not_optimize(json);
         return large_json.size();
      });

      bencher::print_results(stage);
   }

   // ========================================================================
   // Benchmark: write_json (small object)
   // ========================================================================
   {
      generic_default json_default{};
      glz::read_json(json_default, small_json);
      generic_std_map json_stdmap{};
      glz::read_json(json_stdmap, small_json);

      bencher::stage stage;
      stage.name = "write_json (small, 8 keys)";

      stage.run("ordered_small_map", [&] {
         std::string out{};
         auto ec = glz::write_json(json_default, out);
         if (ec) std::abort();
         bencher::do_not_optimize(out);
         return out.size();
      });

      stage.run("std::map", [&] {
         std::string out{};
         auto ec = glz::write_json(json_stdmap, out);
         if (ec) std::abort();
         bencher::do_not_optimize(out);
         return out.size();
      });

      bencher::print_results(stage);
   }

   // ========================================================================
   // Benchmark: write_json (small, reused buffer)
   // ========================================================================
   {
      generic_default json_default{};
      glz::read_json(json_default, small_json);
      generic_std_map json_stdmap{};
      glz::read_json(json_stdmap, small_json);

      std::string out_default, out_stdmap;
      out_default.reserve(512);
      out_stdmap.reserve(512);

      bencher::stage stage;
      stage.name = "write_json (small, 8 keys, reused buffer)";
      stage.cold_cache = false;

      stage.run("ordered_small_map", [&] {
         out_default.clear();
         auto ec = glz::write_json(json_default, out_default);
         if (ec) std::abort();
         bencher::do_not_optimize(out_default);
         return out_default.size();
      });

      stage.run("std::map", [&] {
         out_stdmap.clear();
         auto ec = glz::write_json(json_stdmap, out_stdmap);
         if (ec) std::abort();
         bencher::do_not_optimize(out_stdmap);
         return out_stdmap.size();
      });

      bencher::print_results(stage);
   }

   // ========================================================================
   // Benchmark: write_json (medium object)
   // ========================================================================
   {
      generic_default json_default{};
      glz::read_json(json_default, medium_json);
      generic_std_map json_stdmap{};
      glz::read_json(json_stdmap, medium_json);

      bencher::stage stage;
      stage.name = "write_json (medium, nested)";

      stage.run("ordered_small_map", [&] {
         std::string out{};
         auto ec = glz::write_json(json_default, out);
         if (ec) std::abort();
         bencher::do_not_optimize(out);
         return out.size();
      });

      stage.run("std::map", [&] {
         std::string out{};
         auto ec = glz::write_json(json_stdmap, out);
         if (ec) std::abort();
         bencher::do_not_optimize(out);
         return out.size();
      });

      bencher::print_results(stage);
   }

   // ========================================================================
   // Benchmark: write_json (medium, reused buffer)
   // ========================================================================
   {
      generic_default json_default{};
      glz::read_json(json_default, medium_json);
      generic_std_map json_stdmap{};
      glz::read_json(json_stdmap, medium_json);

      std::string out_default, out_stdmap;
      out_default.reserve(512);
      out_stdmap.reserve(512);

      bencher::stage stage;
      stage.name = "write_json (medium, nested, reused buffer)";
      stage.cold_cache = false;

      stage.run("ordered_small_map", [&] {
         out_default.clear();
         auto ec = glz::write_json(json_default, out_default);
         if (ec) std::abort();
         bencher::do_not_optimize(out_default);
         return out_default.size();
      });

      stage.run("std::map", [&] {
         out_stdmap.clear();
         auto ec = glz::write_json(json_stdmap, out_stdmap);
         if (ec) std::abort();
         bencher::do_not_optimize(out_stdmap);
         return out_stdmap.size();
      });

      bencher::print_results(stage);
   }

   // ========================================================================
   // Benchmark: Key lookup (operator[])
   // ========================================================================
   {
      generic_default json_default{};
      glz::read_json(json_default, small_json);
      generic_std_map json_stdmap{};
      glz::read_json(json_stdmap, small_json);

      bencher::stage stage;
      stage.name = "Key lookup (8 keys, access all)";

      stage.run("ordered_small_map", [&] {
         double sum = 0;
         sum += json_default["id"].get<double>();
         sum += json_default["age"].get<double>();
         sum += json_default["score"].get<double>();
         sum += json_default["active"].get<bool>() ? 1.0 : 0.0;
         bencher::do_not_optimize(sum);
         return 4 * sizeof(double);
      });

      stage.run("std::map", [&] {
         double sum = 0;
         sum += json_stdmap["id"].get<double>();
         sum += json_stdmap["age"].get<double>();
         sum += json_stdmap["score"].get<double>();
         sum += json_stdmap["active"].get<bool>() ? 1.0 : 0.0;
         bencher::do_not_optimize(sum);
         return 4 * sizeof(double);
      });

      bencher::print_results(stage);
   }

   // ========================================================================
   // Benchmark: Roundtrip (read + write)
   // ========================================================================
   {
      bencher::stage stage;
      stage.name = "Roundtrip read+write (medium)";

      stage.run("ordered_small_map", [&] {
         generic_default json{};
         auto ec = glz::read_json(json, medium_json);
         if (ec) std::abort();
         std::string out{};
         auto ec2 = glz::write_json(json, out);
         if (ec2) std::abort();
         bencher::do_not_optimize(out);
         return medium_json.size() + out.size();
      });

      stage.run("std::map", [&] {
         generic_std_map json{};
         auto ec = glz::read_json(json, medium_json);
         if (ec) std::abort();
         std::string out{};
         auto ec2 = glz::write_json(json, out);
         if (ec2) std::abort();
         bencher::do_not_optimize(out);
         return medium_json.size() + out.size();
      });

      bencher::print_results(stage);
   }

   return 0;
}