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