1c5f01b2fSopenharmony_ci// __ _____ _____ _____ 2c5f01b2fSopenharmony_ci// __| | __| | | | JSON for Modern C++ (supporting code) 3c5f01b2fSopenharmony_ci// | | |__ | | | | | | version 3.11.2 4c5f01b2fSopenharmony_ci// |_____|_____|_____|_|___| https://github.com/nlohmann/json 5c5f01b2fSopenharmony_ci// 6c5f01b2fSopenharmony_ci// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me> 7c5f01b2fSopenharmony_ci// SPDX-License-Identifier: MIT 8c5f01b2fSopenharmony_ci 9c5f01b2fSopenharmony_ci#include <benchmark/benchmark.h> 10c5f01b2fSopenharmony_ci#include <nlohmann/json.hpp> 11c5f01b2fSopenharmony_ci#include <fstream> 12c5f01b2fSopenharmony_ci#include <numeric> 13c5f01b2fSopenharmony_ci#include <vector> 14c5f01b2fSopenharmony_ci#include <test_data.hpp> 15c5f01b2fSopenharmony_ci 16c5f01b2fSopenharmony_ciusing json = nlohmann::json; 17c5f01b2fSopenharmony_ci 18c5f01b2fSopenharmony_ci////////////////////////////////////////////////////////////////////////////// 19c5f01b2fSopenharmony_ci// parse JSON from file 20c5f01b2fSopenharmony_ci////////////////////////////////////////////////////////////////////////////// 21c5f01b2fSopenharmony_ci 22c5f01b2fSopenharmony_cistatic void ParseFile(benchmark::State& state, const char* filename) 23c5f01b2fSopenharmony_ci{ 24c5f01b2fSopenharmony_ci while (state.KeepRunning()) 25c5f01b2fSopenharmony_ci { 26c5f01b2fSopenharmony_ci state.PauseTiming(); 27c5f01b2fSopenharmony_ci auto* f = new std::ifstream(filename); 28c5f01b2fSopenharmony_ci auto* j = new json(); 29c5f01b2fSopenharmony_ci state.ResumeTiming(); 30c5f01b2fSopenharmony_ci 31c5f01b2fSopenharmony_ci *j = json::parse(*f); 32c5f01b2fSopenharmony_ci 33c5f01b2fSopenharmony_ci state.PauseTiming(); 34c5f01b2fSopenharmony_ci delete f; 35c5f01b2fSopenharmony_ci delete j; 36c5f01b2fSopenharmony_ci state.ResumeTiming(); 37c5f01b2fSopenharmony_ci } 38c5f01b2fSopenharmony_ci 39c5f01b2fSopenharmony_ci std::ifstream file(filename, std::ios::binary | std::ios::ate); 40c5f01b2fSopenharmony_ci state.SetBytesProcessed(state.iterations() * file.tellg()); 41c5f01b2fSopenharmony_ci} 42c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseFile, jeopardy, TEST_DATA_DIRECTORY "/jeopardy/jeopardy.json"); 43c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseFile, canada, TEST_DATA_DIRECTORY "/nativejson-benchmark/canada.json"); 44c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseFile, citm_catalog, TEST_DATA_DIRECTORY "/nativejson-benchmark/citm_catalog.json"); 45c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseFile, twitter, TEST_DATA_DIRECTORY "/nativejson-benchmark/twitter.json"); 46c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseFile, floats, TEST_DATA_DIRECTORY "/regression/floats.json"); 47c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseFile, signed_ints, TEST_DATA_DIRECTORY "/regression/signed_ints.json"); 48c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseFile, unsigned_ints, TEST_DATA_DIRECTORY "/regression/unsigned_ints.json"); 49c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseFile, small_signed_ints, TEST_DATA_DIRECTORY "/regression/small_signed_ints.json"); 50c5f01b2fSopenharmony_ci 51c5f01b2fSopenharmony_ci////////////////////////////////////////////////////////////////////////////// 52c5f01b2fSopenharmony_ci// parse JSON from string 53c5f01b2fSopenharmony_ci////////////////////////////////////////////////////////////////////////////// 54c5f01b2fSopenharmony_ci 55c5f01b2fSopenharmony_cistatic void ParseString(benchmark::State& state, const char* filename) 56c5f01b2fSopenharmony_ci{ 57c5f01b2fSopenharmony_ci std::ifstream f(filename); 58c5f01b2fSopenharmony_ci std::string str((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>()); 59c5f01b2fSopenharmony_ci 60c5f01b2fSopenharmony_ci while (state.KeepRunning()) 61c5f01b2fSopenharmony_ci { 62c5f01b2fSopenharmony_ci state.PauseTiming(); 63c5f01b2fSopenharmony_ci auto* j = new json(); 64c5f01b2fSopenharmony_ci state.ResumeTiming(); 65c5f01b2fSopenharmony_ci 66c5f01b2fSopenharmony_ci *j = json::parse(str); 67c5f01b2fSopenharmony_ci 68c5f01b2fSopenharmony_ci state.PauseTiming(); 69c5f01b2fSopenharmony_ci delete j; 70c5f01b2fSopenharmony_ci state.ResumeTiming(); 71c5f01b2fSopenharmony_ci } 72c5f01b2fSopenharmony_ci 73c5f01b2fSopenharmony_ci state.SetBytesProcessed(state.iterations() * str.size()); 74c5f01b2fSopenharmony_ci} 75c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseString, jeopardy, TEST_DATA_DIRECTORY "/jeopardy/jeopardy.json"); 76c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseString, canada, TEST_DATA_DIRECTORY "/nativejson-benchmark/canada.json"); 77c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseString, citm_catalog, TEST_DATA_DIRECTORY "/nativejson-benchmark/citm_catalog.json"); 78c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseString, twitter, TEST_DATA_DIRECTORY "/nativejson-benchmark/twitter.json"); 79c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseString, floats, TEST_DATA_DIRECTORY "/regression/floats.json"); 80c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseString, signed_ints, TEST_DATA_DIRECTORY "/regression/signed_ints.json"); 81c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseString, unsigned_ints, TEST_DATA_DIRECTORY "/regression/unsigned_ints.json"); 82c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ParseString, small_signed_ints, TEST_DATA_DIRECTORY "/regression/small_signed_ints.json"); 83c5f01b2fSopenharmony_ci 84c5f01b2fSopenharmony_ci 85c5f01b2fSopenharmony_ci////////////////////////////////////////////////////////////////////////////// 86c5f01b2fSopenharmony_ci// serialize JSON 87c5f01b2fSopenharmony_ci////////////////////////////////////////////////////////////////////////////// 88c5f01b2fSopenharmony_ci 89c5f01b2fSopenharmony_cistatic void Dump(benchmark::State& state, const char* filename, int indent) 90c5f01b2fSopenharmony_ci{ 91c5f01b2fSopenharmony_ci std::ifstream f(filename); 92c5f01b2fSopenharmony_ci std::string str((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>()); 93c5f01b2fSopenharmony_ci json j = json::parse(str); 94c5f01b2fSopenharmony_ci 95c5f01b2fSopenharmony_ci while (state.KeepRunning()) 96c5f01b2fSopenharmony_ci { 97c5f01b2fSopenharmony_ci j.dump(indent); 98c5f01b2fSopenharmony_ci } 99c5f01b2fSopenharmony_ci 100c5f01b2fSopenharmony_ci state.SetBytesProcessed(state.iterations() * j.dump(indent).size()); 101c5f01b2fSopenharmony_ci} 102c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, jeopardy / -, TEST_DATA_DIRECTORY "/jeopardy/jeopardy.json", -1); 103c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, jeopardy / 4, TEST_DATA_DIRECTORY "/jeopardy/jeopardy.json", 4); 104c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, canada / -, TEST_DATA_DIRECTORY "/nativejson-benchmark/canada.json", -1); 105c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, canada / 4, TEST_DATA_DIRECTORY "/nativejson-benchmark/canada.json", 4); 106c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, citm_catalog / -, TEST_DATA_DIRECTORY "/nativejson-benchmark/citm_catalog.json", -1); 107c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, citm_catalog / 4, TEST_DATA_DIRECTORY "/nativejson-benchmark/citm_catalog.json", 4); 108c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, twitter / -, TEST_DATA_DIRECTORY "/nativejson-benchmark/twitter.json", -1); 109c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, twitter / 4, TEST_DATA_DIRECTORY "/nativejson-benchmark/twitter.json", 4); 110c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, floats / -, TEST_DATA_DIRECTORY "/regression/floats.json", -1); 111c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, floats / 4, TEST_DATA_DIRECTORY "/regression/floats.json", 4); 112c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, signed_ints / -, TEST_DATA_DIRECTORY "/regression/signed_ints.json", -1); 113c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, signed_ints / 4, TEST_DATA_DIRECTORY "/regression/signed_ints.json", 4); 114c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, unsigned_ints / -, TEST_DATA_DIRECTORY "/regression/unsigned_ints.json", -1); 115c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, unsigned_ints / 4, TEST_DATA_DIRECTORY "/regression/unsigned_ints.json", 4); 116c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, small_signed_ints / -, TEST_DATA_DIRECTORY "/regression/small_signed_ints.json", -1); 117c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(Dump, small_signed_ints / 4, TEST_DATA_DIRECTORY "/regression/small_signed_ints.json", 4); 118c5f01b2fSopenharmony_ci 119c5f01b2fSopenharmony_ci 120c5f01b2fSopenharmony_ci////////////////////////////////////////////////////////////////////////////// 121c5f01b2fSopenharmony_ci// serialize CBOR 122c5f01b2fSopenharmony_ci////////////////////////////////////////////////////////////////////////////// 123c5f01b2fSopenharmony_cistatic void ToCbor(benchmark::State& state, const char* filename) 124c5f01b2fSopenharmony_ci{ 125c5f01b2fSopenharmony_ci std::ifstream f(filename); 126c5f01b2fSopenharmony_ci std::string str((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>()); 127c5f01b2fSopenharmony_ci json j = json::parse(str); 128c5f01b2fSopenharmony_ci 129c5f01b2fSopenharmony_ci while (state.KeepRunning()) 130c5f01b2fSopenharmony_ci { 131c5f01b2fSopenharmony_ci json::to_cbor(j); 132c5f01b2fSopenharmony_ci } 133c5f01b2fSopenharmony_ci 134c5f01b2fSopenharmony_ci state.SetBytesProcessed(state.iterations() * json::to_cbor(j).size()); 135c5f01b2fSopenharmony_ci} 136c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ToCbor, jeopardy, TEST_DATA_DIRECTORY "/jeopardy/jeopardy.json"); 137c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ToCbor, canada, TEST_DATA_DIRECTORY "/nativejson-benchmark/canada.json"); 138c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ToCbor, citm_catalog, TEST_DATA_DIRECTORY "/nativejson-benchmark/citm_catalog.json"); 139c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ToCbor, twitter, TEST_DATA_DIRECTORY "/nativejson-benchmark/twitter.json"); 140c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ToCbor, floats, TEST_DATA_DIRECTORY "/regression/floats.json"); 141c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ToCbor, signed_ints, TEST_DATA_DIRECTORY "/regression/signed_ints.json"); 142c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ToCbor, unsigned_ints, TEST_DATA_DIRECTORY "/regression/unsigned_ints.json"); 143c5f01b2fSopenharmony_ciBENCHMARK_CAPTURE(ToCbor, small_signed_ints, TEST_DATA_DIRECTORY "/regression/small_signed_ints.json"); 144c5f01b2fSopenharmony_ci 145c5f01b2fSopenharmony_ci////////////////////////////////////////////////////////////////////////////// 146c5f01b2fSopenharmony_ci// serialize binary CBOR 147c5f01b2fSopenharmony_ci////////////////////////////////////////////////////////////////////////////// 148c5f01b2fSopenharmony_cistatic void BinaryToCbor(benchmark::State& state) 149c5f01b2fSopenharmony_ci{ 150c5f01b2fSopenharmony_ci std::vector<uint8_t> data(256); 151c5f01b2fSopenharmony_ci std::iota(data.begin(), data.end(), 0); 152c5f01b2fSopenharmony_ci 153c5f01b2fSopenharmony_ci auto it = data.begin(); 154c5f01b2fSopenharmony_ci std::vector<uint8_t> in; 155c5f01b2fSopenharmony_ci in.reserve(state.range(0)); 156c5f01b2fSopenharmony_ci for (int i = 0; i < state.range(0); ++i) 157c5f01b2fSopenharmony_ci { 158c5f01b2fSopenharmony_ci if (it == data.end()) 159c5f01b2fSopenharmony_ci { 160c5f01b2fSopenharmony_ci it = data.begin(); 161c5f01b2fSopenharmony_ci } 162c5f01b2fSopenharmony_ci 163c5f01b2fSopenharmony_ci in.push_back(*it); 164c5f01b2fSopenharmony_ci ++it; 165c5f01b2fSopenharmony_ci } 166c5f01b2fSopenharmony_ci 167c5f01b2fSopenharmony_ci json::binary_t bin{in}; 168c5f01b2fSopenharmony_ci json j{{"type", "binary"}, {"data", bin}}; 169c5f01b2fSopenharmony_ci 170c5f01b2fSopenharmony_ci while (state.KeepRunning()) 171c5f01b2fSopenharmony_ci { 172c5f01b2fSopenharmony_ci json::to_cbor(j); 173c5f01b2fSopenharmony_ci } 174c5f01b2fSopenharmony_ci 175c5f01b2fSopenharmony_ci state.SetBytesProcessed(state.iterations() * json::to_cbor(j).size()); 176c5f01b2fSopenharmony_ci} 177c5f01b2fSopenharmony_ciBENCHMARK(BinaryToCbor)->RangeMultiplier(2)->Range(8, 8 << 12); 178c5f01b2fSopenharmony_ci 179c5f01b2fSopenharmony_ciBENCHMARK_MAIN(); 180