Skip to content

Commit b2a315c

Browse files
committed
Refactored to simplify benchmark definition into single function call
1 parent 7b46b2b commit b2a315c

File tree

2 files changed

+57
-36
lines changed

2 files changed

+57
-36
lines changed

benchmark_spirit.cpp

+3-7
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,20 @@
88
#include "json_benchmark.hpp"
99
#include <ciere/json/value.hpp>
1010
#include <ciere/json/io.hpp>
11-
namespace jb = jsonbench;
1211
namespace cj = ciere::json;
1312
int main()
1413
{
1514
try
1615
{
17-
//auto const jsons = jb::get_one_json_per_line();
18-
auto const jsons = jb::get_json();
19-
auto const marks = jb::benchmark(jsons, [](std::string const& s) {
16+
jsonbench::run_benchmark("spirit", [](std::string const& s) {
2017
cj::value jv;
21-
bool parsed = cj::construct(s, jv);
18+
bool const parsed = cj::construct(s, jv);
2219
#ifdef JSON_BENCHMARK_DUMP_PARSED_JSON
2320
std::cout << jv << std::endl;
2421
#endif
2522
return parsed && jv.type() != cj::null_type;
2623
});
27-
28-
jb::print_result(std::cout << "spirit: ", marks);
24+
2925
return EXIT_SUCCESS;
3026
}
3127
catch (std::exception const& e)

json_benchmark.hpp

+54-29
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,52 @@
2727
namespace jsonbench
2828
{
2929

30-
std::size_t max_marks = 2;
30+
//
31+
// Default benchmark settings
32+
//
33+
std::size_t max_marks = 10;
3134
std::size_t max_iterations = 1000;
3235

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+
//
3376
typedef std::tuple<std::size_t, std::size_t, std::size_t, double, double> result_t;
3477

3578
template <typename Result>
@@ -81,35 +124,17 @@ inline result_t benchmark(Container const& jsons, Parse parse)
81124
return benchmark(max_marks, max_iterations, jsons, parse);
82125
}
83126

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)
111129
{
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+
}
113138
}
114139

115140
} // namespace jsonbench

0 commit comments

Comments
 (0)