Skip to content

Commit

Permalink
Added the Canada benchmark (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 authored Jun 2, 2024
1 parent 436c9fe commit e83598f
Show file tree
Hide file tree
Showing 16 changed files with 744 additions and 13 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/linux-clang.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ jobs:
./build/tests/yaml/reflect-cpp-yaml-tests
- name: Run benchmarks
run: |
./build/benchmarks/json/reflect-cpp-json-benchmarks
./build/benchmarks/json/reflect-cpp-json-benchmarks --benchmark_filter=canada
./build/benchmarks/json/reflect-cpp-json-benchmarks --benchmark_filter=simple_read
./build/benchmarks/all/reflect-cpp-all-format-benchmarks --benchmark_filter=canada_read
./build/benchmarks/all/reflect-cpp-all-format-benchmarks --benchmark_filter=canada_write
5 changes: 4 additions & 1 deletion .github/workflows/linux-gcc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ jobs:
./build/tests/yaml/reflect-cpp-yaml-tests
- name: Run benchmarks
run: |
./build/benchmarks/json/reflect-cpp-json-benchmarks
./build/benchmarks/json/reflect-cpp-json-benchmarks --benchmark_filter=canada
./build/benchmarks/json/reflect-cpp-json-benchmarks --benchmark_filter=simple_read
./build/benchmarks/all/reflect-cpp-all-format-benchmarks --benchmark_filter=canada_read
./build/benchmarks/all/reflect-cpp-all-format-benchmarks --benchmark_filter=canada_write
5 changes: 4 additions & 1 deletion .github/workflows/macos-clang-arm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ jobs:
./build/tests/yaml/reflect-cpp-yaml-tests
- name: Run benchmarks
run: |
./build/benchmarks/json/reflect-cpp-json-benchmarks
./build/benchmarks/json/reflect-cpp-json-benchmarks --benchmark_filter=canada
./build/benchmarks/json/reflect-cpp-json-benchmarks --benchmark_filter=simple_read
./build/benchmarks/all/reflect-cpp-all-format-benchmarks --benchmark_filter=canada_read
./build/benchmarks/all/reflect-cpp-all-format-benchmarks --benchmark_filter=canada_write
5 changes: 4 additions & 1 deletion .github/workflows/macos-clang-x64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ jobs:
./build/tests/yaml/reflect-cpp-yaml-tests
- name: Run benchmarks
run: |
./build/benchmarks/json/reflect-cpp-json-benchmarks
./build/benchmarks/json/reflect-cpp-json-benchmarks --benchmark_filter=canada
./build/benchmarks/json/reflect-cpp-json-benchmarks --benchmark_filter=simple_read
./build/benchmarks/all/reflect-cpp-all-format-benchmarks --benchmark_filter=canada_read
./build/benchmarks/all/reflect-cpp-all-format-benchmarks --benchmark_filter=canada_write
5 changes: 4 additions & 1 deletion .github/workflows/windows-msvc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ jobs:
.\build\tests\yaml\Release\reflect-cpp-yaml-tests.exe
- name: Run benchmarks
run: |
.\build\benchmarks\json\Release\reflect-cpp-json-benchmarks.exe
.\build\benchmarks\json\Release\reflect-cpp-json-benchmarks.exe --benchmark_filter=canada
.\build\benchmarks\json\Release\reflect-cpp-json-benchmarks.exe --benchmark_filter=simple_read
.\build\benchmarks\all\Release\reflect-cpp-all-format-benchmarks.exe --benchmark_filter=canada_read
.\build\benchmarks\all\Release\reflect-cpp-all-format-benchmarks.exe --benchmark_filter=canada_write
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

!vcpkg.json
!.github/**/*.yaml
!benchmarks/**/*.json

# clangd
compile_flags.txt
Expand Down
14 changes: 7 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,25 @@ target_compile_features(reflectcpp PUBLIC cxx_std_20)

target_include_directories(reflectcpp PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include> )

if (REFLECTCPP_BSON)
if (REFLECTCPP_BSON OR REFLECTCPP_BUILD_BENCHMARKS)
find_package(bson-1.0 CONFIG REQUIRED)
target_link_libraries(reflectcpp PRIVATE $<IF:$<TARGET_EXISTS:mongo::bson_static>,mongo::bson_static,mongo::bson_shared>)
endif ()

if (REFLECTCPP_CBOR)
if (REFLECTCPP_CBOR OR REFLECTCPP_BUILD_BENCHMARKS)
if (MSVC)
target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/tinycbor${CMAKE_STATIC_LIBRARY_SUFFIX}")
else ()
target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/libtinycbor${CMAKE_STATIC_LIBRARY_SUFFIX}")
endif ()
endif ()

if (REFLECTCPP_FLEXBUFFERS)
if (REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_BUILD_BENCHMARKS)
find_package(flatbuffers CONFIG REQUIRED)
target_link_libraries(reflectcpp INTERFACE flatbuffers::flatbuffers)
endif ()

if (REFLECTCPP_MSGPACK)
if (REFLECTCPP_MSGPACK OR REFLECTCPP_BUILD_BENCHMARKS)
find_package(msgpack-c CONFIG REQUIRED)
if (MSVC)
target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/msgpack-c${CMAKE_STATIC_LIBRARY_SUFFIX}")
Expand All @@ -66,20 +66,20 @@ if (REFLECTCPP_MSGPACK)
endif ()
endif ()

if (REFLECTCPP_TOML)
if (REFLECTCPP_TOML OR REFLECTCPP_BUILD_BENCHMARKS)
if (MSVC)
target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/tomlplusplus${CMAKE_STATIC_LIBRARY_SUFFIX}")
else ()
target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/libtomlplusplus${CMAKE_STATIC_LIBRARY_SUFFIX}")
endif ()
endif()

if (REFLECTCPP_XML)
if (REFLECTCPP_XML OR REFLECTCPP_BUILD_BENCHMARKS)
find_package(pugixml CONFIG REQUIRED)
target_link_libraries(reflectcpp INTERFACE pugixml::pugixml)
endif ()

if (REFLECTCPP_YAML)
if (REFLECTCPP_YAML OR REFLECTCPP_BUILD_BENCHMARKS)
find_package(yaml-cpp CONFIG REQUIRED)
target_link_libraries(reflectcpp INTERFACE yaml-cpp::yaml-cpp)
endif ()
Expand Down
1 change: 1 addition & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -Wall -O2")
endif()

add_subdirectory(all)
add_subdirectory(json)
17 changes: 17 additions & 0 deletions benchmarks/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
project(reflect-cpp-all-format-benchmarks)

file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")

add_executable(
reflect-cpp-all-format-benchmarks
${SOURCES}
)

target_include_directories(reflect-cpp-all-format-benchmarks SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include")
target_include_directories(reflect-cpp-all-format-benchmarks SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include/tinycbor")

target_link_libraries(
reflect-cpp-all-format-benchmarks
PRIVATE
reflectcpp
)
129 changes: 129 additions & 0 deletions benchmarks/all/canada_read.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#include <benchmark/benchmark.h>

#include <array>
#include <iostream>
#include <optional>
#include <rfl/bson.hpp>
#include <rfl/cbor.hpp>
#include <rfl/flexbuf.hpp>
#include <rfl/json.hpp>
#include <rfl/msgpack.hpp>
#include <rfl/toml.hpp>
#include <rfl/yaml.hpp>
#include <type_traits>
#include <vector>

namespace canada_read {

// ----------------------------------------------------------------------------

struct Property {
std::string name;
};

struct Geometry {
rfl::Literal<"Polygon"> type;
std::vector<std::vector<std::tuple<double, double>>> coordinates;
};

struct Feature {
rfl::Literal<"Feature"> type;
Property properties;
Geometry geometry;
};

struct FeatureCollection {
rfl::Literal<"FeatureCollection"> type;
std::vector<Feature> features;
};

// ----------------------------------------------------------------------------

static FeatureCollection load_data() {
return rfl::json::load<FeatureCollection>("benchmarks/data/canada.json")
.value();
}

// ----------------------------------------------------------------------------

static void BM_canada_read_reflect_cpp_bson(benchmark::State &state) {
const auto data = rfl::bson::write(load_data());
for (auto _ : state) {
const auto res = rfl::bson::read<FeatureCollection>(data);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_canada_read_reflect_cpp_bson);

static void BM_canada_read_reflect_cpp_cbor(benchmark::State &state) {
const auto data = rfl::cbor::write(load_data());
for (auto _ : state) {
const auto res = rfl::cbor::read<FeatureCollection>(data);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_canada_read_reflect_cpp_cbor);

static void BM_canada_read_reflect_cpp_flexbuf(benchmark::State &state) {
const auto data = rfl::flexbuf::write(load_data());
for (auto _ : state) {
const auto res = rfl::flexbuf::read<FeatureCollection>(data);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_canada_read_reflect_cpp_flexbuf);

static void BM_canada_read_reflect_cpp_json(benchmark::State &state) {
const auto data = rfl::json::write(load_data());
for (auto _ : state) {
const auto res = rfl::json::read<FeatureCollection>(data);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_canada_read_reflect_cpp_json);

static void BM_canada_read_reflect_cpp_msgpack(benchmark::State &state) {
const auto data = rfl::msgpack::write(load_data());
for (auto _ : state) {
const auto res = rfl::msgpack::read<FeatureCollection>(data);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_canada_read_reflect_cpp_msgpack);

static void BM_canada_read_reflect_cpp_toml(benchmark::State &state) {
const auto data = rfl::toml::write(load_data());
for (auto _ : state) {
const auto res = rfl::toml::read<FeatureCollection>(data);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_canada_read_reflect_cpp_toml);

static void BM_canada_read_reflect_cpp_yaml(benchmark::State &state) {
const auto data = rfl::yaml::write(load_data());
for (auto _ : state) {
const auto res = rfl::yaml::read<FeatureCollection>(data);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_canada_read_reflect_cpp_yaml);

// ----------------------------------------------------------------------------

} // namespace canada_read

129 changes: 129 additions & 0 deletions benchmarks/all/canada_write.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#include <benchmark/benchmark.h>

#include <array>
#include <iostream>
#include <optional>
#include <rfl/bson.hpp>
#include <rfl/cbor.hpp>
#include <rfl/flexbuf.hpp>
#include <rfl/json.hpp>
#include <rfl/msgpack.hpp>
#include <rfl/toml.hpp>
#include <rfl/yaml.hpp>
#include <type_traits>
#include <vector>

namespace canada_write {

// ----------------------------------------------------------------------------

struct Property {
std::string name;
};

struct Geometry {
rfl::Literal<"Polygon"> type;
std::vector<std::vector<std::tuple<double, double>>> coordinates;
};

struct Feature {
rfl::Literal<"Feature"> type;
Property properties;
Geometry geometry;
};

struct FeatureCollection {
rfl::Literal<"FeatureCollection"> type;
std::vector<Feature> features;
};

// ----------------------------------------------------------------------------

static FeatureCollection load_data() {
return rfl::json::load<FeatureCollection>("benchmarks/data/canada.json")
.value();
}

// ----------------------------------------------------------------------------

static void BM_canada_write_reflect_cpp_bson(benchmark::State &state) {
const auto data = load_data();
for (auto _ : state) {
const auto output = rfl::bson::write(data);
if (output.size() == 0) {
std::cout << "No output" << std::endl;
}
}
}
BENCHMARK(BM_canada_write_reflect_cpp_bson);

static void BM_canada_write_reflect_cpp_cbor(benchmark::State &state) {
const auto data = load_data();
for (auto _ : state) {
const auto output = rfl::cbor::write(data);
if (output.size() == 0) {
std::cout << "No output" << std::endl;
}
}
}
BENCHMARK(BM_canada_write_reflect_cpp_cbor);

static void BM_canada_write_reflect_cpp_flexbuf(benchmark::State &state) {
const auto data = load_data();
for (auto _ : state) {
const auto output = rfl::flexbuf::write(data);
if (output.size() == 0) {
std::cout << "No output" << std::endl;
}
}
}
BENCHMARK(BM_canada_write_reflect_cpp_flexbuf);

static void BM_canada_write_reflect_cpp_json(benchmark::State &state) {
const auto data = load_data();
for (auto _ : state) {
const auto output = rfl::json::write(data);
if (output.size() == 0) {
std::cout << "No output" << std::endl;
}
}
}
BENCHMARK(BM_canada_write_reflect_cpp_json);

static void BM_canada_write_reflect_cpp_msgpack(benchmark::State &state) {
const auto data = load_data();
for (auto _ : state) {
const auto output = rfl::msgpack::write(data);
if (output.size() == 0) {
std::cout << "No output" << std::endl;
}
}
}
BENCHMARK(BM_canada_write_reflect_cpp_msgpack);

static void BM_canada_write_reflect_cpp_toml(benchmark::State &state) {
const auto data = load_data();
for (auto _ : state) {
const auto output = rfl::toml::write(data);
if (output.size() == 0) {
std::cout << "No output" << std::endl;
}
}
}
BENCHMARK(BM_canada_write_reflect_cpp_toml);

static void BM_canada_write_reflect_cpp_yaml(benchmark::State &state) {
const auto data = load_data();
for (auto _ : state) {
const auto output = rfl::yaml::write(data);
if (output.size() == 0) {
std::cout << "No output" << std::endl;
}
}
}
BENCHMARK(BM_canada_write_reflect_cpp_yaml);

// ----------------------------------------------------------------------------

} // namespace canada_write

3 changes: 3 additions & 0 deletions benchmarks/all/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <benchmark/benchmark.h>

BENCHMARK_MAIN();
Loading

0 comments on commit e83598f

Please sign in to comment.