1c5f01b2fSopenharmony_ci#include "benchmark/benchmark.h" 2c5f01b2fSopenharmony_ci#include <nlohmann/json.hpp> 3c5f01b2fSopenharmony_ci#include <fstream> 4c5f01b2fSopenharmony_ci#include <test_data.hpp> 5c5f01b2fSopenharmony_ci 6c5f01b2fSopenharmony_ciusing json = nlohmann::json; 7c5f01b2fSopenharmony_ci 8c5f01b2fSopenharmony_ci////////////////////////////////////////////////////////////////////////////// 9c5f01b2fSopenharmony_ci// parse JSON from file 10c5f01b2fSopenharmony_ci////////////////////////////////////////////////////////////////////////////// 11c5f01b2fSopenharmony_ci 12c5f01b2fSopenharmony_cistatic void ParseFile(benchmark::State& state, const char* filename) 13c5f01b2fSopenharmony_ci{ 14c5f01b2fSopenharmony_ci while (state.KeepRunning()) 15c5f01b2fSopenharmony_ci { 16c5f01b2fSopenharmony_ci state.PauseTiming(); 17c5f01b2fSopenharmony_ci auto* f = new std::ifstream(filename); 18c5f01b2fSopenharmony_ci auto* j = new json(); 19c5f01b2fSopenharmony_ci state.ResumeTiming(); 20c5f01b2fSopenharmony_ci 21c5f01b2fSopenharmony_ci *j = json::parse(*f); 22c5f01b2fSopenharmony_ci 23c5f01b2fSopenharmony_ci state.PauseTiming(); 24c5f01b2fSopenharmony_ci delete f; 25c5f01b2fSopenharmony_ci delete j; 26c5f01b2fSopenharmony_ci state.ResumeTiming(); 27c5f01b2fSopenharmony_ci } 28c5f01b2fSopenharmony_ci 29c5f01b2fSopenharmony_ci std::ifstream file(filename, std::ios::binary | std::ios::ate); 30c5f01b2fSopenharmony_ci state.SetBytesProcessed(state.iterations() * file.tellg()); 31c5f01b2fSopenharmony_ci} 32c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseFile, jeopardy, TEST_DATA_DIRECTORY "/jeopardy/jeopardy.json"); 33c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseFile, canada, TEST_DATA_DIRECTORY "/nativejson-benchmark/canada.json"); 34c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseFile, citm_catalog, TEST_DATA_DIRECTORY "/nativejson-benchmark/citm_catalog.json"); 35c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseFile, twitter, TEST_DATA_DIRECTORY "/nativejson-benchmark/twitter.json"); 36c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseFile, floats, TEST_DATA_DIRECTORY "/regression/floats.json"); 37c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseFile, signed_ints, TEST_DATA_DIRECTORY "/regression/signed_ints.json"); 38c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseFile, unsigned_ints, TEST_DATA_DIRECTORY "/regression/unsigned_ints.json"); 39c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseFile, small_signed_ints, TEST_DATA_DIRECTORY "/regression/small_signed_ints.json"); 40c5f01b2fSopenharmony_ci 41c5f01b2fSopenharmony_ci////////////////////////////////////////////////////////////////////////////// 42c5f01b2fSopenharmony_ci// parse JSON from string 43c5f01b2fSopenharmony_ci////////////////////////////////////////////////////////////////////////////// 44c5f01b2fSopenharmony_ci 45c5f01b2fSopenharmony_cistatic void ParseString(benchmark::State& state, const char* filename) 46c5f01b2fSopenharmony_ci{ 47c5f01b2fSopenharmony_ci std::ifstream f(filename); 48c5f01b2fSopenharmony_ci std::string str((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>()); 49c5f01b2fSopenharmony_ci 50c5f01b2fSopenharmony_ci while (state.KeepRunning()) 51c5f01b2fSopenharmony_ci { 52c5f01b2fSopenharmony_ci state.PauseTiming(); 53c5f01b2fSopenharmony_ci auto* j = new json(); 54c5f01b2fSopenharmony_ci state.ResumeTiming(); 55c5f01b2fSopenharmony_ci 56c5f01b2fSopenharmony_ci *j = json::parse(str); 57c5f01b2fSopenharmony_ci 58c5f01b2fSopenharmony_ci state.PauseTiming(); 59c5f01b2fSopenharmony_ci delete j; 60c5f01b2fSopenharmony_ci state.ResumeTiming(); 61c5f01b2fSopenharmony_ci } 62c5f01b2fSopenharmony_ci 63c5f01b2fSopenharmony_ci state.SetBytesProcessed(state.iterations() * str.size()); 64c5f01b2fSopenharmony_ci} 65c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseString, jeopardy, TEST_DATA_DIRECTORY "/jeopardy/jeopardy.json"); 66c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseString, canada, TEST_DATA_DIRECTORY "/nativejson-benchmark/canada.json"); 67c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseString, citm_catalog, TEST_DATA_DIRECTORY "/nativejson-benchmark/citm_catalog.json"); 68c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseString, twitter, TEST_DATA_DIRECTORY "/nativejson-benchmark/twitter.json"); 69c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseString, floats, TEST_DATA_DIRECTORY "/regression/floats.json"); 70c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseString, signed_ints, TEST_DATA_DIRECTORY "/regression/signed_ints.json"); 71c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseString, unsigned_ints, TEST_DATA_DIRECTORY "/regression/unsigned_ints.json"); 72c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseString, small_signed_ints, TEST_DATA_DIRECTORY "/regression/small_signed_ints.json"); 73c5f01b2fSopenharmony_ci 74c5f01b2fSopenharmony_ci 75c5f01b2fSopenharmony_ci////////////////////////////////////////////////////////////////////////////// 76c5f01b2fSopenharmony_ci// serialize JSON 77c5f01b2fSopenharmony_ci////////////////////////////////////////////////////////////////////////////// 78c5f01b2fSopenharmony_ci 79c5f01b2fSopenharmony_cistatic void Dump(benchmark::State& state, const char* filename, int indent) 80c5f01b2fSopenharmony_ci{ 81c5f01b2fSopenharmony_ci std::ifstream f(filename); 82c5f01b2fSopenharmony_ci std::string str((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>()); 83c5f01b2fSopenharmony_ci json j = json::parse(str); 84c5f01b2fSopenharmony_ci 85c5f01b2fSopenharmony_ci while (state.KeepRunning()) 86c5f01b2fSopenharmony_ci { 87c5f01b2fSopenharmony_ci j.dump(indent); 88c5f01b2fSopenharmony_ci } 89c5f01b2fSopenharmony_ci 90c5f01b2fSopenharmony_ci state.SetBytesProcessed(state.iterations() * j.dump(indent).size()); 91c5f01b2fSopenharmony_ci} 92c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, jeopardy / -, TEST_DATA_DIRECTORY "/jeopardy/jeopardy.json", -1); 93c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, jeopardy / 4, TEST_DATA_DIRECTORY "/jeopardy/jeopardy.json", 4); 94c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, canada / -, TEST_DATA_DIRECTORY "/nativejson-benchmark/canada.json", -1); 95c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, canada / 4, TEST_DATA_DIRECTORY "/nativejson-benchmark/canada.json", 4); 96c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, citm_catalog / -, TEST_DATA_DIRECTORY "/nativejson-benchmark/citm_catalog.json", -1); 97c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, citm_catalog / 4, TEST_DATA_DIRECTORY "/nativejson-benchmark/citm_catalog.json", 4); 98c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, twitter / -, TEST_DATA_DIRECTORY "/nativejson-benchmark/twitter.json", -1); 99c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, twitter / 4, TEST_DATA_DIRECTORY "/nativejson-benchmark/twitter.json", 4); 100c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, floats / -, TEST_DATA_DIRECTORY "/regression/floats.json", -1); 101c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, floats / 4, TEST_DATA_DIRECTORY "/regression/floats.json", 4); 102c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, signed_ints / -, TEST_DATA_DIRECTORY "/regression/signed_ints.json", -1); 103c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, signed_ints / 4, TEST_DATA_DIRECTORY "/regression/signed_ints.json", 4); 104c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, unsigned_ints / -, TEST_DATA_DIRECTORY "/regression/unsigned_ints.json", -1); 105c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, unsigned_ints / 4, TEST_DATA_DIRECTORY "/regression/unsigned_ints.json", 4); 106c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, small_signed_ints / -, TEST_DATA_DIRECTORY "/regression/small_signed_ints.json", -1); 107c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, small_signed_ints / 4, TEST_DATA_DIRECTORY "/regression/small_signed_ints.json", 4); 108c5f01b2fSopenharmony_ci 109c5f01b2fSopenharmony_ci 110c5f01b2fSopenharmony_ciBENCHMARK_MAIN(); 111