simdjson/benchmark/parsingcompetition.cpp

92 lines
3.1 KiB
C++
Raw Normal View History

2018-08-21 05:30:30 +08:00
#include "jsonparser/jsonparser.h"
2018-08-21 05:30:30 +08:00
#include "benchmark.h"
2018-08-21 05:30:30 +08:00
// #define RAPIDJSON_SSE2 // bad
// #define RAPIDJSON_SSE42 // bad
#include "rapidjson/document.h"
#include "rapidjson/reader.h" // you have to check in the submodule
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
2018-11-21 03:09:43 +08:00
#include "json11.cpp"
2018-09-28 12:00:52 +08:00
#include "sajson.h"
2018-11-21 03:32:12 +08:00
#include "fastjson.cpp"
#include "fastjson_dom.cpp"
2018-09-28 12:00:52 +08:00
2018-08-21 05:30:30 +08:00
using namespace rapidjson;
using namespace std;
2018-11-21 03:32:12 +08:00
// fastjson has a tricky interface
void on_json_error( void *, const fastjson::ErrorContext& ec) {
std::cerr<<"ERROR: "<<ec.mesg<<std::endl;
}
bool fastjson_parse(const char *input) {
fastjson::Token token;
fastjson::dom::Chunk chunk;
std::string error_message;
return fastjson::dom::parse_string(input, &token, &chunk, 0, &on_json_error, NULL);
}
2018-08-21 05:30:30 +08:00
int main(int argc, char *argv[]) {
if (argc < 2) {
cerr << "Usage: " << argv[0] << " <jsonfile>\n";
cerr << "Or " << argv[0] << " -v <jsonfile>\n";
exit(1);
}
bool verbose = false;
if (argc > 2) {
if (strcmp(argv[1], "-v"))
verbose = true;
}
pair<u8 *, size_t> p = get_corpus(argv[argc - 1]);
2018-08-21 05:40:50 +08:00
if (verbose) {
std::cout << "Input has ";
if (p.second > 1024 * 1024)
std::cout << p.second / (1024 * 1024) << " MB ";
else if (p.second > 1024)
std::cout << p.second / 1024 << " KB ";
else
std::cout << p.second << " B ";
std::cout << std::endl;
}
2018-08-21 05:30:30 +08:00
ParsedJson *pj_ptr = allocate_ParsedJson(p.second);
if (pj_ptr == NULL) {
std::cerr << "can't allocate memory" << std::endl;
return EXIT_FAILURE;
}
ParsedJson &pj(*pj_ptr);
int repeat = 10;
int volume = p.second;
2018-11-21 03:32:12 +08:00
BEST_TIME("simdjson", json_parse(p.first, p.second, pj), true, , repeat, volume, true);
2018-08-21 05:30:30 +08:00
rapidjson::Document d;
char *buffer = (char *)malloc(p.second + 1);
memcpy(buffer, p.first, p.second);
buffer[p.second] = '\0';
BEST_TIME("RapidJSON",
2018-08-21 05:30:30 +08:00
d.Parse<kParseValidateEncodingFlag>((const char *)buffer).HasParseError(),
false, memcpy(buffer, p.first, p.second), repeat, volume, true);
BEST_TIME("RapidJSON Insitu", d.ParseInsitu<kParseValidateEncodingFlag>(buffer).HasParseError(), false,
2018-08-21 05:30:30 +08:00
memcpy(buffer, p.first, p.second), repeat, volume, true);
BEST_TIME("sajson (dynamic mem)", sajson::parse(sajson::dynamic_allocation(), sajson::mutable_string_view(p.second, buffer)).is_valid(), true, memcpy(buffer, p.first, p.second), repeat, volume, true);
2018-09-28 12:00:52 +08:00
size_t astbuffersize = p.second;
size_t * ast_buffer = (size_t *) malloc(astbuffersize * sizeof(size_t));
BEST_TIME("sajson (static alloc)", sajson::parse(sajson::bounded_allocation(ast_buffer, astbuffersize), sajson::mutable_string_view(p.second, buffer)).is_valid(), true, memcpy(buffer, p.first, p.second), repeat, volume, true);
2018-11-21 03:09:43 +08:00
std::string json11err;
BEST_TIME("dropbox (json11) ", json11::Json::parse(buffer,json11err).is_null(), false, memcpy(buffer, p.first, p.second), repeat, volume, true);
2018-09-28 12:00:52 +08:00
2018-11-21 03:32:12 +08:00
BEST_TIME("fastjson ", fastjson_parse(buffer), true, memcpy(buffer, p.first, p.second), repeat, volume, true);
2018-08-21 05:30:30 +08:00
free(p.first);
2018-09-28 12:00:52 +08:00
free(ast_buffer);
2018-08-21 05:30:30 +08:00
deallocate_ParsedJson(pj_ptr);
}