forked from getml/reflect-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
find_package(benchmark CONFIG REQUIRED) | ||
|
||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O2") | ||
|
||
if (MSVC) | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std:c++20") | ||
else() | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -Wall -Wno-sign-compare -Wno-missing-braces -Wno-psabi -pthread -fno-strict-aliasing -fwrapv -O2 -ftemplate-backtrace-limit=0 -fsanitize=undefined") | ||
endif() | ||
|
||
# add_subdirectory(json) | ||
|
||
# if (REFLECTCPP_BSON) | ||
# add_subdirectory(bson) | ||
# endif() | ||
|
||
if (REFLECTCPP_CBOR) | ||
add_subdirectory(cbor) | ||
endif() | ||
|
||
# if (REFLECTCPP_FLEXBUFFERS) | ||
# add_subdirectory(flexbuffers) | ||
# endif() | ||
|
||
# if (REFLECTCPP_XML) | ||
# add_subdirectory(xml) | ||
# endif() | ||
|
||
# if (REFLECTCPP_YAML) | ||
# add_subdirectory(yaml) | ||
# endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
project(reflect-cpp-cbor-benches) | ||
|
||
file(GLOB_RECURSE SOURCES "*.cpp") | ||
|
||
add_executable(reflect-cpp-cbor-benches ${SOURCES}) | ||
|
||
target_include_directories(reflect-cpp-cbor-benches SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include/tinycbor") | ||
target_link_libraries(reflect-cpp-cbor-benches PRIVATE reflectcpp benchmark::benchmark) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
#include <benchmark/benchmark.h> | ||
#include <cbor.h> | ||
|
||
#include <rfl.hpp> | ||
#include <rfl/cbor.hpp> | ||
|
||
struct Minimal { | ||
double foo; | ||
double bar; | ||
double baz; | ||
double qux; | ||
}; | ||
|
||
static void BM_Write(benchmark::State& state) { | ||
const auto input = Minimal{ | ||
.foo = 10.5, | ||
.bar = 11.1, | ||
.baz = 10.9, | ||
.qux = 11.3, | ||
}; | ||
|
||
for (auto _ : state) { | ||
benchmark::DoNotOptimize(rfl::cbor::write(input)); | ||
} | ||
} | ||
BENCHMARK(BM_Write); | ||
|
||
std::vector<uint8_t> write_tinycbor(const Minimal& input) { | ||
std::vector<uint8_t> buffer(4096); | ||
|
||
CborEncoder encoder; | ||
cbor_encoder_init(&encoder, buffer.data(), buffer.size(), 0); | ||
|
||
CborEncoder map_encoder; | ||
cbor_encoder_create_map(&encoder, &map_encoder, 4); | ||
cbor_encode_text_string(&map_encoder, "foo", 3); | ||
cbor_encode_double(&map_encoder, input.foo); | ||
cbor_encode_text_string(&map_encoder, "bar", 3); | ||
cbor_encode_double(&map_encoder, input.bar); | ||
cbor_encode_text_string(&map_encoder, "baz", 3); | ||
cbor_encode_double(&map_encoder, input.baz); | ||
cbor_encode_text_string(&map_encoder, "qux", 3); | ||
cbor_encode_double(&map_encoder, input.qux); | ||
cbor_encoder_close_container(&encoder, &map_encoder); | ||
|
||
const auto length = cbor_encoder_get_buffer_size(&encoder, buffer.data()); | ||
buffer.resize(length); | ||
|
||
return buffer; | ||
} | ||
|
||
static void BM_Write_tinycbor(benchmark::State& state) { | ||
const auto input = Minimal{ | ||
.foo = 10.5, | ||
.bar = 11.1, | ||
.baz = 10.9, | ||
.qux = 11.3, | ||
}; | ||
|
||
for (auto _ : state) { | ||
benchmark::DoNotOptimize(write_tinycbor(input)); | ||
} | ||
} | ||
BENCHMARK(BM_Write_tinycbor); | ||
|
||
static void BM_Read(benchmark::State& state) { | ||
const auto input = Minimal{ | ||
.foo = 10.5, | ||
.bar = 11.1, | ||
.baz = 10.9, | ||
.qux = 11.3, | ||
}; | ||
|
||
const auto serialized = rfl::cbor::write(input); | ||
|
||
for (auto _ : state) { | ||
benchmark::DoNotOptimize(rfl::cbor::read<Minimal>(serialized)); | ||
} | ||
} | ||
BENCHMARK(BM_Read); | ||
|
||
Minimal read_tinycbor(const uint8_t* data, size_t size) { | ||
Minimal deserialized; | ||
|
||
CborParser parser; | ||
CborValue it; | ||
cbor_parser_init(data, size, 0, &parser, &it); | ||
|
||
CborValue recursed; | ||
cbor_value_enter_container(&it, &recursed); | ||
|
||
{ | ||
std::vector<char> buffer; | ||
size_t length{}; | ||
cbor_value_get_string_length(&recursed, &length); | ||
buffer.resize(length + 1); | ||
buffer[length] = '\0'; | ||
cbor_value_copy_text_string(&recursed, buffer.data(), &length, nullptr); | ||
cbor_value_advance(&recursed); | ||
} | ||
{ | ||
cbor_value_get_double(&recursed, &deserialized.foo); | ||
cbor_value_advance(&recursed); | ||
} | ||
|
||
{ | ||
std::vector<char> buffer; | ||
size_t length{}; | ||
cbor_value_get_string_length(&recursed, &length); | ||
buffer.resize(length + 1); | ||
buffer[length] = '\0'; | ||
cbor_value_copy_text_string(&recursed, buffer.data(), &length, nullptr); | ||
cbor_value_advance(&recursed); | ||
} | ||
{ | ||
cbor_value_get_double(&recursed, &deserialized.bar); | ||
cbor_value_advance(&recursed); | ||
} | ||
|
||
{ | ||
std::vector<char> buffer; | ||
size_t length{}; | ||
cbor_value_get_string_length(&recursed, &length); | ||
buffer.resize(length + 1); | ||
buffer[length] = '\0'; | ||
cbor_value_copy_text_string(&recursed, buffer.data(), &length, nullptr); | ||
cbor_value_advance(&recursed); | ||
} | ||
{ | ||
cbor_value_get_double(&recursed, &deserialized.baz); | ||
cbor_value_advance(&recursed); | ||
} | ||
|
||
{ | ||
std::vector<char> buffer; | ||
size_t length{}; | ||
cbor_value_get_string_length(&recursed, &length); | ||
buffer.resize(length + 1); | ||
buffer[length] = '\0'; | ||
cbor_value_copy_text_string(&recursed, buffer.data(), &length, nullptr); | ||
cbor_value_advance(&recursed); | ||
} | ||
{ | ||
cbor_value_get_double(&recursed, &deserialized.qux); | ||
cbor_value_advance(&recursed); | ||
} | ||
|
||
cbor_value_leave_container(&it, &recursed); | ||
|
||
return deserialized; | ||
} | ||
|
||
static void BM_Read_tinycbor(benchmark::State& state) { | ||
const auto input = Minimal{ | ||
.foo = 10.5, | ||
.bar = 11.1, | ||
.baz = 10.9, | ||
.qux = 11.3, | ||
}; | ||
|
||
const auto serialized = rfl::cbor::write(input); | ||
|
||
for (auto _ : state) { | ||
benchmark::DoNotOptimize( | ||
read_tinycbor(reinterpret_cast<const uint8_t*>(serialized.data()), | ||
serialized.size())); | ||
} | ||
} | ||
BENCHMARK(BM_Read_tinycbor); | ||
|
||
BENCHMARK_MAIN(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters