Skip to content

Commit

Permalink
Refactored to simplify benchmark definition into single function call
Browse files Browse the repository at this point in the history
  • Loading branch information
mloskot committed Apr 17, 2013
1 parent 7b46b2b commit b2a315c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 36 deletions.
10 changes: 3 additions & 7 deletions benchmark_spirit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,20 @@
#include "json_benchmark.hpp"
#include <ciere/json/value.hpp>
#include <ciere/json/io.hpp>
namespace jb = jsonbench;
namespace cj = ciere::json;
int main()
{
try
{
//auto const jsons = jb::get_one_json_per_line();
auto const jsons = jb::get_json();
auto const marks = jb::benchmark(jsons, [](std::string const& s) {
jsonbench::run_benchmark("spirit", [](std::string const& s) {
cj::value jv;
bool parsed = cj::construct(s, jv);
bool const parsed = cj::construct(s, jv);
#ifdef JSON_BENCHMARK_DUMP_PARSED_JSON
std::cout << jv << std::endl;
#endif
return parsed && jv.type() != cj::null_type;
});

jb::print_result(std::cout << "spirit: ", marks);

return EXIT_SUCCESS;
}
catch (std::exception const& e)
Expand Down
83 changes: 54 additions & 29 deletions json_benchmark.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,52 @@
namespace jsonbench
{

std::size_t max_marks = 2;
//
// Default benchmark settings
//
std::size_t max_marks = 10;
std::size_t max_iterations = 1000;

//
// Routines handling test data files
//
typedef std::vector<std::string> jsons_t;

inline jsons_t load_json(std::string file)
{
typedef std::string::value_type char_t;
typedef std::istreambuf_iterator<char_t> iterator_t;

jsons_t v;
std::ifstream ifs(file);
v.push_back(std::string(iterator_t(ifs), (iterator_t())));
return v;
}

inline jsons_t load_jsons(std::string file)
{
jsons_t v;
std::ifstream ifs(file);
for (std::string line; std::getline(ifs, line); )
v.push_back(line);
return v;
}

// load single large JSON string
inline jsons_t get_large()
{
return load_json("data/canada.json");
}

// load collection of small to medium size JSON strings
inline jsons_t get_small()
{
return load_jsons("data/one-json-per-line.jsons");
}

//
// Benchmark running routines
//
typedef std::tuple<std::size_t, std::size_t, std::size_t, double, double> result_t;

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

typedef std::vector<std::string> jsons_t;

inline jsons_t load_json(std::string file)
{
typedef std::string::value_type char_t;
typedef std::istreambuf_iterator<char_t> iterator_t;

jsons_t v;
std::ifstream ifs(file);
v.push_back(std::string(iterator_t(ifs), (iterator_t())));
return v;
}

inline jsons_t load_jsons(std::string file)
{
jsons_t v;
std::ifstream ifs(file);
for (std::string line; std::getline(ifs, line); )
v.push_back(line);
return v;
}

inline jsons_t get_json()
{
return load_json("data/canada.json");
}
inline jsons_t get_one_json_per_line()
template <typename Parse>
inline void run_benchmark(char const* name, Parse parse)
{
return load_jsons("data/one-json-per-line.txt");
{
auto const marks = benchmark(max_marks, max_iterations, get_small(), parse);
print_result(std::cout << name << ".small: ", marks);
}
{
auto const marks = benchmark(max_marks, max_iterations, get_large(), parse);
print_result(std::cout << name << ".large: ", marks);
}
}

} // namespace jsonbench
Expand Down

0 comments on commit b2a315c

Please sign in to comment.