diff --git a/benchmark/bench_sax.cpp b/benchmark/bench_sax.cpp index 36baba80..9eb56e4a 100644 --- a/benchmark/bench_sax.cpp +++ b/benchmark/bench_sax.cpp @@ -28,4 +28,8 @@ SIMDJSON_POP_DISABLE_WARNINGS #include "largerandom/sax.h" #include "largerandom/dom.h" +#include "kostya/ondemand.h" +#include "kostya/iter.h" +#include "kostya/dom.h" + BENCHMARK_MAIN(); diff --git a/benchmark/kostya/dom.h b/benchmark/kostya/dom.h new file mode 100644 index 00000000..b81fbc7e --- /dev/null +++ b/benchmark/kostya/dom.h @@ -0,0 +1,69 @@ +#pragma once + +#if SIMDJSON_EXCEPTIONS + +#include "kostya.h" + +namespace kostya { + +using namespace simdjson; + +class Dom { +public: + simdjson_really_inline bool Run(const padded_string &json); + + simdjson_really_inline const std::vector &Result() { return container; } + simdjson_really_inline size_t ItemCount() { return container.size(); } + +private: + dom::parser parser{}; + std::vector container{}; +}; + +simdjson_really_inline bool Dom::Run(const padded_string &json) { + container.clear(); + + for (auto point : parser.parse(json)["coordinates"]) { + container.emplace_back(my_point{point["x"], point["y"], point["z"]}); + } + + return true; +} + +BENCHMARK_TEMPLATE(Kostya, Dom); + +namespace sum { + +class Dom { +public: + simdjson_really_inline bool Run(const padded_string &json); + + simdjson_really_inline my_point &Result() { return sum; } + simdjson_really_inline size_t ItemCount() { return count; } + +private: + dom::parser parser{}; + my_point sum{}; + size_t count{}; +}; + +simdjson_really_inline bool Dom::Run(const padded_string &json) { + sum = { 0, 0, 0 }; + count = 0; + + for (auto coord : parser.parse(json)["coordinates"]) { + sum.x += double(coord["x"]); + sum.y += double(coord["y"]); + sum.z += double(coord["z"]); + count++; + } + + return true; +} + +BENCHMARK_TEMPLATE(KostyaSum, Dom); + +} // namespace sum +} // namespace kostya + +#endif // SIMDJSON_EXCEPTIONS \ No newline at end of file diff --git a/benchmark/kostya/iter.h b/benchmark/kostya/iter.h new file mode 100644 index 00000000..6de10dd0 --- /dev/null +++ b/benchmark/kostya/iter.h @@ -0,0 +1,103 @@ +#pragma once + +#ifdef SIMDJSON_IMPLEMENTATION +#if SIMDJSON_EXCEPTIONS + +#include "kostya.h" + +namespace kostya { +namespace { + +using namespace simdjson; +using namespace SIMDJSON_IMPLEMENTATION; +using namespace SIMDJSON_IMPLEMENTATION::stage2; + +class Iter { +public: + simdjson_really_inline bool Run(const padded_string &json); + + simdjson_really_inline const std::vector &Result() { return container; } + simdjson_really_inline size_t ItemCount() { return container.size(); } + +private: + ondemand::parser parser{}; + std::vector container{}; + + simdjson_really_inline simdjson_result first_double(SIMDJSON_IMPLEMENTATION::ondemand::json_iterator &iter, const char *key) { + if (!iter.start_object() || ondemand::raw_json_string(iter.field_key()) != key || iter.field_value()) { throw "Invalid field"; } + return iter.get_double(); + } + + simdjson_really_inline simdjson_result next_double(SIMDJSON_IMPLEMENTATION::ondemand::json_iterator &iter, const char *key) { + if (!iter.has_next_field() || ondemand::raw_json_string(iter.field_key()) != key || iter.field_value()) { throw "Invalid field"; } + return iter.get_double(); + } + +}; + +simdjson_really_inline bool Iter::Run(const padded_string &json) { + container.clear(); + + using std::cerr; + using std::endl; + auto iter = parser.iterate_raw(json).value(); + if (!iter.start_object() || !iter.find_field_raw("coordinates")) { cerr << "find coordinates field failed" << endl; return false; } + if (iter.start_array()) { + do { + container.emplace_back(my_point{first_double(iter, "x"), next_double(iter, "y"), next_double(iter, "z")}); + if (iter.skip_container()) { return false; } // Skip the rest of the coordinates object + } while (iter.has_next_element()); + } + + return true; +} + +BENCHMARK_TEMPLATE(Kostya, Iter); + +} // unnamed namespace + +namespace sum { +namespace { + +class Iter { +public: + simdjson_really_inline bool Run(const padded_string &json); + + simdjson_really_inline my_point &Result() { return sum; } + simdjson_really_inline size_t ItemCount() { return count; } + +private: + ondemand::parser parser{}; + my_point sum{}; + size_t count{}; +}; + +simdjson_really_inline bool Iter::Run(const padded_string &json) { + sum = {0,0,0}; + count = 0; + + auto iter = parser.iterate_raw(json).value(); + if (!iter.start_object() || !iter.find_field_raw("coordinates")) { return false; } + if (!iter.start_array()) { return false; } + do { + if (!iter.start_object() || !iter.find_field_raw("x")) { return false; } + sum.x += iter.get_double(); + if (!iter.has_next_field() || !iter.find_field_raw("y")) { return false; } + sum.y += iter.get_double(); + if (!iter.has_next_field() || !iter.find_field_raw("z")) { return false; } + sum.z += iter.get_double(); + if (iter.skip_container()) { return false; } // Skip the rest of the coordinates object + count++; + } while (iter.has_next_element()); + + return true; +} + +BENCHMARK_TEMPLATE(KostyaSum, Iter); + +} // unnamed namespace +} // namespace sum +} // namespace kostya + +#endif // SIMDJSON_EXCEPTIONS +#endif // SIMDJSON_IMPLEMENTATION diff --git a/benchmark/kostya/kostya.h b/benchmark/kostya/kostya.h new file mode 100644 index 00000000..1f9442e9 --- /dev/null +++ b/benchmark/kostya/kostya.h @@ -0,0 +1,95 @@ +#pragma once + +#if SIMDJSON_EXCEPTIONS + +// +// Interface +// + +namespace kostya { +template static void Kostya(benchmark::State &state); +namespace sum { +template static void KostyaSum(benchmark::State &state); +} + +using namespace simdjson; + +static void append_coordinate(std::default_random_engine &e, std::uniform_real_distribution<> &dis, std::stringstream &myss) { + using std::endl; + myss << R"( {)" << endl; + myss << R"( "x": )" << dis(e) << "," << endl; + myss << R"( "y": )" << dis(e) << "," << endl; + myss << R"( "z": )" << dis(e) << "," << endl; + myss << R"( "name": ")" << char('a'+dis(e)*25) << char('a'+dis(e)*25) << char('a'+dis(e)*25) << char('a'+dis(e)*25) << char('a'+dis(e)*25) << char('a'+dis(e)*25) << " " << int(dis(e)*10000) << "\"," << endl; + myss << R"( "opts": {)" << endl; + myss << R"( "1": [)" << endl; + myss << R"( 1,)" << endl; + myss << R"( true)" << endl; + myss << R"( ])" << endl; + myss << R"( })" << endl; + myss << R"( })"; +} + +static std::string build_json_array(size_t N) { + using namespace std; + default_random_engine e; + uniform_real_distribution<> dis(0, 1); + stringstream myss; + myss << R"({)" << endl; + myss << R"( "coordinates": [)" << endl; + for (size_t i=1; i +#include "event_counter.h" +#include "dom.h" +#include "json_benchmark.h" + +namespace kostya { + +template static void Kostya(benchmark::State &state) { + JsonBenchmark(state, get_built_json_array()); +} + +namespace sum { +template static void KostyaSum(benchmark::State &state) { + JsonBenchmark(state, get_built_json_array()); +} +} + +} // namespace kostya + +#endif // SIMDJSON_EXCEPTIONS diff --git a/benchmark/kostya/ondemand.h b/benchmark/kostya/ondemand.h new file mode 100644 index 00000000..23302699 --- /dev/null +++ b/benchmark/kostya/ondemand.h @@ -0,0 +1,83 @@ +#pragma once + +#ifdef SIMDJSON_IMPLEMENTATION +#if SIMDJSON_EXCEPTIONS + +#include "kostya.h" + +namespace kostya { +namespace { + +using namespace simdjson; +using namespace SIMDJSON_IMPLEMENTATION; +using namespace SIMDJSON_IMPLEMENTATION::stage2; + +class OnDemand { +public: + simdjson_really_inline bool Run(const padded_string &json); + + simdjson_really_inline const std::vector &Result() { return container; } + simdjson_really_inline size_t ItemCount() { return container.size(); } + +private: + ondemand::parser parser{}; + std::vector container{}; +}; + +simdjson_really_inline bool OnDemand::Run(const padded_string &json) { + container.clear(); + + using std::cout; + using std::endl; + + auto doc = parser.iterate(json); + for (ondemand::object coord : doc["coordinates"]) { + container.emplace_back(my_point{coord["x"], coord["y"], coord["z"]}); + } + + return true; +} + +BENCHMARK_TEMPLATE(Kostya, OnDemand); + +} // unnamed namespace + +namespace sum { +namespace { + +class OnDemand { +public: + simdjson_really_inline bool Run(const padded_string &json); + + simdjson_really_inline my_point &Result() { return sum; } + simdjson_really_inline size_t ItemCount() { return count; } + +private: + ondemand::parser parser{}; + my_point sum{}; + size_t count{}; +}; + +simdjson_really_inline bool OnDemand::Run(const padded_string &json) { + sum = {0,0,0}; + count = 0; + + auto doc = parser.iterate(json); + for (ondemand::object coord : doc["coordinates"]) { + sum.x += double(coord["x"]); + sum.y += double(coord["y"]); + sum.z += double(coord["z"]); + count++; + } + + return true; +} + +BENCHMARK_TEMPLATE(KostyaSum, OnDemand); + +} // unnamed namespace +} // namespace sum +} // namespace kostya + +#endif // SIMDJSON_EXCEPTIONS +#endif // SIMDJSON_IMPLEMENTATION diff --git a/benchmark/largerandom/ondemand.h b/benchmark/largerandom/ondemand.h index 79508dc0..8464f9cf 100644 --- a/benchmark/largerandom/ondemand.h +++ b/benchmark/largerandom/ondemand.h @@ -32,8 +32,6 @@ simdjson_really_inline bool OnDemand::Run(const padded_string &json) { container.emplace_back(my_point{coord["x"], coord["y"], coord["z"]}); } - printf("count: %d\n", int(container.size())); fflush(stdout); - return true; }