Skip to content

Commit d036434

Browse files
committed
Added zpp_bits performance benchmark.
1 parent 0f59603 commit d036434

File tree

6 files changed

+208
-3
lines changed

6 files changed

+208
-3
lines changed

.gitlinks

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CppCommon modules/CppCommon https://github.com/chronoxor/CppCommon.git master
66
flatbuffers modules/flatbuffers https://github.com/google/flatbuffers.git master
77
protobuf modules/protobuf https://github.com/google/protobuf.git main
88
rapidjson modules/rapidjson https://github.com/miloyip/rapidjson.git master
9+
zpp_bits modules/zpp_bits https://github.com/eyalz800/zpp_bits.git v4.4.10
910

1011
# Scripts
1112
build build https://github.com/chronoxor/CppBuildScripts.git master

CMakeLists.txt

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ endif()
2020
# CMake module path
2121
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
2222

23+
# C++20
24+
set(CMAKE_CXX_STANDARD 20)
25+
2326
# Compiler features
2427
include(SetCompilerFeatures)
2528
include(SetCompilerWarnings)
@@ -161,7 +164,7 @@ if(NOT CPPSERIALIZATION_MODULE)
161164
string(REGEX REPLACE "(.*)\\.cpp" "\\1" EXAMPLE_NAME ${EXAMPLE_SOURCE_FILE})
162165
set(EXAMPLE_TARGET "cppserialization-example-${EXAMPLE_NAME}")
163166
add_executable(${EXAMPLE_TARGET} ${EXAMPLE_HEADER_FILES} ${EXAMPLE_INLINE_FILES} "examples/${EXAMPLE_SOURCE_FILE}")
164-
set_target_properties(${EXAMPLE_TARGET} PROPERTIES COMPILE_FLAGS "${PEDANTIC_COMPILE_FLAGS}" FOLDER "examples")
167+
set_target_properties(${EXAMPLE_TARGET} PROPERTIES COMPILE_FLAGS "${PEDANTIC_COMPILE_FLAGS} -Wno-shadow" FOLDER "examples")
165168
target_link_libraries(${EXAMPLE_TARGET} ${LINKLIBS})
166169
list(APPEND INSTALL_TARGETS ${EXAMPLE_TARGET})
167170
list(APPEND INSTALL_TARGETS_PDB ${EXAMPLE_TARGET})
@@ -175,7 +178,7 @@ if(NOT CPPSERIALIZATION_MODULE)
175178
string(REGEX REPLACE "(.*)\\.cpp" "\\1" BENCHMARK_NAME ${BENCHMARK_SOURCE_FILE})
176179
set(BENCHMARK_TARGET "cppserialization-performance-${BENCHMARK_NAME}")
177180
add_executable(${BENCHMARK_TARGET} ${BENCHMARK_HEADER_FILES} ${BENCHMARK_INLINE_FILES} "performance/${BENCHMARK_SOURCE_FILE}")
178-
set_target_properties(${BENCHMARK_TARGET} PROPERTIES COMPILE_FLAGS "${PEDANTIC_COMPILE_FLAGS}" FOLDER "performance")
181+
set_target_properties(${BENCHMARK_TARGET} PROPERTIES COMPILE_FLAGS "${PEDANTIC_COMPILE_FLAGS} -Wno-shadow" FOLDER "performance")
179182
target_link_libraries(${BENCHMARK_TARGET} ${LINKLIBS} cppbenchmark)
180183
list(APPEND INSTALL_TARGETS ${BENCHMARK_TARGET})
181184
list(APPEND INSTALL_TARGETS_PDB ${BENCHMARK_TARGET})
@@ -186,7 +189,7 @@ if(NOT CPPSERIALIZATION_MODULE)
186189
file(GLOB TESTS_INLINE_FILES "tests/*.inl")
187190
file(GLOB TESTS_SOURCE_FILES "tests/*.cpp")
188191
add_executable(cppserialization-tests ${TESTS_HEADER_FILES} ${TESTS_INLINE_FILES} ${TESTS_SOURCE_FILES} ${Catch2})
189-
set_target_properties(cppserialization-tests PROPERTIES COMPILE_FLAGS "${PEDANTIC_COMPILE_FLAGS}" FOLDER "tests")
192+
set_target_properties(cppserialization-tests PROPERTIES COMPILE_FLAGS "${PEDANTIC_COMPILE_FLAGS} -Wno-shadow" FOLDER "tests")
190193
target_include_directories(cppserialization-tests PRIVATE ${Catch2})
191194
target_link_libraries(cppserialization-tests ${LINKLIBS})
192195
list(APPEND INSTALL_TARGETS cppserialization-tests)

