From ecf25cfbd95faf59796ad6d96256a31135bab829 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 23 Feb 2022 13:19:02 -0700 Subject: [PATCH] Add some comparison tests for walking perf --- include/json2cpp/constexpr_json.hpp | 7 +-- src/schema_validator.cpp | 69 +++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/include/json2cpp/constexpr_json.hpp b/include/json2cpp/constexpr_json.hpp index 91cc28f..0bb7905 100644 --- a/include/json2cpp/constexpr_json.hpp +++ b/include/json2cpp/constexpr_json.hpp @@ -303,7 +303,7 @@ template struct basic_json } } - constexpr iterator find(const std::string_view key) const + constexpr iterator find(const std::basic_string_view key) const { for (auto itr = begin(); itr != end(); ++itr) { if (itr.key() == key) { return itr; } @@ -312,7 +312,7 @@ template struct basic_json return end(); } - constexpr const basic_json &operator[](const std::string_view key) const + constexpr const basic_json &operator[](const std::basic_string_view key) const { const auto &children = object_data(); @@ -369,7 +369,8 @@ template struct basic_json } } else if constexpr (std::is_same_v) { if (const auto *value = data.get_if_floating_point(); value != nullptr) { return *value; } - } else if constexpr (std::is_same_v) { + } else if constexpr (std::is_same_v> || std::is_same_v>) { if (const auto *value = data.get_if_string(); value != nullptr) { return *value; } } else if constexpr (std::is_same_v) { if (const auto *value = data.get_if_boolean(); value != nullptr) { return *value; } diff --git a/src/schema_validator.cpp b/src/schema_validator.cpp index 738382f..e32a076 100644 --- a/src/schema_validator.cpp +++ b/src/schema_validator.cpp @@ -46,11 +46,13 @@ static constexpr auto USAGE = Usage: schema_validator [--internal] schema_validator (-h | --help) + schema_validator --walk [--internal] schema_validator --version Options: -h --help Show this screen. --version Show version. --internal Use internal schema + --walk Just walk the schema and count objects (perf test) )"; @@ -134,6 +136,55 @@ bool validate_internal(const std::filesystem::path &file_to_validate) return result; } +template +void walk_internal(std::int64_t &int_sum, + double &double_sum, + std::size_t &string_sizes, + int &array_count, + int &object_count, + const JSON &obj) +{ + if (obj.is_number_integer()) { + int_sum += obj.template get(); + } else if (obj.is_number_float()) { + double_sum += obj.template get(); + } else if (obj.is_string()) { + string_sizes += obj.template get().size(); + } else if (obj.is_array()) { + ++array_count; + for (const auto &child : obj) { + walk_internal(int_sum, double_sum, string_sizes, array_count, object_count, child); + } + } else if (obj.is_object()) { + ++object_count; + for (const auto &child : obj) { + walk_internal(int_sum, double_sum, string_sizes, array_count, object_count, child); + } + } +} + +template +void walk(const JSON &objects) +{ + std::int64_t int_sum{}; + double double_sum{}; + std::size_t string_sizes{}; + int array_count{}; + int object_count{}; + + spdlog::info("Starting tree walk"); + + walk_internal(int_sum, + double_sum, + string_sizes, + array_count, + object_count, + objects + ); + + spdlog::info("{} {} {} {} {}", int_sum, double_sum, string_sizes, array_count, object_count); +} + int main(int argc, const char **argv) { try { @@ -142,9 +193,27 @@ int main(int argc, const char **argv) true,// show help if requested "schema_validator 0.0.1 Copyright 2022 Jason Turner");// version string + if (args.at("--walk").asBool()) { + if (args.at("--internal").asBool()) { + walk(compiled_json::energyplus_schema::get_energyplus_schema()); + } else { + std::filesystem::path schema_file_name = args.at("").asString(); + spdlog::info("Creating nlohmann::json object"); + nlohmann::json schema; + spdlog::info("Opening json file"); + std::ifstream schema_file(schema_file_name); + spdlog::info("Loading json file"); + schema_file >> schema; + walk(schema); + } + return EXIT_SUCCESS; + } + + std::filesystem::path schema = args.at("").asString(); std::filesystem::path doc = args.at("").asString(); + if (args.at("--internal").asBool()) { validate_internal(doc); } else {