|
27 | 27 | namespace jsonbench
|
28 | 28 | {
|
29 | 29 |
|
30 |
| -std::size_t max_marks = 2; |
| 30 | +// |
| 31 | +// Default benchmark settings |
| 32 | +// |
| 33 | +std::size_t max_marks = 10; |
31 | 34 | std::size_t max_iterations = 1000;
|
32 | 35 |
|
| 36 | +// |
| 37 | +// Routines handling test data files |
| 38 | +// |
| 39 | +typedef std::vector<std::string> jsons_t; |
| 40 | + |
| 41 | +inline jsons_t load_json(std::string file) |
| 42 | +{ |
| 43 | + typedef std::string::value_type char_t; |
| 44 | + typedef std::istreambuf_iterator<char_t> iterator_t; |
| 45 | + |
| 46 | + jsons_t v; |
| 47 | + std::ifstream ifs(file); |
| 48 | + v.push_back(std::string(iterator_t(ifs), (iterator_t()))); |
| 49 | + return v; |
| 50 | +} |
| 51 | + |
| 52 | +inline jsons_t load_jsons(std::string file) |
| 53 | +{ |
| 54 | + jsons_t v; |
| 55 | + std::ifstream ifs(file); |
| 56 | + for (std::string line; std::getline(ifs, line); ) |
| 57 | + v.push_back(line); |
| 58 | + return v; |
| 59 | +} |
| 60 | + |
| 61 | +// load single large JSON string |
| 62 | +inline jsons_t get_large() |
| 63 | +{ |
| 64 | + return load_json("data/canada.json"); |
| 65 | +} |
| 66 | + |
| 67 | +// load collection of small to medium size JSON strings |
| 68 | +inline jsons_t get_small() |
| 69 | +{ |
| 70 | + return load_jsons("data/one-json-per-line.jsons"); |
| 71 | +} |
| 72 | + |
| 73 | +// |
| 74 | +// Benchmark running routines |
| 75 | +// |
33 | 76 | typedef std::tuple<std::size_t, std::size_t, std::size_t, double, double> result_t;
|
34 | 77 |
|
35 | 78 | template <typename Result>
|
@@ -81,35 +124,17 @@ inline result_t benchmark(Container const& jsons, Parse parse)
|
81 | 124 | return benchmark(max_marks, max_iterations, jsons, parse);
|
82 | 125 | }
|
83 | 126 |
|
84 |
| -typedef std::vector<std::string> jsons_t; |
85 |
| - |
86 |
| -inline jsons_t load_json(std::string file) |
87 |
| -{ |
88 |
| - typedef std::string::value_type char_t; |
89 |
| - typedef std::istreambuf_iterator<char_t> iterator_t; |
90 |
| - |
91 |
| - jsons_t v; |
92 |
| - std::ifstream ifs(file); |
93 |
| - v.push_back(std::string(iterator_t(ifs), (iterator_t()))); |
94 |
| - return v; |
95 |
| -} |
96 |
| - |
97 |
| -inline jsons_t load_jsons(std::string file) |
98 |
| -{ |
99 |
| - jsons_t v; |
100 |
| - std::ifstream ifs(file); |
101 |
| - for (std::string line; std::getline(ifs, line); ) |
102 |
| - v.push_back(line); |
103 |
| - return v; |
104 |
| -} |
105 |
| - |
106 |
| -inline jsons_t get_json() |
107 |
| -{ |
108 |
| - return load_json("data/canada.json"); |
109 |
| -} |
110 |
| -inline jsons_t get_one_json_per_line() |
| 127 | +template <typename Parse> |
| 128 | +inline void run_benchmark(char const* name, Parse parse) |
111 | 129 | {
|
112 |
| - return load_jsons("data/one-json-per-line.txt"); |
| 130 | + { |
| 131 | + auto const marks = benchmark(max_marks, max_iterations, get_small(), parse); |
| 132 | + print_result(std::cout << name << ".small: ", marks); |
| 133 | + } |
| 134 | + { |
| 135 | + auto const marks = benchmark(max_marks, max_iterations, get_large(), parse); |
| 136 | + print_result(std::cout << name << ".large: ", marks); |
| 137 | + } |
113 | 138 | }
|
114 | 139 |
|
115 | 140 | } // namespace jsonbench
|
|
0 commit comments