examples/zpp_bits.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*!
2+
\file zpp_bits.cpp
3+
\brief zpp_bits serialization example
4+
\author Ivan Shynkarenka
5+
\date 30.03.2017
6+
\copyright MIT License
7+
*/
8+
9+
#include "../proto/trade.h"
10+
#include "zpp_bits/zpp_bits.h"
11+
12+
#include <iostream>
13+
14+
namespace TradeProto
15+
{
16+
auto serialize(const Order &) -> zpp::bits::members<6>;
17+
auto serialize(const Account &) -> zpp::bits::members<4>;
18+
auto serialize(const Balance &) -> zpp::bits::members<2>;
19+
}
20+
21+
int main(int argc, char** argv)
22+
{
23+
// Create a new account with some orders
24+
TradeProto::Account account(1, "Test", "USD", 1000);
25+
account.Orders.emplace_back(TradeProto::Order(1, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::MARKET, 1.23456, 1000));
26+
account.Orders.emplace_back(TradeProto::Order(2, "EURUSD", TradeProto::OrderSide::SELL, TradeProto::OrderType::LIMIT, 1.0, 100));
27+
account.Orders.emplace_back(TradeProto::Order(3, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::STOP, 1.5, 10));
28+
29+
// Serialize the account to bytes.
30+
auto [data, out] = zpp::bits::data_out();
31+
(void) out(account);
32+
33+
// Show the serialized size
34+
std::cout << "zpp_bits data size: " << data.size() << std::endl;
35+
36+
TradeProto::Account deserialized;
37+
(void) zpp::bits::in{data}(deserialized);
38+
39+
// Show account content
40+
std::cout << std::endl;
41+
std::cout << "Account.Id = " << deserialized.Id << std::endl;
42+
std::cout << "Account.Name = " << deserialized.Name << std::endl;
43+
std::cout << "Account.Wallet.Currency = " << deserialized.Wallet.Currency << std::endl;
44+
std::cout << "Account.Wallet.Amount = " << deserialized.Wallet.Amount << std::endl;
45+
for (const auto& order : deserialized.Orders)
46+
{
47+
std::cout << "Account.Order => Id: " << order.Id
48+
<< ", Symbol: " << order.Symbol
49+
<< ", Side: " << (int)order.Side
50+
<< ", Type: " << (int)order.Type
51+
<< ", Price: " << order.Price
52+
<< ", Volume: " << order.Volume
53+
<< std::endl;
54+
}
55+
56+
return 0;
57+
}

performance/zpp_deserialize.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Created by Ivan Shynkarenka on 30.03.2018
3+
//
4+
5+
#include "benchmark/cppbenchmark.h"
6+
7+
#include "../proto/trade.h"
8+
#include "zpp_bits/zpp_bits.h"
9+
10+
namespace TradeProto
11+
{
12+
auto serialize(const Order &) -> zpp::bits::members<6>;
13+
auto serialize(const Account &) -> zpp::bits::members<4>;
14+
auto serialize(const Balance &) -> zpp::bits::members<2>;
15+
}
16+
17+
class DeserializationFixture
18+
{
19+
protected:
20+
TradeProto::Account account;
21+
std::array<std::byte, 0x1000> buffer;
22+
23+
DeserializationFixture() : account(1, "Test", "USD", 1000)
24+
{
25+
// Create a new account with some orders
26+
account.Orders.emplace_back(1, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::MARKET, 1.23456, 1000);
27+
account.Orders.emplace_back(2, "EURUSD", TradeProto::OrderSide::SELL, TradeProto::OrderType::LIMIT, 1.0, 100);
28+
account.Orders.emplace_back(3, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::STOP, 1.5, 10);
29+
30+
// Serialize
31+
(void) zpp::bits::out{buffer}(account);
32+
}
33+
};
34+
35+
BENCHMARK_FIXTURE(DeserializationFixture, "ZppBits-Deserialize")
36+
{
37+
zpp::bits::in in{buffer};
38+
(void) in(account);
39+
context.metrics().AddBytes(in.position());
40+
context.metrics().SetCustom("Size", (unsigned)in.position());
41+
}
42+
43+
BENCHMARK_MAIN()

