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
|
#pragma once
#include "json_benchmark/file_runner.h"
#include <string>
namespace accessor_performance {
using namespace json_benchmark;
// Test JSON for accessor benchmarks
static const char* TEST_JSON = R"({
"name": "Alice",
"age": 30,
"email": "alice@example.com",
"address": {
"street": "123 Main St",
"city": "Boston",
"state": "MA",
"zip": 12345,
"coordinates": {
"lat": 42.3601,
"lon": -71.0589
}
},
"scores": [95, 87, 92, 88, 91],
"preferences": {
"theme": "dark",
"notifications": {
"email": true,
"push": false,
"sms": true
}
}
})";
// Struct definitions for compile-time validation
#if SIMDJSON_STATIC_REFLECTION
struct Coordinates {
double lat;
double lon;
};
struct Address {
std::string street;
std::string city;
std::string state;
int64_t zip;
Coordinates coordinates;
};
struct Notifications {
bool email;
bool push;
bool sms;
};
struct Preferences {
std::string theme;
Notifications notifications;
};
struct TestData {
std::string name;
int64_t age;
std::string email;
Address address;
std::vector<int64_t> scores;
Preferences preferences;
};
#endif // SIMDJSON_STATIC_REFLECTION
// Single-access benchmark runner: measures ONE field access per iteration
template<typename I>
struct single_access_runner : public file_runner<I> {
std::string result_string;
int64_t result_int{};
double result_double{};
bool result_bool{};
bool setup(benchmark::State &state) {
this->json = simdjson::padded_string(TEST_JSON, strlen(TEST_JSON));
state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(this->json.size()));
return true;
}
bool before_run(benchmark::State &state) {
if (!file_runner<I>::before_run(state)) { return false; }
result_string.clear();
result_int = 0;
result_double = 0.0;
result_bool = false;
return true;
}
bool run(benchmark::State &) {
return this->implementation.run(this->json, result_string, result_int, result_double, result_bool);
}
template<typename R>
bool diff(benchmark::State &state, single_access_runner<R> &reference) {
if (result_string != reference.result_string ||
result_int != reference.result_int ||
result_double != reference.result_double ||
result_bool != reference.result_bool) {
std::cerr << "Accessor benchmark results differ!" << std::endl;
return false;
}
return true;
}
size_t items_per_iteration() {
return 1;
}
};
// Benchmark template definitions
struct runtime_at_path_simple;
template<typename I> simdjson_inline static void accessor_simple(benchmark::State &state) {
run_json_benchmark<single_access_runner<I>, single_access_runner<runtime_at_path_simple>>(state);
}
struct runtime_at_path_nested;
template<typename I> simdjson_inline static void accessor_nested(benchmark::State &state) {
run_json_benchmark<single_access_runner<I>, single_access_runner<runtime_at_path_nested>>(state);
}
struct runtime_at_path_deep;
template<typename I> simdjson_inline static void accessor_deep(benchmark::State &state) {
run_json_benchmark<single_access_runner<I>, single_access_runner<runtime_at_path_deep>>(state);
}
} // namespace accessor_performance
|