Skip to content

Commit ecf25cf

Browse files
committed
Add some comparison tests for walking perf
1 parent fe14af7 commit ecf25cf

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

include/json2cpp/constexpr_json.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ template<typename CharType> struct basic_json
303303
}
304304
}
305305

306-
constexpr iterator find(const std::string_view key) const
306+
constexpr iterator find(const std::basic_string_view<CharType> key) const
307307
{
308308
for (auto itr = begin(); itr != end(); ++itr) {
309309
if (itr.key() == key) { return itr; }
@@ -312,7 +312,7 @@ template<typename CharType> struct basic_json
312312
return end();
313313
}
314314

315-
constexpr const basic_json &operator[](const std::string_view key) const
315+
constexpr const basic_json &operator[](const std::basic_string_view<CharType> key) const
316316
{
317317
const auto &children = object_data();
318318

@@ -369,7 +369,8 @@ template<typename CharType> struct basic_json
369369
}
370370
} else if constexpr (std::is_same_v<Type, double>) {
371371
if (const auto *value = data.get_if_floating_point(); value != nullptr) { return *value; }
372-
} else if constexpr (std::is_same_v<Type, std::string_view>) {
372+
} else if constexpr (std::is_same_v<Type,
373+
std::basic_string_view<CharType>> || std::is_same_v<Type, std::basic_string<CharType>>) {
373374
if (const auto *value = data.get_if_string(); value != nullptr) { return *value; }
374375
} else if constexpr (std::is_same_v<Type, bool>) {
375376
if (const auto *value = data.get_if_boolean(); value != nullptr) { return *value; }

src/schema_validator.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ static constexpr auto USAGE =
4646
Usage:
4747
schema_validator <schema_file> <document_to_validate> [--internal]
4848
schema_validator (-h | --help)
49+
schema_validator <schema_file> --walk [--internal]
4950
schema_validator --version
5051
Options:
5152
-h --help Show this screen.
5253
--version Show version.
5354
--internal Use internal schema
55+
--walk Just walk the schema and count objects (perf test)
5456
)";
5557

5658

@@ -134,6 +136,55 @@ bool validate_internal(const std::filesystem::path &file_to_validate)
134136
return result;
135137
}
136138

139+
template<typename JSON>
140+
void walk_internal(std::int64_t &int_sum,
141+
double &double_sum,
142+
std::size_t &string_sizes,
143+
int &array_count,
144+
int &object_count,
145+
const JSON &obj)
146+
{
147+
if (obj.is_number_integer()) {
148+
int_sum += obj.template get<std::int64_t>();
149+
} else if (obj.is_number_float()) {
150+
double_sum += obj.template get<double>();
151+
} else if (obj.is_string()) {
152+
string_sizes += obj.template get<std::string_view>().size();
153+
} else if (obj.is_array()) {
154+
++array_count;
155+
for (const auto &child : obj) {
156+
walk_internal(int_sum, double_sum, string_sizes, array_count, object_count, child);
157+
}
158+
} else if (obj.is_object()) {
159+
++object_count;
160+
for (const auto &child : obj) {
161+
walk_internal(int_sum, double_sum, string_sizes, array_count, object_count, child);
162+
}
163+
}
164+
}
165+
166+
template<typename JSON>
167+
void walk(const JSON &objects)
168+
{
169+
std::int64_t int_sum{};
170+
double double_sum{};
171+
std::size_t string_sizes{};
172+
int array_count{};
173+
int object_count{};
174+
175+
spdlog::info("Starting tree walk");
176+
177+
walk_internal(int_sum,
178+
double_sum,
179+
string_sizes,
180+
array_count,
181+
object_count,
182+
objects
183+
);
184+
185+
spdlog::info("{} {} {} {} {}", int_sum, double_sum, string_sizes, array_count, object_count);
186+
}
187+
137188
int main(int argc, const char **argv)
138189
{
139190
try {
@@ -142,9 +193,27 @@ int main(int argc, const char **argv)
142193
true,// show help if requested
143194
"schema_validator 0.0.1 Copyright 2022 Jason Turner");// version string
144195

196+
if (args.at("--walk").asBool()) {
197+
if (args.at("--internal").asBool()) {
198+
walk(compiled_json::energyplus_schema::get_energyplus_schema());
199+
} else {
200+
std::filesystem::path schema_file_name = args.at("<schema_file>").asString();
201+
spdlog::info("Creating nlohmann::json object");
202+
nlohmann::json schema;
203+
spdlog::info("Opening json file");
204+
std::ifstream schema_file(schema_file_name);
205+
spdlog::info("Loading json file");
206+
schema_file >> schema;
207+
walk(schema);
208+
}
209+
return EXIT_SUCCESS;
210+
}
211+
212+
145213
std::filesystem::path schema = args.at("<schema_file>").asString();
146214
std::filesystem::path doc = args.at("<document_to_validate>").asString();
147215

216+
148217
if (args.at("--internal").asBool()) {
149218
validate_internal(doc);
150219
} else {

0 commit comments

Comments
 (0)