From 9ae8b92ba549ab916c88e9004a95c1ed0cd16059 Mon Sep 17 00:00:00 2001 From: Joel Grunbaum <joelgrun@gmail.com> Date: Wed, 19 Jan 2022 10:48:57 +0000 Subject: [PATCH] Added loop to click trader and hit self for perf testing --- protocol.cpp | 133 ++++++++++++++++++++++++++++--------------- 1 files changed, 86 insertions(+), 47 deletions(-) diff --git a/protocol.cpp b/protocol.cpp index 0396038..f1083c0 100644 --- a/protocol.cpp +++ b/protocol.cpp @@ -22,6 +22,7 @@ mapAnnounce; std::string server = std::string(HOST) + ":" + std::string(PORT); httplib::Client cli("http://" + server); +std::unique_ptr<easywsclient::WebSocket> ws; double lastime = 0; @@ -57,32 +58,44 @@ return bs; } -void catchUp(std::unordered_map<std::string, book::Book>& bs) +void createWebSocket() { - static std::unique_ptr<easywsclient::WebSocket> ws( - easywsclient::WebSocket::from_url("ws://" + server + "/information")); - std::string feed; - bool gotMessage = false; + ws = std::unique_ptr<easywsclient::WebSocket>( + easywsclient::WebSocket::pointer(easywsclient::WebSocket::from_url( + "ws://" + server + "/information"))); ws->poll(); - ws->dispatch([gotMessageOut = &gotMessage, messageOut = &feed, - ws = ws.get()](const std::string& message) { - *gotMessageOut = true; - *messageOut = message; - }); - if (gotMessage) { - std::queue<json::Message*> a(json::parse(feed)); - while (!a.empty()) { - if (static_cast<json::FromExchange*>(a.front()) != nullptr || - static_cast<json::FromExchange*>(a.front())->timestamp > - lastime) { - lastime = - static_cast<json::FromExchange*>(a.front())->timestamp; +} + +std::deque<json::Message*> +catchUp(std::unordered_map<std::string, book::Book>& bs) +{ + std::string feed; + bool gotMessage; + std::deque<json::Message*> out; + do { + gotMessage = false; + ws->poll(); + ws->dispatch([gotMessageOut = &gotMessage, messageOut = &feed, + ws = ws.get()](const std::string& message) { + *gotMessageOut = true; + *messageOut = message; + }); + if (gotMessage) { + std::queue<json::Message*> a(json::parse(feed)); + while (!a.empty()) { + if (static_cast<json::FromExchange*>(a.front()) != nullptr || + static_cast<json::FromExchange*>(a.front())->timestamp > + lastime) { + lastime = + static_cast<json::FromExchange*>(a.front())->timestamp; + } + protocol::handleMessage(bs, a.front()); + out.push_back(a.front()); + a.pop(); } - protocol::handleMessage(bs, a.front()); - delete a.front(); - a.pop(); } - } + } while (gotMessage); + return out; } json::Message* addOrder(json::AddMessage& order) @@ -109,24 +122,24 @@ case json::SPREAD_TYPE: case json::CALL_TYPE: case json::PUT_TYPE: - announce(bs, dynamic_cast<json::AnnounceMessage*>(message)); + announce(bs, static_cast<json::AnnounceMessage*>(message)); break; case json::SETTLEMENT: - settle(bs, dynamic_cast<json::SettleMessage*>(message)); + settle(bs, static_cast<json::SettleMessage*>(message)); break; case json::ADDED: - addedOrder(bs, dynamic_cast<json::AddedMessage*>(message)); + addedOrder(bs, static_cast<json::AddedMessage*>(message)); break; case json::DELETED: - deletedOrder(bs, dynamic_cast<json::DeletedMessage*>(message)); + deletedOrder(bs, static_cast<json::DeletedMessage*>(message)); break; case json::TRADE: - tradeOrder(bs, dynamic_cast<json::TradeMessage*>(message)); + tradeOrder(bs, static_cast<json::TradeMessage*>(message)); break; case json::BROKER_REQUEST: case json::BROKER_ACK: case json::BROKER_CONFIRM: - broker(bs, dynamic_cast<json::Broker*>(message)); + broker(bs, static_cast<json::Broker*>(message)); break; default:; } @@ -162,31 +175,57 @@ json::DeletedMessage* message) { if (message->side == book::Buy) { - bs[message->product].bidSide.erase(message->id); + for (auto i = bs[message->product].bidSide.begin(); i != bs[message->product].bidSide.end(); i++) { + if (i->id == message->id) { + bs[message->product].bidSide.erase(i); + break; + } + } } else { - bs[message->product].askSide.erase(message->id); + for (auto i = bs[message->product].askSide.begin(); i != bs[message->product].askSide.end(); i++) { + if (i->id == message->id) { + bs[message->product].askSide.erase(i); + break; + } + } } } void tradeOrder(std::unordered_map<std::string, book::Book>& bs, json::TradeMessage* message) { - if (bs.find(message->passiveOrder) != bs.end()) { - if (message->tradeType == json::BUY_AGGRESSOR) { - if (message->passiveOrderRemaining > 0) { - bs[message->product].askSide.at(message->passiveOrder).volume = - message->passiveOrderRemaining; - } else { - bs[message->product].askSide.erase(message->passiveOrder); - } - } else if (message->tradeType == json::SELL_AGGRESSOR) { - if (message->passiveOrderRemaining > 0) { - bs[message->product].bidSide.at(message->passiveOrder).volume = - message->passiveOrderRemaining; - } else { - bs[message->product].bidSide.erase(message->passiveOrder); - } - } - } + if (message->tradeType == json::BUY_AGGRESSOR) { + if (message->passiveOrderRemaining > 0) { + for (auto& i : bs[message->product].askSide) { + if (i.id == message->passiveOrder) { + i.volume = message->passiveOrderRemaining; + break; + } + } + } else { + for (auto i = bs[message->product].askSide.begin(); i != bs[message->product].askSide.end(); i++) { + if (i->id == message->passiveOrder) { + bs[message->product].askSide.erase(i); + break; + } + } + } + } else if (message->tradeType == json::SELL_AGGRESSOR) { + if (message->passiveOrderRemaining > 0) { + for (auto& i : bs[message->product].bidSide) { + if (i.id == message->passiveOrder) { + i.volume = message->passiveOrderRemaining; + break; + } + } + } else { + for (auto i = bs[message->product].bidSide.begin(); i != bs[message->product].bidSide.end(); i++) { + if (i->id == message->passiveOrder) { + bs[message->product].bidSide.erase(i); + break; + } + } + } + } } void broker(std::unordered_map<std::string, book::Book>& bs, -- Gitblit v1.9.3