OnixS C++ ICE Binary Order Entry Handler 1.1.1
API Documentation
Loading...
Searching...
No Matches
TCPDirectBenchmark.cpp
Go to the documentation of this file.
1/*
2 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3 *
4 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5 * and international copyright treaties.
6 *
7 * Access to and use of the software is governed by the terms of the applicable ONIXS Software
8 * Services Agreement (the Agreement) and Customer end user license agreements granting
9 * a non-assignable, non-transferable and non-exclusive license to use the software
10 * for it's own data processing purposes under the terms defined in the Agreement.
11 *
12 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13 * of this source code or associated reference material to any other location for further reproduction
14 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15 *
16 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 * the terms of the Agreement is a violation of copyright law.
18 */
19// Uncomment to disable using of huge pages.
20//#define ONIXS_USE_HUGE_PAGE 0
21
22// Uncomment to enable debug mode.
23//#define ONIXS_DEBUGGING
24
26
27using namespace ONIXS_ICEBOE_NAMESPACE;
29
30#include <Settings/Defaults.h>
31#include <Common/Helpers.h>
32#include <Common/Listener.h>
33#include <Common/Settings.h>
34#include <Common/Signal.h>
35#include <Common/Emulator.h>
37
38#include <algorithm>
39
40using namespace Samples;
41
42
43int main(int argc, char* argv[])
44{
45 // `--help` to show options.
46 const AppConfiguration<
54 > cfg{"TCPDirectBenchmark", argc, argv};
55
56 int result = 0;
57 try
58 {
59 const auto numberOfMessages = cfg.numberOfMessages();
60 const auto warmupInterval = cfg.warmupInterval();
61 const auto intervalBetweenSending = (std::max)(cfg.intervalBetweenSending(), warmupInterval);
62 const auto storageType = cfg.storage();
63
64 ThisThread::affinity(cfg.mainThreadCpu());
66
67 auto settings = fillSettings(cfg);
68 const auto emulator = createEmulator(settings, cfg, true);
69
70 // Request Binary Order Gateway credentials from Binary Utility Service Gateway.
71 const auto bgwCredentials = receiveBgwCredentials(settings, cfg.host(), cfg.port());
72
73 settings
74 .receiveSpinningTimeout(1000 * 1000)
75 .useSpinLock(true)
76 .logBeforeSending(false)
77 .threadingModel(ThreadingModel::External);
78
79 printBenchmarkSettings(std::clog, numberOfMessages, intervalBetweenSending, warmupInterval, storageType);
80
81 const auto guard = setPriorityAndPolicy();
82
83 TcpDirectAttr attr;
84
85 // The default interface name could be provided by the environment variable
86 // ZF_ATTR="interface=<interface-name>", see TCPDirect specs.
87 attr.set("interface", cfg.nif());
88
89 TcpDirectStack stack{attr};
90
91 ReactorBenchmarkListener listener{numberOfMessages};
92 listener.measureOverhead();
93
94 try
95 {
96 const auto bgwSession = std::make_unique<BgwSession>(stack, settings, &listener, cfg.storage());
97
98 const auto connected = bgwSession->connectAsync(bgwCredentials);
99
100 while(!connected.is_ready())
101 stack.dispatchEvents();
102
103 if(connected.has_exception())
104 connected.get();
105
106 auto order = Helper::createOrder(cfg.traderId());
107
108 std::clog << "\nWarm-up phase to make first calls faster..." << std::endl;
109
111 stack, *bgwSession, order, numberOfMessages, intervalBetweenSending, warmupInterval);
112
113 if(listener.receivedAllMessages())
114 {
115 std::clog << "\nMeasurement phase..." << std::endl;
116
118 stack, *bgwSession, order, numberOfMessages, intervalBetweenSending, warmupInterval);
119 }
120
121 const auto disconnected = bgwSession->disconnectAsync();
122
123 while(!disconnected.is_ready())
124 stack.dispatchEvents();
125
126 if(disconnected.has_exception())
127 disconnected.get();
128 }
129 catch(const std::exception& ex)
130 {
131 std::cerr << "\nEXCEPTION: " << ex.what() << std::endl;
132 result = 1;
133 }
134
135 while(!stack.isQuiescent())
136 stack.dispatchEvents();
137
138 if(listener.receivedAllMessages())
139 {
140 if(listener.packetGroupingDetected())
141 std::clog << "Attention: packet grouping detected! Increase the interval between sending!" << std::endl;
142
143 BenchmarkData::reportResults("Latency", listener.sendMarks(), listener.receiveMarks(), numberOfMessages, listener.overhead());
144 }
145 }
146 catch (const std::exception& ex)
147 {
148 std::cerr << "\nEXCEPTION: " << ex.what() << std::endl;
149 result = 1;
150 }
151
152 return result;
153}
154
#define ONIXS_ICEBOE_NAMESPACE
Definition ABI.h:113
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE
Definition ABI.h:114
int main(int argc, char *argv[])
const BenchmarkData::SendMarks * sendMarks() const noexcept
const BenchmarkData::ReceiveMarks * receiveMarks() const noexcept
const BenchmarkData::Overhead & overhead() const noexcept
bool packetGroupingDetected() const noexcept
TCPDirect Attributes to pass configuration details (a wrapper around the zf_attr struct).
void set(const std::string &name, const std::string &value)
Sets the attribute to the given value.
A high-level wrapper over the TCPDirect network stack.
bool isQuiescent() const noexcept override
void dispatchEvents() noexcept override
This function processes events on a stack and performs the necessary handling.
static void affinity(const CpuIndexes &cpuIndexes)
Sets the processor affinity mask for the current thread. The thread is rescheduled after this call.
bool receivedAllMessages() const noexcept
void collectSessionTimeMarks(Stack &stack, Session &session, MessageHolder< NewOrderRequest > &order, size_t numberOfMessages, size_t sendPeriod, size_t warmupPeriod)
static MessageHolder< NewOrderRequest > createOrder(const std::string &traderId)
Definition Helpers.h:104
std::shared_ptr< void > setPriorityAndPolicy(Session *session=nullptr)
Definition Helpers.h:150
std::pair< std::unique_ptr< GatewayEmulatorThread< BusSessionGatewayListener > >, std::unique_ptr< GatewayEmulatorThread< GatewayListener > > > createEmulator(const SessionSettings &settings, const ConnectivityConfiguration &cfg, bool tcpDirect=false)
Definition Emulator.h:157
BgwCredentials receiveBgwCredentials(SessionSettings settings, std::string host, Port port)
Definition Listener.h:264
SessionSettings fillSettings(const LogonConfiguration &logonCfg, const ConnectivityConfiguration &connCfg, const SettingsConfiguration &settingsCfg)
Definition Settings.h:32
void printBenchmarkSettings(std::ostream &o, size_t numberOfMessages, size_t intervalBetweenSending, size_t warmupInterval, SessionStorageType::Enum storageType)
Definition Helpers.h:182
static void reportResults(const std::string &name, const SendMarks *sendMarksArray, const ReceiveMarks *receiveMarksArray, std::size_t count, const Overhead &overhead)
@ External
Session events are handled by rolling an event loop in a user thread.
static void manageSignals() noexcept
Definition Signal.h:46