-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added zpp_bits performance benchmark.
- Loading branch information
Showing
6 changed files
with
208 additions
and
3 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
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,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; | ||
} |
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,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() |
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,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() |
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,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); | ||
} |