Skip to content

Commit

Permalink
Added zpp_bits performance benchmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalz800 committed Aug 23, 2022
1 parent a7b6a87 commit 7004112
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitlinks
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CppCommon modules/CppCommon https://github.com/chronoxor/CppCommon.git master
flatbuffers modules/flatbuffers https://github.com/google/flatbuffers.git master
protobuf modules/protobuf https://github.com/google/protobuf.git 21.x
rapidjson modules/rapidjson https://github.com/miloyip/rapidjson.git master
zpp_bits modules/zpp_bits https://github.com/eyalz800/zpp_bits.git v4.4.10

# Scripts
build build https://github.com/chronoxor/CppBuildScripts.git master
Expand Down
9 changes: 6 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ endif()
# CMake module path
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

# C++20
set(CMAKE_CXX_STANDARD 20)

# Compiler features
include(SetCompilerFeatures)
include(SetCompilerWarnings)
Expand Down Expand Up @@ -162,7 +165,7 @@ if(NOT CPPSERIALIZATION_MODULE)
string(REGEX REPLACE "(.*)\\.cpp" "\\1" EXAMPLE_NAME ${EXAMPLE_SOURCE_FILE})
set(EXAMPLE_TARGET "cppserialization-example-${EXAMPLE_NAME}")
add_executable(${EXAMPLE_TARGET} ${EXAMPLE_HEADER_FILES} ${EXAMPLE_INLINE_FILES} "examples/${EXAMPLE_SOURCE_FILE}")
set_target_properties(${EXAMPLE_TARGET} PROPERTIES COMPILE_FLAGS "${PEDANTIC_COMPILE_FLAGS}" FOLDER "examples")
set_target_properties(${EXAMPLE_TARGET} PROPERTIES COMPILE_FLAGS "${PEDANTIC_COMPILE_FLAGS} -Wno-shadow" FOLDER "examples")
target_link_libraries(${EXAMPLE_TARGET} ${LINKLIBS})
list(APPEND INSTALL_TARGETS ${EXAMPLE_TARGET})
list(APPEND INSTALL_TARGETS_PDB ${EXAMPLE_TARGET})
Expand All @@ -176,7 +179,7 @@ if(NOT CPPSERIALIZATION_MODULE)
string(REGEX REPLACE "(.*)\\.cpp" "\\1" BENCHMARK_NAME ${BENCHMARK_SOURCE_FILE})
set(BENCHMARK_TARGET "cppserialization-performance-${BENCHMARK_NAME}")
add_executable(${BENCHMARK_TARGET} ${BENCHMARK_HEADER_FILES} ${BENCHMARK_INLINE_FILES} "performance/${BENCHMARK_SOURCE_FILE}")
set_target_properties(${BENCHMARK_TARGET} PROPERTIES COMPILE_FLAGS "${PEDANTIC_COMPILE_FLAGS}" FOLDER "performance")
set_target_properties(${BENCHMARK_TARGET} PROPERTIES COMPILE_FLAGS "${PEDANTIC_COMPILE_FLAGS} -Wno-shadow" FOLDER "performance")
target_link_libraries(${BENCHMARK_TARGET} ${LINKLIBS} cppbenchmark)
list(APPEND INSTALL_TARGETS ${BENCHMARK_TARGET})
list(APPEND INSTALL_TARGETS_PDB ${BENCHMARK_TARGET})
Expand All @@ -187,7 +190,7 @@ if(NOT CPPSERIALIZATION_MODULE)
file(GLOB TESTS_INLINE_FILES "tests/*.inl")
file(GLOB TESTS_SOURCE_FILES "tests/*.cpp")
add_executable(cppserialization-tests ${TESTS_HEADER_FILES} ${TESTS_INLINE_FILES} ${TESTS_SOURCE_FILES} ${Catch2})
set_target_properties(cppserialization-tests PROPERTIES COMPILE_FLAGS "${PEDANTIC_COMPILE_FLAGS}" FOLDER "tests")
set_target_properties(cppserialization-tests PROPERTIES COMPILE_FLAGS "${PEDANTIC_COMPILE_FLAGS} -Wno-shadow" FOLDER "tests")
target_include_directories(cppserialization-tests PRIVATE ${Catch2})
target_link_libraries(cppserialization-tests ${LINKLIBS})
list(APPEND INSTALL_TARGETS cppserialization-tests)
Expand Down
57 changes: 57 additions & 0 deletions examples/zpp_bits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*!
\file zpp_bits.cpp
\brief zpp_bits serialization example
\author Ivan Shynkarenka
\date 30.03.2017
\copyright MIT License
*/