performance/zpp_serialize.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Created by Ivan Shynkarenka on 30.03.2018
3+
//
4+
5+
#include "benchmark/cppbenchmark.h"
6+
7+
#include "../proto/trade.h"
8+
#include "zpp_bits/zpp_bits.h"
9+
10+
namespace TradeProto
11+
{
12+
auto serialize(const Order &) -> zpp::bits::members<6>;
13+
auto serialize(const Account &) -> zpp::bits::members<4>;
14+
auto serialize(const Balance &) -> zpp::bits::members<2>;
15+
}
16+
17+
class SerializationFixture
18+
{
19+
protected:
20+
TradeProto::Account account;
21+
std::array<std::byte, 0x1000> buffer;
22+
23+
SerializationFixture() : account(1, "Test", "USD", 1000)
24+
{
25+
// Create a new account with some orders
26+
account.Orders.emplace_back(1, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::MARKET, 1.23456, 1000);
27+
account.Orders.emplace_back(2, "EURUSD", TradeProto::OrderSide::SELL, TradeProto::OrderType::LIMIT, 1.0, 100);
28+
account.Orders.emplace_back(3, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::STOP, 1.5, 10);
29+
}
30+
};
31+
32+
BENCHMARK_FIXTURE(SerializationFixture, "ZppBits-Serialize")
33+
{
34+
zpp::bits::out out{buffer};
35+
(void) out(account);
36+
context.metrics().AddBytes(out.position());
37+
context.metrics().SetCustom("Size", (unsigned)out.position());
38+
}
39+
40+
BENCHMARK_MAIN()

tests/test_zpp_bits.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// Created by Ivan Shynkarenka on 30.03.2017
3+
//
4+
5+
#include "test.h"
6+
7+
#include "../proto/trade.h"
8+
#include "zpp_bits/zpp_bits.h"
9+
10+
using namespace CppCommon;
11+
using namespace CppSerialization;
12+
13+
namespace TradeProto
14+
{
15+
auto serialize(const Order &) -> zpp::bits::members<6>;
16+
auto serialize(const Account &) -> zpp::bits::members<4>;
17+
auto serialize(const Balance &) -> zpp::bits::members<2>;
18+
}
19+
20+
TEST_CASE("ZppBits", "[CppSerialization]")
21+
{
22+
// Create a new account with some orders
23+
TradeProto::Account account(1, "Test", "USD", 1000);
24+
account.Orders.emplace_back(TradeProto::Order(1, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::MARKET, 1.23456, 1000));
25+
account.Orders.emplace_back(TradeProto::Order(2, "EURUSD", TradeProto::OrderSide::SELL, TradeProto::OrderType::LIMIT, 1.0, 100));
26+
account.Orders.emplace_back(TradeProto::Order(3, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::STOP, 1.5, 10));
27+
28+
// Serialize the account to bytes.
29+
auto [buffer, in, out] = zpp::bits::data_in_out();
30+
(void) out(account);
31+
32+
REQUIRE(!buffer.empty());
33+
34+
// Deserialize the account from bytes.
35+
TradeProto::Account deserialized;
36+
(void) in(deserialized);
37+
38+
REQUIRE(deserialized.Id == 1);
39+
REQUIRE(deserialized.Name == "Test");
40+
REQUIRE(std::string(deserialized.Wallet.Currency) == "USD");
41+
REQUIRE(deserialized.Wallet.Amount == 1000);
42+
REQUIRE(deserialized.Orders.size() == 3);
43+
REQUIRE(deserialized.Orders[0].Id == 1);
44+
REQUIRE(std::string(deserialized.Orders[0].Symbol) == "EURUSD");
45+
REQUIRE(deserialized.Orders[0].Side == TradeProto::OrderSide::BUY);
46+
REQUIRE(deserialized.Orders[0].Type == TradeProto::OrderType::MARKET);
47+
REQUIRE(deserialized.Orders[0].Price == 1.23456);
48+
REQUIRE(deserialized.Orders[0].Volume == 1000);
49+
REQUIRE(deserialized.Orders[1].Id == 2);
50+
REQUIRE(std::string(deserialized.Orders[1].Symbol) == "EURUSD");
51+
REQUIRE(deserialized.Orders[1].Side == TradeProto::OrderSide::SELL);
52+
REQUIRE(deserialized.Orders[1].Type == TradeProto::OrderType::LIMIT);
53+
REQUIRE(deserialized.Orders[1].Price == 1.0);
54+
REQUIRE(deserialized.Orders[1].Volume == 100);
55+
REQUIRE(deserialized.Orders[2].Id == 3);
56+
REQUIRE(std::string(deserialized.Orders[2].Symbol) == "EURUSD");
57+
REQUIRE(deserialized.Orders[2].Side == TradeProto::OrderSide::BUY);
58+
REQUIRE(deserialized.Orders[2].Type == TradeProto::OrderType::STOP);
59+
REQUIRE(deserialized.Orders[2].Price == 1.5);
60+
REQUIRE(deserialized.Orders[2].Volume == 10);
61+
}

0 commit comments

Comments
 (0)