Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
theodelrieu committed Jul 4, 2018
1 parent 9007790 commit 9c773ef
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 84 deletions.
4 changes: 1 addition & 3 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
cmake_minimum_required(VERSION 3.8)
project(JSON_Benchmarks LANGUAGES CXX)

# set compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-devirtualize")

# configure Google Benchmarks
set(BENCHMARK_ENABLE_TESTING OFF CACHE INTERNAL "" FORCE)
Expand All @@ -20,6 +18,6 @@ file(COPY ${CMAKE_SOURCE_DIR}/../test/data/regression/floats.json
DESTINATION data/numbers)

# benchmark binary
add_executable(json_benchmarks src/benchmarks.cpp)
add_executable(json_benchmarks src/benchmarks.cpp src/virtual.cpp)
target_compile_features(json_benchmarks PRIVATE cxx_std_11)
target_link_libraries(json_benchmarks benchmark ${CMAKE_THREAD_LIBS_INIT})
103 changes: 22 additions & 81 deletions benchmarks/src/benchmarks.cpp
Original file line number Diff line number Diff line change
@@ -1,83 +1,12 @@
#include "benchmark/benchmark.h"
#include <nlohmann/json.hpp>
#include <fstream>
#include "virtual.h"

using json = nlohmann::json;

namespace
{
struct abstract_bench_sax
{
virtual bool null() = 0;
virtual bool boolean(bool) = 0;
virtual bool number_integer(json::number_integer_t) = 0;
virtual bool number_unsigned(json::number_unsigned_t) = 0;
virtual bool number_float(json::number_float_t, const json::string_t&) = 0;
virtual bool string(json::string_t& val) = 0;
virtual bool start_object(std::size_t elements) = 0;
virtual bool key(json::string_t& val) = 0;
virtual bool end_object() = 0;
virtual bool start_array(std::size_t elements) = 0;
virtual bool end_array() = 0;
// FIXME exceptions should not be in detail
virtual bool parse_error(std::size_t position, const std::string& last_token,
const nlohmann::detail::exception& ex) = 0;
};

struct virtual_bench_sax : public abstract_bench_sax
{
bool null() override
{
return true;
}
bool boolean(bool) override
{
return true;
}
bool number_integer(json::number_integer_t) override
{
return true;
}
bool number_unsigned(json::number_unsigned_t) override
{
return true;
}
bool number_float(json::number_float_t, const json::string_t&) override
{
return true;
}
bool string(json::string_t& val) override
{
return true;
}
bool start_object(std::size_t elements) override
{
return true;
}
bool key(json::string_t& val) override
{
return true;
}
bool end_object() override
{
return true;
}
bool start_array(std::size_t elements) override
{
return true;
}
bool end_array() override
{
return true;
}
// FIXME exceptions should not be in detail
bool parse_error(std::size_t position, const std::string& last_token,
const nlohmann::detail::exception& ex) override
{
return true;
}
};

struct template_sax
{
bool null()
Expand Down Expand Up @@ -137,15 +66,14 @@ struct template_sax
// parse JSON from file (SAX)
//////////////////////////////////////////////////////////////////////////////

template <typename SAX, typename SAXBase = SAX>
static void ParseFileSAX(benchmark::State& state, const char* filename)
static void ParseFileSAXVirtual(benchmark::State& state, const char* filename)
{
while (state.KeepRunning())
{
state.PauseTiming();
auto* f = new std::ifstream(filename);
auto* j = new json();
SAXBase* sax = new SAX;
auto sax = create_virtual_sax();
state.ResumeTiming();

*j = json::sax_parse(*f, sax);
Expand All @@ -161,14 +89,27 @@ static void ParseFileSAX(benchmark::State& state, const char* filename)
state.SetBytesProcessed(state.iterations() * file.tellg());
}

static void ParseFileSAXVirtual(benchmark::State& state, const char* filename)
{
ParseFileSAX<virtual_bench_sax, abstract_bench_sax>(state, filename);
}

static void ParseFileSAXTemplate(benchmark::State& state, const char* filename)
{
ParseFileSAX<template_sax>(state, filename);
while (state.KeepRunning())
{
state.PauseTiming();
auto* f = new std::ifstream(filename);
auto* j = new json();
auto sax = new template_sax;
state.ResumeTiming();

*j = json::sax_parse(*f, sax);

state.PauseTiming();
delete f;
delete j;
delete sax;
state.ResumeTiming();
}

std::ifstream file(filename, std::ios::binary | std::ios::ate);
state.SetBytesProcessed(state.iterations() * file.tellg());
}

BENCHMARK_CAPTURE(ParseFileSAXVirtual, jeopardy, "data/jeopardy/jeopardy.json")->Unit(benchmark::kMillisecond);
Expand Down
62 changes: 62 additions & 0 deletions benchmarks/src/virtual.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "virtual.h"

using json = nlohmann::json;

struct virtual_bench_sax : public abstract_bench_sax
{
bool null() override
{
return true;
}
bool boolean(bool) override
{
return true;
}
bool number_integer(json::number_integer_t) override
{
return true;
}
bool number_unsigned(json::number_unsigned_t) override
{
return true;
}
bool number_float(json::number_float_t, const json::string_t&) override
{
return true;
}
bool string(json::string_t& val) override
{
return true;
}
bool start_object(std::size_t elements) override
{
return true;
}
bool key(json::string_t& val) override
{
return true;
}
bool end_object() override
{
return true;
}
bool start_array(std::size_t elements) override
{
return true;
}
bool end_array() override
{
return true;
}
// FIXME exceptions should not be in detail
bool parse_error(std::size_t position, const std::string& last_token,
const nlohmann::detail::exception& ex) override
{
return true;
}
};

abstract_bench_sax* create_virtual_sax()
{
return new virtual_bench_sax;
}
23 changes: 23 additions & 0 deletions benchmarks/src/virtual.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <nlohmann/json.hpp>

struct abstract_bench_sax
{
virtual bool null() = 0;
virtual bool boolean(bool) = 0;
virtual bool number_integer(nlohmann::json::number_integer_t) = 0;
virtual bool number_unsigned(nlohmann::json::number_unsigned_t) = 0;
virtual bool number_float(nlohmann::json::number_float_t, const nlohmann::json::string_t&) = 0;
virtual bool string(nlohmann::json::string_t& val) = 0;
virtual bool start_object(std::size_t elements) = 0;
virtual bool key(nlohmann::json::string_t& val) = 0;
virtual bool end_object() = 0;
virtual bool start_array(std::size_t elements) = 0;
virtual bool end_array() = 0;
// FIXME exceptions should not be in detail
virtual bool parse_error(std::size_t position, const std::string& last_token,
const nlohmann::detail::exception& ex) = 0;
};

abstract_bench_sax* create_virtual_sax();

0 comments on commit 9c773ef

Please sign in to comment.