#include "../proto/trade.h"
#include "zpp_bits/zpp_bits.h"

#include <iostream>

namespace TradeProto
{
auto serialize(const Order &) -> zpp::bits::members<6>;
auto serialize(const Account &) -> zpp::bits::members<4>;
auto serialize(const Balance &) -> zpp::bits::members<2>;
}

int main(int argc, char** argv)
{
// Create a new account with some orders
TradeProto::Account account(1, "Test", "USD", 1000);
account.Orders.emplace_back(TradeProto::Order(1, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::MARKET, 1.23456, 1000));
account.Orders.emplace_back(TradeProto::Order(2, "EURUSD", TradeProto::OrderSide::SELL, TradeProto::OrderType::LIMIT, 1.0, 100));
account.Orders.emplace_back(TradeProto::Order(3, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::STOP, 1.5, 10));

// Serialize the account to bytes.
auto [data, out] = zpp::bits::data_out();
(void) out(account);

// Show the serialized size
std::cout << "zpp_bits data size: " << data.size() << std::endl;

TradeProto::Account deserialized;
(void) zpp::bits::in{data}(deserialized);

// Show account content
std::cout << std::endl;
std::cout << "Account.Id = " << deserialized.Id << std::endl;
std::cout << "Account.Name = " << deserialized.Name << std::endl;
std::cout << "Account.Wallet.Currency = " << deserialized.Wallet.Currency << std::endl;
std::cout << "Account.Wallet.Amount = " << deserialized.Wallet.Amount << std::endl;
for (const auto& order : deserialized.Orders)
{
std::cout << "Account.Order => Id: " << order.Id
<< ", Symbol: " << order.Symbol
<< ", Side: " << (int)order.Side
<< ", Type: " << (int)order.Type
<< ", Price: " << order.Price
<< ", Volume: " << order.Volume
<< std::endl;
}

return 0;
}
43 changes: 43 additions & 0 deletions performance/zpp_deserialize.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Created by Ivan Shynkarenka on 30.03.2018
//

#include "benchmark/cppbenchmark.h"

#include "../proto/trade.h"
#include "zpp_bits/zpp_bits.h"

namespace TradeProto
{
auto serialize(const Order &) -> zpp::bits::members<6>;
auto serialize(const Account &) -> zpp::bits::members<4>;
auto serialize(const Balance &) -> zpp::bits::members<2>;
}

class DeserializationFixture
{
protected:
TradeProto::Account account;
std::array<std::byte, 0x1000> buffer;

DeserializationFixture() : account(1, "Test", "USD", 1000)
{
// Create a new account with some orders
account.Orders.emplace_back(1, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::MARKET, 1.23456, 1000);
account.Orders.emplace_back(2, "EURUSD", TradeProto::OrderSide::SELL, TradeProto::OrderType::LIMIT, 1.0, 100);
account.Orders.emplace_back(3, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::STOP, 1.5, 10);

// Serialize
(void) zpp::bits::out{buffer}(account);
}
};

BENCHMARK_FIXTURE(DeserializationFixture, "ZppBits-Deserialize")
{
zpp::bits::in in{buffer};
(void) in(account);
context.metrics().AddBytes(in.position());
context.metrics().SetCustom("Size", (unsigned)in.position());
}

BENCHMARK_MAIN()
40 changes: 40 additions & 0 deletions performance/zpp_serialize.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Created by Ivan Shynkarenka on 30.03.2018
//

#include "benchmark/cppbenchmark.h"

#include "../proto/trade.h"
#include "zpp_bits/zpp_bits.h"

