2018-11-30 22:37:57 +08:00
|
|
|
#include "simdjson/jsonparser.h"
|
2019-01-02 03:39:35 +08:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
#include "linux-perf-events.h"
|
2019-05-25 08:08:06 +08:00
|
|
|
#include <unistd.h>
|
2019-01-02 03:39:35 +08:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <libgen.h>
|
|
|
|
#endif //__linux__
|
|
|
|
#endif // _MSC_VER
|
2018-08-21 05:30:30 +08:00
|
|
|
|
2019-02-26 04:03:20 +08:00
|
|
|
#include <memory>
|
|
|
|
|
2018-08-21 05:30:30 +08:00
|
|
|
#include "benchmark.h"
|
2018-08-21 05:51:38 +08:00
|
|
|
|
2018-11-30 22:37:57 +08:00
|
|
|
// #define RAPIDJSON_SSE2 // bad for performance
|
|
|
|
// #define RAPIDJSON_SSE42 // bad for performance
|
2018-08-21 05:30:30 +08:00
|
|
|
#include "rapidjson/document.h"
|
2018-12-12 23:19:32 +08:00
|
|
|
#include "rapidjson/reader.h"
|
2018-08-21 05:30:30 +08:00
|
|
|
#include "rapidjson/stringbuffer.h"
|
|
|
|
#include "rapidjson/writer.h"
|
2018-11-30 22:37:57 +08:00
|
|
|
|
2018-12-19 13:40:04 +08:00
|
|
|
#include "sajson.h"
|
|
|
|
|
|
|
|
#ifdef ALLPARSER
|
2019-01-25 03:28:26 +08:00
|
|
|
|
2018-11-21 03:32:12 +08:00
|
|
|
#include "fastjson.cpp"
|
|
|
|
#include "fastjson_dom.cpp"
|
2018-11-21 05:43:22 +08:00
|
|
|
#include "gason.cpp"
|
2019-01-25 03:28:26 +08:00
|
|
|
|
2018-12-12 23:19:32 +08:00
|
|
|
#include "json11.cpp"
|
|
|
|
extern "C" {
|
2019-01-18 06:24:29 +08:00
|
|
|
#include "cJSON.c"
|
2019-05-25 08:08:06 +08:00
|
|
|
#include "cJSON.h"
|
2019-01-18 06:24:29 +08:00
|
|
|
#include "jsmn.c"
|
2019-05-25 08:08:06 +08:00
|
|
|
#include "jsmn.h"
|
|
|
|
#include "ujdecode.h"
|
|
|
|
#include "ultrajsondec.c"
|
2018-11-21 05:43:22 +08:00
|
|
|
}
|
2019-01-25 03:28:26 +08:00
|
|
|
|
|
|
|
#include "jsoncpp.cpp"
|
2019-05-25 08:08:06 +08:00
|
|
|
#include "json/json.h"
|
2019-01-25 03:28:26 +08:00
|
|
|
|
2019-05-25 08:08:06 +08:00
|
|
|
#endif
|
2018-12-19 13:40:04 +08:00
|
|
|
|
2018-08-21 05:30:30 +08:00
|
|
|
using namespace rapidjson;
|
|
|
|
|
2018-12-19 13:40:04 +08:00
|
|
|
#ifdef ALLPARSER
|
2018-11-21 03:32:12 +08:00
|
|
|
// fastjson has a tricky interface
|
2018-12-12 23:19:32 +08:00
|
|
|
void on_json_error(void *, const fastjson::ErrorContext &ec) {
|
|
|
|
// std::cerr<<"ERROR: "<<ec.mesg<<std::endl;
|
2018-11-21 03:32:12 +08:00
|
|
|
}
|
|
|
|
bool fastjson_parse(const char *input) {
|
|
|
|
fastjson::Token token;
|
|
|
|
fastjson::dom::Chunk chunk;
|
2018-12-12 23:19:32 +08:00
|
|
|
return fastjson::dom::parse_string(input, &token, &chunk, 0, &on_json_error,
|
|
|
|
NULL);
|
2018-11-21 03:32:12 +08:00
|
|
|
}
|
2018-11-21 05:43:22 +08:00
|
|
|
// end of fastjson stuff
|
2018-12-19 13:40:04 +08:00
|
|
|
#endif
|
2018-11-21 03:32:12 +08:00
|
|
|
|
2018-08-21 05:30:30 +08:00
|
|
|
int main(int argc, char *argv[]) {
|
2018-11-21 09:08:02 +08:00
|
|
|
bool verbose = false;
|
2018-12-19 11:18:23 +08:00
|
|
|
bool justdata = false;
|
2018-11-21 09:08:02 +08:00
|
|
|
int c;
|
2018-12-25 04:30:25 +08:00
|
|
|
while ((c = getopt(argc, argv, "vt")) != -1)
|
2018-12-12 23:19:32 +08:00
|
|
|
switch (c) {
|
2018-12-19 11:18:23 +08:00
|
|
|
case 't':
|
|
|
|
justdata = true;
|
|
|
|
break;
|
2018-12-12 23:19:32 +08:00
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
2018-11-21 09:08:02 +08:00
|
|
|
if (optind >= argc) {
|
2019-05-10 05:59:51 +08:00
|
|
|
std::cerr << "Usage: " << argv[0] << " <jsonfile>" << std::endl;
|
|
|
|
std::cerr << "Or " << argv[0] << " -v <jsonfile>" << std::endl;
|
2019-05-25 08:08:06 +08:00
|
|
|
std::cerr << "The '-t' flag outputs a table. " << std::endl;
|
2018-08-21 05:30:30 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2018-12-12 23:19:32 +08:00
|
|
|
const char *filename = argv[optind];
|
|
|
|
if (optind + 1 < argc) {
|
2019-05-25 08:08:06 +08:00
|
|
|
std::cerr << "warning: ignoring everything after " << argv[optind + 1]
|
|
|
|
<< std::endl;
|
2018-08-21 05:30:30 +08:00
|
|
|
}
|
2019-07-03 03:21:00 +08:00
|
|
|
simdjson::padded_string p;
|
2018-11-28 03:37:59 +08:00
|
|
|
try {
|
2019-07-03 03:21:00 +08:00
|
|
|
simdjson::get_corpus(filename).swap(p);
|
2018-12-12 23:19:32 +08:00
|
|
|
} catch (const std::exception &e) { // caught by reference to base
|
2018-11-28 03:37:59 +08:00
|
|
|
std::cout << "Could not load the file " << filename << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2018-12-12 23:19:32 +08:00
|
|
|
|
2018-08-21 05:40:50 +08:00
|
|
|
if (verbose) {
|
|
|
|
std::cout << "Input has ";
|
2018-12-01 09:27:16 +08:00
|
|
|
if (p.size() > 1024 * 1024)
|
|
|
|
std::cout << p.size() / (1024 * 1024) << " MB ";
|
|
|
|
else if (p.size() > 1024)
|
|
|
|
std::cout << p.size() / 1024 << " KB ";
|
2018-08-21 05:40:50 +08:00
|
|
|
else
|
2018-12-01 09:27:16 +08:00
|
|
|
std::cout << p.size() << " B ";
|
2018-08-21 05:40:50 +08:00
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
2019-07-03 03:21:00 +08:00
|
|
|
simdjson::ParsedJson pj;
|
2018-12-01 09:27:16 +08:00
|
|
|
bool allocok = pj.allocateCapacity(p.size(), 1024);
|
|
|
|
|
|
|
|
if (!allocok) {
|
2018-08-21 05:30:30 +08:00
|
|
|
std::cerr << "can't allocate memory" << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2019-06-03 22:59:07 +08:00
|
|
|
int repeat = (p.size() < 1 * 1000 * 1000 ? 1000 : 10);
|
2018-12-01 09:27:16 +08:00
|
|
|
int volume = p.size();
|
2019-05-25 08:08:06 +08:00
|
|
|
if (justdata) {
|
|
|
|
printf("%-42s %20s %20s %20s %20s \n", "name", "cycles_per_byte",
|
|
|
|
"cycles_per_byte_err", "gb_per_s", "gb_per_s_err");
|
2018-12-28 06:10:19 +08:00
|
|
|
}
|
2019-05-25 08:08:06 +08:00
|
|
|
if (!justdata)
|
|
|
|
BEST_TIME("simdjson (dynamic mem) ", build_parsed_json(p).isValid(), true, ,
|
|
|
|
repeat, volume, !justdata);
|
|
|
|
// (static alloc)
|
|
|
|
BEST_TIME("simdjson ", json_parse(p, pj), simdjson::SUCCESS, , repeat, volume,
|
|
|
|
!justdata);
|
2018-08-21 05:30:30 +08:00
|
|
|
|
|
|
|
rapidjson::Document d;
|
|
|
|
|
2018-12-01 09:27:16 +08:00
|
|
|
char *buffer = (char *)malloc(p.size() + 1);
|
2018-12-01 10:31:05 +08:00
|
|
|
memcpy(buffer, p.data(), p.size());
|
2018-12-01 09:27:16 +08:00
|
|
|
buffer[p.size()] = '\0';
|
2019-01-18 08:21:09 +08:00
|
|
|
#ifndef ALLPARSER
|
2019-05-25 08:08:06 +08:00
|
|
|
if (!justdata)
|
|
|
|
#endif
|
|
|
|
BEST_TIME(
|
|
|
|
"RapidJSON ", d.Parse<kParseValidateEncodingFlag>((const char *)buffer)
|
|
|
|
.HasParseError(),
|
|
|
|
false, memcpy(buffer, p.data(), p.size()), repeat, volume, !justdata);
|
2019-01-18 06:24:29 +08:00
|
|
|
BEST_TIME("RapidJSON (insitu)",
|
2018-12-12 23:19:32 +08:00
|
|
|
d.ParseInsitu<kParseValidateEncodingFlag>(buffer).HasParseError(),
|
2019-05-25 08:08:06 +08:00
|
|
|
false,
|
|
|
|
memcpy(buffer, p.data(), p.size()) && (buffer[p.size()] = '\0'),
|
|
|
|
repeat, volume, !justdata);
|
2019-01-18 08:21:09 +08:00
|
|
|
#ifndef ALLPARSER
|
2019-05-25 08:08:06 +08:00
|
|
|
if (!justdata)
|
|
|
|
#endif
|
|
|
|
BEST_TIME("sajson (dynamic mem)",
|
|
|
|
sajson::parse(sajson::dynamic_allocation(),
|
|
|
|
sajson::mutable_string_view(p.size(), buffer))
|
|
|
|
.is_valid(),
|
|
|
|
true, memcpy(buffer, p.data(), p.size()), repeat, volume,
|
|
|
|
!justdata);
|
2018-09-28 12:00:52 +08:00
|
|
|
|
2018-12-01 09:27:16 +08:00
|
|
|
size_t astbuffersize = p.size();
|
2018-12-12 23:19:32 +08:00
|
|
|
size_t *ast_buffer = (size_t *)malloc(astbuffersize * sizeof(size_t));
|
2018-12-19 11:18:23 +08:00
|
|
|
// (static alloc, insitu)
|
|
|
|
BEST_TIME("sajson",
|
2018-12-12 23:19:32 +08:00
|
|
|
sajson::parse(sajson::bounded_allocation(ast_buffer, astbuffersize),
|
|
|
|
sajson::mutable_string_view(p.size(), buffer))
|
|
|
|
.is_valid(),
|
2019-05-25 08:08:06 +08:00
|
|
|
true, memcpy(buffer, p.data(), p.size()), repeat, volume,
|
|
|
|
!justdata);
|
2018-12-19 13:40:04 +08:00
|
|
|
#ifdef ALLPARSER
|
2018-11-21 03:09:43 +08:00
|
|
|
std::string json11err;
|
2019-05-25 08:08:06 +08:00
|
|
|
BEST_TIME("dropbox (json11) ",
|
|
|
|
((json11::Json::parse(buffer, json11err).is_null()) ||
|
|
|
|
(!json11err.empty())),
|
|
|
|
false, memcpy(buffer, p.data(), p.size()), repeat, volume,
|
|
|
|
!justdata);
|
2018-09-28 12:00:52 +08:00
|
|
|
|
2019-05-25 08:08:06 +08:00
|
|
|
BEST_TIME("fastjson ", fastjson_parse(buffer), true,
|
|
|
|
memcpy(buffer, p.data(), p.size()), repeat, volume, !justdata);
|
2018-11-21 05:43:22 +08:00
|
|
|
JsonValue value;
|
|
|
|
JsonAllocator allocator;
|
|
|
|
char *endptr;
|
2019-05-25 08:08:06 +08:00
|
|
|
BEST_TIME("gason ", jsonParse(buffer, &endptr, &value, allocator),
|
|
|
|
JSON_OK, memcpy(buffer, p.data(), p.size()), repeat, volume,
|
|
|
|
!justdata);
|
2018-11-21 05:43:22 +08:00
|
|
|
void *state;
|
2019-05-25 08:08:06 +08:00
|
|
|
BEST_TIME("ultrajson ",
|
|
|
|
(UJDecode(buffer, p.size(), NULL, &state) == NULL), false,
|
|
|
|
memcpy(buffer, p.data(), p.size()), repeat, volume, !justdata);
|
2019-01-18 06:24:29 +08:00
|
|
|
|
2019-05-10 05:59:51 +08:00
|
|
|
{
|
2019-05-25 08:08:06 +08:00
|
|
|
std::unique_ptr<jsmntok_t[]> tokens =
|
|
|
|
std::make_unique<jsmntok_t[]>(p.size());
|
2019-01-18 06:24:29 +08:00
|
|
|
jsmn_parser parser;
|
|
|
|
jsmn_init(&parser);
|
|
|
|
memcpy(buffer, p.data(), p.size());
|
|
|
|
buffer[p.size()] = '\0';
|
2019-05-25 08:08:06 +08:00
|
|
|
BEST_TIME("jsmn ", (jsmn_parse(&parser, buffer, p.size(),
|
|
|
|
tokens.get(), p.size()) > 0),
|
|
|
|
true, jsmn_init(&parser), repeat, volume, !justdata);
|
2019-01-18 06:24:29 +08:00
|
|
|
}
|
|
|
|
memcpy(buffer, p.data(), p.size());
|
|
|
|
buffer[p.size()] = '\0';
|
2019-05-25 08:08:06 +08:00
|
|
|
cJSON *tree = cJSON_Parse(buffer);
|
|
|
|
BEST_TIME("cJSON ", ((tree = cJSON_Parse(buffer)) != NULL), true,
|
|
|
|
cJSON_Delete(tree), repeat, volume, !justdata);
|
2019-01-25 03:28:26 +08:00
|
|
|
cJSON_Delete(tree);
|
2019-05-25 08:08:06 +08:00
|
|
|
|
2019-01-25 03:28:26 +08:00
|
|
|
Json::CharReaderBuilder b;
|
2019-05-25 08:08:06 +08:00
|
|
|
Json::CharReader *jsoncppreader = b.newCharReader();
|
2019-01-25 03:28:26 +08:00
|
|
|
Json::Value root;
|
|
|
|
Json::String errs;
|
|
|
|
BEST_TIME("jsoncpp ",
|
2019-05-25 08:08:06 +08:00
|
|
|
jsoncppreader->parse(buffer, buffer + volume, &root, &errs), true, ,
|
|
|
|
repeat, volume, !justdata);
|
2019-01-25 03:28:26 +08:00
|
|
|
delete jsoncppreader;
|
2018-12-19 13:40:04 +08:00
|
|
|
#endif
|
2019-05-25 08:08:06 +08:00
|
|
|
if (!justdata)
|
|
|
|
BEST_TIME("memcpy ",
|
|
|
|
(memcpy(buffer, p.data(), p.size()) == buffer), true, , repeat,
|
|
|
|
volume, !justdata);
|
|
|
|
#ifdef __linux__
|
|
|
|
if (!justdata) {
|
|
|
|
printf("\n \n <doing additional analysis with performance counters (Linux "
|
|
|
|
"only)>\n");
|
|
|
|
std::vector<int> evts;
|
|
|
|
evts.push_back(PERF_COUNT_HW_CPU_CYCLES);
|
|
|
|
evts.push_back(PERF_COUNT_HW_INSTRUCTIONS);
|
|
|
|
evts.push_back(PERF_COUNT_HW_BRANCH_MISSES);
|
|
|
|
evts.push_back(PERF_COUNT_HW_CACHE_REFERENCES);
|
|
|
|
evts.push_back(PERF_COUNT_HW_CACHE_MISSES);
|
|
|
|
LinuxEvents<PERF_TYPE_HARDWARE> unified(evts);
|
|
|
|
std::vector<unsigned long long> results;
|
|
|
|
std::vector<unsigned long long> stats;
|
|
|
|
results.resize(evts.size());
|
|
|
|
stats.resize(evts.size());
|
|
|
|
std::fill(stats.begin(), stats.end(), 0); // unnecessary
|
|
|
|
for (int i = 0; i < repeat; i++) {
|
|
|
|
unified.start();
|
|
|
|
if (json_parse(p, pj) != simdjson::SUCCESS)
|
|
|
|
printf("bug\n");
|
|
|
|
unified.end(results);
|
|
|
|
std::transform(stats.begin(), stats.end(), results.begin(), stats.begin(),
|
|
|
|
std::plus<unsigned long long>());
|
|
|
|
}
|
|
|
|
printf("simdjson : cycles %10.0f instructions %10.0f branchmisses %10.0f "
|
|
|
|
"cacheref %10.0f cachemisses %10.0f bytespercachemiss %10.0f "
|
|
|
|
"inspercycle %10.1f insperbyte %10.1f\n",
|
|
|
|
stats[0] * 1.0 / repeat, stats[1] * 1.0 / repeat,
|
|
|
|
stats[2] * 1.0 / repeat, stats[3] * 1.0 / repeat,
|
|
|
|
stats[4] * 1.0 / repeat, volume * repeat * 1.0 / stats[2],
|
|
|
|
stats[1] * 1.0 / stats[0], stats[1] * 1.0 / (volume * repeat));
|
|
|
|
|
|
|
|
std::fill(stats.begin(), stats.end(), 0);
|
|
|
|
for (int i = 0; i < repeat; i++) {
|
|
|
|
memcpy(buffer, p.data(), p.size());
|
|
|
|
buffer[p.size()] = '\0';
|
|
|
|
unified.start();
|
|
|
|
if (d.ParseInsitu<kParseValidateEncodingFlag>(buffer).HasParseError() !=
|
|
|
|
false)
|
|
|
|
printf("bug\n");
|
|
|
|
unified.end(results);
|
|
|
|
std::transform(stats.begin(), stats.end(), results.begin(), stats.begin(),
|
|
|
|
std::plus<unsigned long long>());
|
|
|
|
}
|
|
|
|
printf("RapidJSON: cycles %10.0f instructions %10.0f branchmisses %10.0f "
|
|
|
|
"cacheref %10.0f cachemisses %10.0f bytespercachemiss %10.0f "
|
|
|
|
"inspercycle %10.1f insperbyte %10.1f\n",
|
|
|
|
stats[0] * 1.0 / repeat, stats[1] * 1.0 / repeat,
|
|
|
|
stats[2] * 1.0 / repeat, stats[3] * 1.0 / repeat,
|
|
|
|
stats[4] * 1.0 / repeat, volume * repeat * 1.0 / stats[2],
|
|
|
|
stats[1] * 1.0 / stats[0], stats[1] * 1.0 / (volume * repeat));
|
|
|
|
|
|
|
|
std::fill(stats.begin(), stats.end(), 0); // unnecessary
|
|
|
|
for (int i = 0; i < repeat; i++) {
|
|
|
|
memcpy(buffer, p.data(), p.size());
|
|
|
|
unified.start();
|
|
|
|
if (sajson::parse(sajson::bounded_allocation(ast_buffer, astbuffersize),
|
|
|
|
sajson::mutable_string_view(p.size(), buffer))
|
|
|
|
.is_valid() != true)
|
|
|
|
printf("bug\n");
|
|
|
|
unified.end(results);
|
|
|
|
std::transform(stats.begin(), stats.end(), results.begin(), stats.begin(),
|
|
|
|
std::plus<unsigned long long>());
|
|
|
|
}
|
|
|
|
printf("sajson : cycles %10.0f instructions %10.0f branchmisses %10.0f "
|
|
|
|
"cacheref %10.0f cachemisses %10.0f bytespercachemiss %10.0f "
|
|
|
|
"inspercycle %10.1f insperbyte %10.1f\n",
|
|
|
|
stats[0] * 1.0 / repeat, stats[1] * 1.0 / repeat,
|
|
|
|
stats[2] * 1.0 / repeat, stats[3] * 1.0 / repeat,
|
|
|
|
stats[4] * 1.0 / repeat, volume * repeat * 1.0 / stats[2],
|
|
|
|
stats[1] * 1.0 / stats[0], stats[1] * 1.0 / (volume * repeat));
|
|
|
|
}
|
|
|
|
#endif // __linux__
|
|
|
|
|
2018-09-28 12:00:52 +08:00
|
|
|
free(ast_buffer);
|
2018-11-30 11:15:02 +08:00
|
|
|
free(buffer);
|
2018-08-21 05:30:30 +08:00
|
|
|
}
|