From cfb2f34ebb471c58b1a83d3ab2999dad2217218b Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 1 May 2020 20:59:47 +0200 Subject: [PATCH] :hammer: fix benchmarks --- benchmarks/CMakeLists.txt | 11 +++--- benchmarks/src/benchmarks.cpp | 65 +++++++++++++++++----------------- cmake/download_test_data.cmake | 11 ++++++ test/CMakeLists.txt | 13 ++----- test/src/test_data.hpp.in | 1 - 5 files changed, 51 insertions(+), 50 deletions(-) create mode 100644 cmake/download_test_data.cmake delete mode 100644 test/src/test_data.hpp.in diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index 953b1cb8..86063dba 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -14,15 +14,12 @@ add_subdirectory(thirdparty/benchmark) include_directories(thirdparty) include_directories(${CMAKE_SOURCE_DIR}/../single_include) -# copy test files to build folder -file(COPY ${CMAKE_SOURCE_DIR}/data DESTINATION .) -file(COPY ${CMAKE_SOURCE_DIR}/../test/data/regression/floats.json - ${CMAKE_SOURCE_DIR}/../test/data/regression/unsigned_ints.json - ${CMAKE_SOURCE_DIR}/../test/data/regression/signed_ints.json - ${CMAKE_SOURCE_DIR}/../test/data/regression/small_signed_ints.json - DESTINATION data/numbers) +# download test data +include(${CMAKE_SOURCE_DIR}/../cmake/download_test_data.cmake) # benchmark binary add_executable(json_benchmarks src/benchmarks.cpp) target_compile_features(json_benchmarks PRIVATE cxx_std_11) target_link_libraries(json_benchmarks benchmark ${CMAKE_THREAD_LIBS_INIT}) +add_dependencies(json_benchmarks download_test_data) +target_include_directories(json_benchmarks PRIVATE ${CMAKE_BINARY_DIR}/include) diff --git a/benchmarks/src/benchmarks.cpp b/benchmarks/src/benchmarks.cpp index 2ce0fbc9..4f32a61a 100644 --- a/benchmarks/src/benchmarks.cpp +++ b/benchmarks/src/benchmarks.cpp @@ -1,6 +1,7 @@ #include "benchmark/benchmark.h" #include #include +#include using json = nlohmann::json; @@ -28,14 +29,14 @@ static void ParseFile(benchmark::State& state, const char* filename) std::ifstream file(filename, std::ios::binary | std::ios::ate); state.SetBytesProcessed(state.iterations() * file.tellg()); } -BENCHMARK_CAPTURE(ParseFile, jeopardy, "data/jeopardy/jeopardy.json"); -BENCHMARK_CAPTURE(ParseFile, canada, "data/nativejson-benchmark/canada.json"); -BENCHMARK_CAPTURE(ParseFile, citm_catalog, "data/nativejson-benchmark/citm_catalog.json"); -BENCHMARK_CAPTURE(ParseFile, twitter, "data/nativejson-benchmark/twitter.json"); -BENCHMARK_CAPTURE(ParseFile, floats, "data/numbers/floats.json"); -BENCHMARK_CAPTURE(ParseFile, signed_ints, "data/numbers/signed_ints.json"); -BENCHMARK_CAPTURE(ParseFile, unsigned_ints, "data/numbers/unsigned_ints.json"); -BENCHMARK_CAPTURE(ParseFile, small_signed_ints, "data/numbers/small_signed_ints.json"); +BENCHMARK_CAPTURE(ParseFile, jeopardy, TEST_DATA_DIRECTORY "/jeopardy/jeopardy.json"); +BENCHMARK_CAPTURE(ParseFile, canada, TEST_DATA_DIRECTORY "/nativejson-benchmark/canada.json"); +BENCHMARK_CAPTURE(ParseFile, citm_catalog, TEST_DATA_DIRECTORY "/nativejson-benchmark/citm_catalog.json"); +BENCHMARK_CAPTURE(ParseFile, twitter, TEST_DATA_DIRECTORY "/nativejson-benchmark/twitter.json"); +BENCHMARK_CAPTURE(ParseFile, floats, TEST_DATA_DIRECTORY "/regression/floats.json"); +BENCHMARK_CAPTURE(ParseFile, signed_ints, TEST_DATA_DIRECTORY "/regression/signed_ints.json"); +BENCHMARK_CAPTURE(ParseFile, unsigned_ints, TEST_DATA_DIRECTORY "/regression/unsigned_ints.json"); +BENCHMARK_CAPTURE(ParseFile, small_signed_ints, TEST_DATA_DIRECTORY "/regression/small_signed_ints.json"); ////////////////////////////////////////////////////////////////////////////// // parse JSON from string @@ -61,14 +62,14 @@ static void ParseString(benchmark::State& state, const char* filename) state.SetBytesProcessed(state.iterations() * str.size()); } -BENCHMARK_CAPTURE(ParseString, jeopardy, "data/jeopardy/jeopardy.json"); -BENCHMARK_CAPTURE(ParseString, canada, "data/nativejson-benchmark/canada.json"); -BENCHMARK_CAPTURE(ParseString, citm_catalog, "data/nativejson-benchmark/citm_catalog.json"); -BENCHMARK_CAPTURE(ParseString, twitter, "data/nativejson-benchmark/twitter.json"); -BENCHMARK_CAPTURE(ParseString, floats, "data/numbers/floats.json"); -BENCHMARK_CAPTURE(ParseString, signed_ints, "data/numbers/signed_ints.json"); -BENCHMARK_CAPTURE(ParseString, unsigned_ints, "data/numbers/unsigned_ints.json"); -BENCHMARK_CAPTURE(ParseString, small_signed_ints, "data/numbers/small_signed_ints.json"); +BENCHMARK_CAPTURE(ParseString, jeopardy, TEST_DATA_DIRECTORY "/jeopardy/jeopardy.json"); +BENCHMARK_CAPTURE(ParseString, canada, TEST_DATA_DIRECTORY "/nativejson-benchmark/canada.json"); +BENCHMARK_CAPTURE(ParseString, citm_catalog, TEST_DATA_DIRECTORY "/nativejson-benchmark/citm_catalog.json"); +BENCHMARK_CAPTURE(ParseString, twitter, TEST_DATA_DIRECTORY "/nativejson-benchmark/twitter.json"); +BENCHMARK_CAPTURE(ParseString, floats, TEST_DATA_DIRECTORY "/regression/floats.json"); +BENCHMARK_CAPTURE(ParseString, signed_ints, TEST_DATA_DIRECTORY "/regression/signed_ints.json"); +BENCHMARK_CAPTURE(ParseString, unsigned_ints, TEST_DATA_DIRECTORY "/regression/unsigned_ints.json"); +BENCHMARK_CAPTURE(ParseString, small_signed_ints, TEST_DATA_DIRECTORY "/regression/small_signed_ints.json"); ////////////////////////////////////////////////////////////////////////////// @@ -88,22 +89,22 @@ static void Dump(benchmark::State& state, const char* filename, int indent) state.SetBytesProcessed(state.iterations() * j.dump(indent).size()); } -BENCHMARK_CAPTURE(Dump, jeopardy / -, "data/jeopardy/jeopardy.json", -1); -BENCHMARK_CAPTURE(Dump, jeopardy / 4, "data/jeopardy/jeopardy.json", 4); -BENCHMARK_CAPTURE(Dump, canada / -, "data/nativejson-benchmark/canada.json", -1); -BENCHMARK_CAPTURE(Dump, canada / 4, "data/nativejson-benchmark/canada.json", 4); -BENCHMARK_CAPTURE(Dump, citm_catalog / -, "data/nativejson-benchmark/citm_catalog.json", -1); -BENCHMARK_CAPTURE(Dump, citm_catalog / 4, "data/nativejson-benchmark/citm_catalog.json", 4); -BENCHMARK_CAPTURE(Dump, twitter / -, "data/nativejson-benchmark/twitter.json", -1); -BENCHMARK_CAPTURE(Dump, twitter / 4, "data/nativejson-benchmark/twitter.json", 4); -BENCHMARK_CAPTURE(Dump, floats / -, "data/numbers/floats.json", -1); -BENCHMARK_CAPTURE(Dump, floats / 4, "data/numbers/floats.json", 4); -BENCHMARK_CAPTURE(Dump, signed_ints / -, "data/numbers/signed_ints.json", -1); -BENCHMARK_CAPTURE(Dump, signed_ints / 4, "data/numbers/signed_ints.json", 4); -BENCHMARK_CAPTURE(Dump, unsigned_ints / -, "data/numbers/unsigned_ints.json", -1); -BENCHMARK_CAPTURE(Dump, unsigned_ints / 4, "data/numbers/unsigned_ints.json", 4); -BENCHMARK_CAPTURE(Dump, small_signed_ints / -, "data/numbers/small_signed_ints.json", -1); -BENCHMARK_CAPTURE(Dump, small_signed_ints / 4, "data/numbers/small_signed_ints.json", 4); +BENCHMARK_CAPTURE(Dump, jeopardy / -, TEST_DATA_DIRECTORY "/jeopardy/jeopardy.json", -1); +BENCHMARK_CAPTURE(Dump, jeopardy / 4, TEST_DATA_DIRECTORY "/jeopardy/jeopardy.json", 4); +BENCHMARK_CAPTURE(Dump, canada / -, TEST_DATA_DIRECTORY "/nativejson-benchmark/canada.json", -1); +BENCHMARK_CAPTURE(Dump, canada / 4, TEST_DATA_DIRECTORY "/nativejson-benchmark/canada.json", 4); +BENCHMARK_CAPTURE(Dump, citm_catalog / -, TEST_DATA_DIRECTORY "/nativejson-benchmark/citm_catalog.json", -1); +BENCHMARK_CAPTURE(Dump, citm_catalog / 4, TEST_DATA_DIRECTORY "/nativejson-benchmark/citm_catalog.json", 4); +BENCHMARK_CAPTURE(Dump, twitter / -, TEST_DATA_DIRECTORY "/nativejson-benchmark/twitter.json", -1); +BENCHMARK_CAPTURE(Dump, twitter / 4, TEST_DATA_DIRECTORY "/nativejson-benchmark/twitter.json", 4); +BENCHMARK_CAPTURE(Dump, floats / -, TEST_DATA_DIRECTORY "/regression/floats.json", -1); +BENCHMARK_CAPTURE(Dump, floats / 4, TEST_DATA_DIRECTORY "/regression/floats.json", 4); +BENCHMARK_CAPTURE(Dump, signed_ints / -, TEST_DATA_DIRECTORY "/regression/signed_ints.json", -1); +BENCHMARK_CAPTURE(Dump, signed_ints / 4, TEST_DATA_DIRECTORY "/regression/signed_ints.json", 4); +BENCHMARK_CAPTURE(Dump, unsigned_ints / -, TEST_DATA_DIRECTORY "/regression/unsigned_ints.json", -1); +BENCHMARK_CAPTURE(Dump, unsigned_ints / 4, TEST_DATA_DIRECTORY "/regression/unsigned_ints.json", 4); +BENCHMARK_CAPTURE(Dump, small_signed_ints / -, TEST_DATA_DIRECTORY "/regression/small_signed_ints.json", -1); +BENCHMARK_CAPTURE(Dump, small_signed_ints / 4, TEST_DATA_DIRECTORY "/regression/small_signed_ints.json", 4); BENCHMARK_MAIN(); diff --git a/cmake/download_test_data.cmake b/cmake/download_test_data.cmake new file mode 100644 index 00000000..8cc93f88 --- /dev/null +++ b/cmake/download_test_data.cmake @@ -0,0 +1,11 @@ +find_package(Git) + +# target to download test data +add_custom_target(download_test_data + COMMAND test -d json_test_data || ${GIT_EXECUTABLE} clone https://github.com/nlohmann/json_test_data.git --quiet --depth 1 + COMMENT "Downloading test data from https://github.com/nlohmann/json_test_data" + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} +) + +# create a header with the path to the downloaded test data +file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/include/test_data.hpp "#define TEST_DATA_DIRECTORY \"${CMAKE_BINARY_DIR}/json_test_data\"") diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 30bef5e6..961e8f00 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -3,20 +3,13 @@ option(JSON_Valgrind "Execute test suite with Valgrind" OFF) option(JSON_NoExceptions "Build test suite without exceptions" OFF) option(JSON_Coverage "Build test suite with coverage information" OFF) -# target to download test data -find_package(Git) -add_custom_target(download_test_data - COMMAND test -d json_test_data || ${GIT_EXECUTABLE} clone https://github.com/nlohmann/json_test_data.git --quiet --depth 1 - COMMENT "Downloading test data from https://github.com/nlohmann/json_test_data" - WORKING_DIRECTORY ${CMAKE_BINARY_DIR} -) +# download test data +include(${CMAKE_SOURCE_DIR}/cmake/download_test_data.cmake) # test fixture to download test data -add_test(NAME "download_test_data" COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target download_test_data) +add_test(NAME "download_test_data" COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target download_test_data) set_tests_properties(download_test_data PROPERTIES FIXTURES_SETUP TEST_DATA) -configure_file(src/test_data.hpp.in include/test_data.hpp) - if(JSON_Sanitizer) message(STATUS "Building test suite with Clang sanitizer") if(NOT MSVC) diff --git a/test/src/test_data.hpp.in b/test/src/test_data.hpp.in deleted file mode 100644 index ac66486e..00000000 --- a/test/src/test_data.hpp.in +++ /dev/null @@ -1 +0,0 @@ -#define TEST_DATA_DIRECTORY "${CMAKE_BINARY_DIR}/json_test_data"