namespace TradeProto
{
auto serialize(const Order &) -> zpp::bits::members<6>;
auto serialize(const Account &) -> zpp::bits::members<4>;
auto serialize(const Balance &) -> zpp::bits::members<2>;
}

class SerializationFixture
{
protected:
TradeProto::Account account;
std::array<std::byte, 0x1000> buffer;

SerializationFixture() : account(1, "Test", "USD", 1000)
{
// Create a new account with some orders
account.Orders.emplace_back(1, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::MARKET, 1.23456, 1000);
account.Orders.emplace_back(2, "EURUSD", TradeProto::OrderSide::SELL, TradeProto::OrderType::LIMIT, 1.0, 100);
account.Orders.emplace_back(3, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::STOP, 1.5, 10);
}
};

BENCHMARK_FIXTURE(SerializationFixture, "ZppBits-Serialize")
{
zpp::bits::out out{buffer};
(void) out(account);
context.metrics().AddBytes(out.position());
context.metrics().SetCustom("Size", (unsigned)out.position());
}

BENCHMARK_MAIN()
61 changes: 61 additions & 0 deletions tests/test_zpp_bits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// Created by Ivan Shynkarenka on 30.03.2017
//

#include "test.h"

#include "../proto/trade.h"
#include "zpp_bits/zpp_bits.h"

using namespace CppCommon;
using namespace CppSerialization;

namespace TradeProto
{
auto serialize(const Order &) -> zpp::bits::members<6>;
auto serialize(const Account &) -> zpp::bits::members<4>;
auto serialize(const Balance &) -> zpp::bits::members<2>;
}

TEST_CASE("ZppBits", "[CppSerialization]")
{
// Create a new account with some orders
TradeProto::Account account(1, "Test", "USD", 1000);
account.Orders.emplace_back(TradeProto::Order(1, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::MARKET, 1.23456, 1000));
account.Orders.emplace_back(TradeProto::Order(2, "EURUSD", TradeProto::OrderSide::SELL, TradeProto::OrderType::LIMIT, 1.0, 100));
account.Orders.emplace_back(TradeProto::Order(3, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::STOP, 1.5, 10));

// Serialize the account to bytes.
auto [buffer, in, out] = zpp::bits::data_in_out();
(void) out(account);

REQUIRE(!buffer.empty());

// Deserialize the account from bytes.
TradeProto::Account deserialized;
(void) in(deserialized);

REQUIRE(deserialized.Id == 1);
REQUIRE(deserialized.Name == "Test");
REQUIRE(std::string(deserialized.Wallet.Currency) == "USD");
REQUIRE(deserialized.Wallet.Amount == 1000);
REQUIRE(deserialized.Orders.size() == 3);
REQUIRE(deserialized.Orders[0].Id == 1);
REQUIRE(std::string(deserialized.Orders[0].Symbol) == "EURUSD");
REQUIRE(deserialized.Orders[0].Side == TradeProto::OrderSide::BUY);
REQUIRE(deserialized.Orders[0].Type == TradeProto::OrderType::MARKET);
REQUIRE(deserialized.Orders[0].Price == 1.23456);
REQUIRE(deserialized.Orders[0].Volume == 1000);
REQUIRE(deserialized.Orders[1].Id == 2);
REQUIRE(std::string(deserialized.Orders[1].Symbol) == "EURUSD");
REQUIRE(deserialized.Orders[1].Side == TradeProto::OrderSide::SELL);
REQUIRE(deserialized.Orders[1].Type == TradeProto::OrderType::LIMIT);
REQUIRE(deserialized.Orders[1].Price == 1.0);
REQUIRE(deserialized.Orders[1].Volume == 100);
REQUIRE(deserialized.Orders[2].Id == 3);
REQUIRE(std::string(deserialized.Orders[2].Symbol) == "EURUSD");
REQUIRE(deserialized.Orders[2].Side == TradeProto::OrderSide::BUY);
REQUIRE(deserialized.Orders[2].Type == TradeProto::OrderType::STOP);
REQUIRE(deserialized.Orders[2].Price == 1.5);
REQUIRE(deserialized.Orders[2].Volume == 10);
}

0 comments on commit 7004112

Please sign in to comment.