This sample demonstrates how to measure latency and use the warm-up feature.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <iostream>
26
28
31
40
42
43int main(
int argc,
char* argv[])
44{
45
54 > cfg{"Benchmark", argc, argv};
55
56 try
57 {
58 const auto numberOfMessages = cfg.numberOfMessages();
59 const auto warmupInterval = cfg.warmupInterval();
60 const auto intervalBetweenSending = (std::max)(cfg.intervalBetweenSending(), warmupInterval);
61 const auto storageType = cfg.storage();
62 const auto receivingThreadAffinity = cfg.receivingThreadCpu();
63
66
69
70
72
73 settings
74 .receiveSpinningTimeout(1000 * 1000)
75 .useSpinLock(true)
76 .logBeforeSending(false);
77
79 {
80 public:
81 explicit BenchmarkListener(size_t numberOfMessages)
83 {}
84
85 using SessionListener::onError;
86
87 void onError(
88 SessionErrorReason::Enum reason, const std::string & description, Session * session, Messaging::SbeMessage msg) override
89 {
91 receivedAllMessages_.set_exception(std::make_exception_ptr(std::runtime_error(description)));
92 }
93
95 void collectSessionTimeMarks(Session & session, MessageHolder<NewOrderRequest> & order,
96 size_t numberOfMessages, size_t sendPeriodUsec, size_t warmupPeriodUsec)
97 {
98 receivedAllMessages_ = std::promise<void>{};
99
100 resetBenchmark();
101
102 auto sendingTime = UtcWatch::now();
103
104 for(uint64_t messageCounter = 0; messageCounter < numberOfMessages; ++messageCounter)
105 {
106 order->orderQty(messageCounter + 1);
107 order->clOrdId(messageCounter);
108
109 beforeSending();
110 session.
send(order, sendingTime);
111 afterSending();
112
113 sendingTime = UtcWatch::now();
114
115 if(warmupPeriodUsec)
116 {
118
119 do
120 {
121 ThisThread::spinWait(warmupPeriodUsec);
122 session.
warmUp(order, sendingTime);
123 }
125 }
126 else if(sendPeriodUsec)
127 {
128 ThisThread::spinWait(sendPeriodUsec);
129 }
130 }
131
132 waitUntilReceivedAllMessages();
133 }
134
135 private:
136 void onReceivedAllMessages() override
137 {
139 receivedAllMessages_.set_value();
140 }
141
142 void waitUntilReceivedAllMessages()
143 {
144 auto future = receivedAllMessages_.get_future();
145
146 if(future.wait_for(std::chrono::seconds{120}) != std::future_status::ready)
147 receivedAllMessages_.set_exception(std::make_exception_ptr(std::runtime_error("Operation timed out.")));
148
149 future.get();
150 }
151
152 std::promise<void> receivedAllMessages_;
153 }
154 listener{numberOfMessages};
155
157
159
161
162 listener.measureOverhead();
163
164 bgwSession->
165 receivingThreadAffinity(receivingThreadAffinity)
167 .connect(bgwCredentials.host(), bgwCredentials.port(), bgwCredentials.ipSessionToken());
168
170
171 std::clog << "\nWarm-up phase to make first calls faster..." << std::endl;
172 listener.collectSessionTimeMarks(*bgwSession, order, numberOfMessages, intervalBetweenSending, warmupInterval);
173
174 std::clog << "\nMeasurement phase..." << std::endl;
175 listener.collectSessionTimeMarks(*bgwSession, order, numberOfMessages, intervalBetweenSending, warmupInterval);
176
178 const bool hasTcpInfo = bgwSession->getTcpInfo(tcpInfo);
179
180 bgwSession->disconnect();
181
182 if(listener.packetGroupingDetected())
183 std::clog << "Attention: packet grouping detected! Increase the interval between sending!" << std::endl;
184
186
187 if(hasTcpInfo)
188 std::clog <<
"\n\n" << tcpInfo.
toString();
189 }
190 catch (const std::exception& ex)
191 {
192 std::cerr << "\nEXCEPTION: " << ex.what() << std::endl;
193 return 1;
194 }
195
196 return 0;
197}
198
#define ONIXS_ICEBOE_NAMESPACE
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE
int main(int argc, char *argv[])
#define ONIXS_ICEBOE_HOTPATH
void onError(SessionErrorReason::Enum, const std::string &description, Session *, Messaging::SbeMessage) override
virtual void onReceivedAllMessages()
Session & warmUp(Messaging::MessageHolder< SbeMessageType, MaxMessageSize > &msg, Messaging::Timestamp sendingTime=UtcWatch::now(), int warmupFlags=0)
Warms up the sending path.
Session & send(Messaging::MessageHolder< SbeMessageType, MaxMessageSize, MessageInitializer > &msg, Messaging::Timestamp sendingTime=UtcWatch::now())
Sends the message.
static void affinity(const CpuIndexes &cpuIndexes)
Sets the processor affinity mask for the current thread. The thread is rescheduled after this call.
static MessageHolder< NewOrderRequest > createOrder(const std::string &traderId)
constexpr Threading::CpuIndex AuxiliarySendingThreadAffinity
std::shared_ptr< void > setPriorityAndPolicy(Session *session=nullptr)
std::pair< std::unique_ptr< GatewayEmulatorThread< BusSessionGatewayListener > >, std::unique_ptr< GatewayEmulatorThread< GatewayListener > > > createEmulator(const SessionSettings &settings, const ConnectivityConfiguration &cfg, bool tcpDirect=false)
BgwCredentials receiveBgwCredentials(SessionSettings settings, std::string host, Port port)
SessionSettings fillSettings(const LogonConfiguration &logonCfg, const ConnectivityConfiguration &connCfg, const SettingsConfiguration &settingsCfg)
void printBenchmarkSettings(std::ostream &o, size_t numberOfMessages, size_t intervalBetweenSending, size_t warmupInterval, SessionStorageType::Enum storageType)
std::unique_ptr< T > makeUnique(Args &&...args)
static void reportResults(const std::string &name, const SendMarks *sendMarksArray, const ReceiveMarks *receiveMarksArray, std::size_t count, const Overhead &overhead)
std::string toString() const
static void manageSignals() noexcept