simdjson/benchmark/kostya/kostya.h

104 lines
3.1 KiB
C
Raw Normal View History

2020-09-04 04:32:13 +08:00
#pragma once
#if SIMDJSON_EXCEPTIONS
#include "json_benchmark/string_runner.h"
#include <vector>
#include <random>
2020-09-04 04:32:13 +08:00
namespace kostya {
static const simdjson::padded_string &get_built_json_array();
struct point {
double x;
double y;
double z;
simdjson_really_inline bool operator==(const point &other) const {
return x == other.x && y == other.y && z == other.z;
}
simdjson_really_inline bool operator!=(const point &other) const {
return !(*this == other);
}
};
simdjson_unused static std::ostream &operator<<(std::ostream &o, const point &p) {
return o << p.x << "," << p.y << "," << p.z << std::endl;
2020-09-04 04:32:13 +08:00
}
template<typename I>
struct runner : public json_benchmark::string_runner<I> {
std::vector<point> points;
runner() : json_benchmark::string_runner<I>(get_built_json_array()) {}
bool before_run(benchmark::State &state) {
if (!json_benchmark::string_runner<I>::before_run(state)) { return false; }
points.clear();
return true;
}
bool run(benchmark::State &) {
return this->implementation.run(this->json, points);
}
template<typename R>
bool diff(benchmark::State &state, runner<R> &reference) {
return diff_results(state, points, reference.points);
}
2021-01-05 07:22:35 +08:00
size_t items_per_iteration() {
return points.size();
}
};
2020-09-04 04:32:13 +08:00
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<N; i++) {
append_coordinate(e, dis, myss); myss << "," << endl;
}
append_coordinate(e, dis, myss); myss << endl;
myss << R"( ],)" << endl;
myss << R"( "info": "some info")" << endl;
myss << R"(})" << endl;
string answer = myss.str();
2020-11-04 04:48:09 +08:00
cout << "Creating a source file spanning " << (answer.size() + 512) / 1024 << " KB " << endl;
2020-09-04 04:32:13 +08:00
return answer;
}
static const simdjson::padded_string &get_built_json_array() {
static simdjson::padded_string json = build_json_array(524288);
2020-09-04 04:32:13 +08:00
return json;
}
struct simdjson_dom;
2020-09-04 04:32:13 +08:00
template<typename I> simdjson_really_inline static void kostya(benchmark::State &state) {
json_benchmark::run_json_benchmark<runner<I>, runner<simdjson_dom>>(state);
2020-09-04 04:32:13 +08:00
}
} // namespace kostya
#endif // SIMDJSON_EXCEPTIONS