Added the Canada benchmark (#110)

This commit is contained in:
liuzicheng1987
2024-06-02 23:10:30 +02:00
committed by GitHub
parent 436c9fe14b
commit e83598f651
16 changed files with 744 additions and 13 deletions
+4 -1
View File
@@ -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
+4 -1
View File
@@ -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
+4 -1
View File
@@ -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
+4 -1
View File
@@ -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
+4 -1
View File
@@ -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
View File
@@ -54,6 +54,7 @@
!vcpkg.json
!.github/**/*.yaml
!benchmarks/**/*.json
# clangd
compile_flags.txt
+7 -7
View File
@@ -39,12 +39,12 @@ 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 ()
@@ -52,12 +52,12 @@ if (REFLECTCPP_CBOR)
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}")
@@ -66,7 +66,7 @@ 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 ()
@@ -74,12 +74,12 @@ if (REFLECTCPP_TOML)
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 ()
+1
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -0,0 +1,3 @@
#include <benchmark/benchmark.h>
BENCHMARK_MAIN();
File diff suppressed because one or more lines are too long
+425
View File
@@ -0,0 +1,425 @@
#include <benchmark/benchmark.h>
#include <rapidjson/document.h>
#include <simdjson.h>
#include <array>
#include <iostream>
#include <nlohmann/json.hpp>
#include <optional>
#include <rfl/json.hpp>
#include <type_traits>
#include <vector>
namespace canada {
// ----------------------------------------------------------------------------
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 std::string load_data() {
return rfl::io::load_string("benchmarks/data/canada.json").value();
}
// ----------------------------------------------------------------------------
// nlohmann/json
std::vector<std::vector<std::tuple<double, double>>> nlohmann_to_coordinates(
const nlohmann::json &_val);
Property nlohmann_to_property(const nlohmann::json &_val);
Geometry nlohmann_to_geometry(const nlohmann::json &_val);
Feature nlohmann_to_feature(const nlohmann::json &_val);
FeatureCollection nlohmann_to_feature_collection(const nlohmann::json &_val);
std::vector<std::vector<std::tuple<double, double>>> nlohmann_to_coordinates(
const nlohmann::json &_val) {
std::vector<std::vector<std::tuple<double, double>>> coordinates;
for (const auto &arr1 : _val) {
std::vector<std::tuple<double, double>> vec;
for (const auto &arr2 : arr1) {
std::tuple<double, double> tup;
std::get<0>(tup) = arr2[0].template get<double>();
std::get<1>(tup) = arr2[1].template get<double>();
vec.emplace_back(std::move(tup));
}
coordinates.emplace_back(std::move(vec));
}
return coordinates;
}
Property nlohmann_to_property(const nlohmann::json &_val) {
Property property;
property.name = _val["name"].template get<std::string>();
return property;
}
Geometry nlohmann_to_geometry(const nlohmann::json &_val) {
Geometry geometry;
geometry.type = _val["type"].template get<std::string>();
geometry.coordinates = nlohmann_to_coordinates(_val["coordinates"]);
return geometry;
}
Feature nlohmann_to_feature(const nlohmann::json &_val) {
Feature feature;
feature.type = _val["type"].template get<std::string>();
feature.properties = nlohmann_to_property(_val["properties"]);
feature.geometry = nlohmann_to_geometry(_val["geometry"]);
return feature;
}
FeatureCollection nlohmann_to_feature_collection(const nlohmann::json &_val) {
FeatureCollection feature_collection;
feature_collection.type = _val["type"].template get<std::string>();
for (const auto &val : _val["features"]) {
feature_collection.features.push_back(nlohmann_to_feature(val));
}
return feature_collection;
}
static rfl::Result<FeatureCollection> read_using_nlohmann(
const std::string &_json_string) {
try {
auto val = nlohmann::json::parse(_json_string);
return nlohmann_to_feature_collection(val);
} catch (std::exception &e) {
return rfl::Error(e.what());
}
}
// ----------------------------------------------------------------------------
// rapidjson
std::vector<std::vector<std::tuple<double, double>>> rapidjson_to_coordinates(
const rapidjson::Value &_arr);
Property rapidjson_to_property(const rapidjson::Value &_val);
Geometry rapidjson_to_geometry(const rapidjson::Value &_val);
Feature rapidjson_to_feature(const rapidjson::Value &_val);
FeatureCollection rapidjson_to_feature_collection(const rapidjson::Value &_val);
std::vector<std::vector<std::tuple<double, double>>> rapidjson_to_coordinates(
const rapidjson::Value &_arr) {
std::vector<std::vector<std::tuple<double, double>>> coordinates;
for (auto it1 = _arr.Begin(); it1 != _arr.End(); ++it1) {
auto arr1 = it1->GetArray();
std::vector<std::tuple<double, double>> vec;
for (auto it2 = arr1.Begin(); it2 != arr1.End(); ++it2) {
auto arr2 = it2->GetArray();
std::tuple<double, double> tup;
std::get<0>(tup) = arr2[0].GetDouble();
std::get<1>(tup) = arr2[1].GetDouble();
vec.emplace_back(std::move(tup));
}
coordinates.emplace_back(std::move(vec));
}
return coordinates;
}
Property rapidjson_to_property(const rapidjson::Value &_val) {
Property property;
property.name = _val["name"].GetString();
return property;
}
Geometry rapidjson_to_geometry(const rapidjson::Value &_val) {
Geometry geometry;
geometry.type = _val["type"].GetString();
geometry.coordinates =
rapidjson_to_coordinates(_val["coordinates"].GetArray());
return geometry;
}
Feature rapidjson_to_feature(const rapidjson::Value &_val) {
Feature feature;
feature.type = _val["type"].GetString();
feature.properties = rapidjson_to_property(_val["properties"].GetObject());
feature.geometry = rapidjson_to_geometry(_val["geometry"].GetObject());
return feature;
}
FeatureCollection rapidjson_to_feature_collection(
const rapidjson::Value &_val) {
FeatureCollection feature_collection;
feature_collection.type = _val["type"].GetString();
auto arr = _val["features"].GetArray();
for (auto it = arr.Begin(); it != arr.End(); ++it) {
feature_collection.features.push_back(
rapidjson_to_feature(it->GetObject()));
}
return feature_collection;
}
static rfl::Result<FeatureCollection> read_using_rapidjson(
const std::string &_json_string) {
try {
rapidjson::Document d;
d.Parse(_json_string.c_str());
return rapidjson_to_feature_collection(d.GetObject());
} catch (std::exception &e) {
return rfl::Error(e.what());
}
}
// ----------------------------------------------------------------------------
// simdjson
std::vector<std::vector<std::tuple<double, double>>> simdjson_to_coordinates(
simdjson::ondemand::array _val);
Property simdjson_to_property(simdjson::ondemand::object _val);
Geometry simdjson_to_geometry(simdjson::ondemand::object _val);
Feature simdjson_to_feature(simdjson::ondemand::object _val);
FeatureCollection simdjson_to_feature_collection(
simdjson::ondemand::object _val);
std::vector<std::vector<std::tuple<double, double>>> simdjson_to_coordinates(
simdjson::ondemand::array _val) {
std::vector<std::vector<std::tuple<double, double>>> coordinates;
for (auto arr1 : _val) {
std::vector<std::tuple<double, double>> vec;
for (auto val2 : arr1.get_array()) {
auto arr2 = val2.get_array();
std::tuple<double, double> tup;
std::get<0>(tup) = arr2.at(0).get_double();
std::get<1>(tup) = arr2.at(1).get_double();
vec.emplace_back(std::move(tup));
}
coordinates.emplace_back(std::move(vec));
}
return coordinates;
}
Property simdjson_to_property(simdjson::ondemand::object _val) {
Property property;
property.name = _val["name"].get_string().value();
return property;
}
Geometry simdjson_to_geometry(simdjson::ondemand::object _val) {
Geometry geometry;
geometry.type = std::string(_val["type"].get_string().value());
geometry.coordinates =
simdjson_to_coordinates(_val["coordinates"].get_array());
return geometry;
}
Feature simdjson_to_feature(simdjson::ondemand::object _val) {
Feature feature;
feature.type = std::string(_val["type"].get_string().value());
feature.properties = simdjson_to_property(_val["properties"].get_object());
feature.geometry = simdjson_to_geometry(_val["geometry"].get_object());
return feature;
}
FeatureCollection simdjson_to_feature_collection(
simdjson::ondemand::object _val) {
FeatureCollection feature_collection;
feature_collection.type = std::string(_val["type"].get_string().value());
for (auto val : _val["features"].get_array()) {
feature_collection.features.push_back(simdjson_to_feature(val));
}
return feature_collection;
}
static rfl::Result<FeatureCollection> read_using_simdjson(
const std::string &_json_string) {
try {
simdjson::ondemand::parser parser;
auto padded_str = simdjson::padded_string(_json_string);
auto doc = parser.iterate(padded_str).value();
return simdjson_to_feature_collection(doc.get_object());
} catch (std::exception &e) {
return rfl::Error(e.what());
}
}
// ----------------------------------------------------------------------------
// yyjson
std::vector<std::vector<std::tuple<double, double>>> yyjson_to_coordinates(
yyjson_val *_val);
Property yyjson_to_property(yyjson_val *_val);
Geometry yyjson_to_geometry(yyjson_val *_val);
Feature yyjson_to_feature(yyjson_val *_val);
FeatureCollection yyjson_to_feature_collection(yyjson_val *_val);
std::vector<std::vector<std::tuple<double, double>>> yyjson_to_coordinates(
yyjson_val *_val) {
std::vector<std::vector<std::tuple<double, double>>> coordinates;
yyjson_val *v1;
yyjson_arr_iter iter1;
yyjson_arr_iter_init(_val, &iter1);
while ((v1 = yyjson_arr_iter_next(&iter1))) {
std::vector<std::tuple<double, double>> vec;
yyjson_val *arr2;
yyjson_arr_iter iter2;
yyjson_arr_iter_init(v1, &iter2);
while ((arr2 = yyjson_arr_iter_next(&iter2))) {
std::tuple<double, double> tup;
std::get<0>(tup) =
static_cast<double>(yyjson_get_num(yyjson_arr_get(arr2, 0)));
std::get<1>(tup) =
static_cast<double>(yyjson_get_num(yyjson_arr_get(arr2, 1)));
vec.emplace_back(std::move(tup));
}
coordinates.emplace_back(std::move(vec));
}
return coordinates;
}
Property yyjson_to_property(yyjson_val *_val) {
Property property;
property.name = yyjson_get_str(yyjson_obj_get(_val, "name"));
return property;
}
Geometry yyjson_to_geometry(yyjson_val *_val) {
Geometry geometry;
geometry.type = std::string(yyjson_get_str(yyjson_obj_get(_val, "type")));
geometry.coordinates =
yyjson_to_coordinates(yyjson_obj_get(_val, "coordinates"));
return geometry;
}
Feature yyjson_to_feature(yyjson_val *_val) {
Feature feature;
feature.type = std::string(yyjson_get_str(yyjson_obj_get(_val, "type")));
feature.properties = yyjson_to_property(yyjson_obj_get(_val, "properties"));
feature.geometry = yyjson_to_geometry(yyjson_obj_get(_val, "geometry"));
return feature;
}
FeatureCollection yyjson_to_feature_collection(yyjson_val *_val) {
FeatureCollection feature_collection;
feature_collection.type =
std::string(yyjson_get_str(yyjson_obj_get(_val, "type")));
auto features = yyjson_obj_get(_val, "features");
yyjson_val *feat;
yyjson_arr_iter iter;
yyjson_arr_iter_init(features, &iter);
while ((feat = yyjson_arr_iter_next(&iter))) {
feature_collection.features.push_back(yyjson_to_feature(feat));
}
return feature_collection;
}
static rfl::Result<FeatureCollection> read_using_yyjson(
const std::string &_json_string) {
yyjson_doc *doc = yyjson_read(_json_string.c_str(), _json_string.size(), 0);
if (!doc) {
std::cout << "Could not parse document!" << std::endl;
return rfl::Error("Could not parse document");
}
yyjson_val *root = yyjson_doc_get_root(doc);
auto person = yyjson_to_feature_collection(root);
yyjson_doc_free(doc);
return person;
try {
auto val = nlohmann::json::parse(_json_string);
return nlohmann_to_feature_collection(val);
} catch (std::exception &e) {
return rfl::Error(e.what());
}
}
// ----------------------------------------------------------------------------
static void BM_canada_nlohmann(benchmark::State &state) {
const auto json_string = load_data();
for (auto _ : state) {
const auto res = read_using_nlohmann(json_string);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_canada_nlohmann);
static void BM_canada_rapidjson(benchmark::State &state) {
const auto json_string = load_data();
for (auto _ : state) {
const auto res = read_using_rapidjson(json_string);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_canada_rapidjson);
#ifndef __clang__
// simdjson has a bug in clang.
static void BM_canada_simdjson(benchmark::State &state) {
const auto json_string = load_data();
for (auto _ : state) {
const auto res = read_using_simdjson(json_string);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_canada_simdjson);
#endif
static void BM_canada_yyjson(benchmark::State &state) {
const auto json_string = load_data();
for (auto _ : state) {
const auto res = read_using_yyjson(json_string);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_canada_yyjson);
static void BM_canada_reflect_cpp(benchmark::State &state) {
const auto json_string = load_data();
for (auto _ : state) {
const auto res = rfl::json::read<FeatureCollection>(json_string);
if (!res) {
std::cout << res.error()->what() << std::endl;
}
}
}
BENCHMARK(BM_canada_reflect_cpp);
// ----------------------------------------------------------------------------
} // namespace canada
+3
View File
@@ -0,0 +1,3 @@
#include <benchmark/benchmark.h>
BENCHMARK_MAIN();
-1
View File
@@ -294,4 +294,3 @@ BENCHMARK(BM_simple_read_reflect_cpp);
} // namespace simple_read
BENCHMARK_MAIN();