-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
157 lines (105 loc) · 5.58 KB
/
main.cpp
File metadata and controls
157 lines (105 loc) · 5.58 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "OrderBook.h"
#include <chrono>
#include <iomanip>
#include <iostream>
#include <random>
#include <thread>
#include <vector>
static constexpr uint64_t TOTAL_ORDERS = 1'000'000;
static constexpr uint64_t NUM_THREADS = 4;
static constexpr uint64_t PRICE_MIN = 9'500;
static constexpr uint64_t PRICE_MAX = 10'500;
// Quantity range per order
static constexpr uint64_t QTY_MIN = 1;
static constexpr uint64_t QTY_MAX = 100;
void producerTask(OrderBook* book,
uint64_t ordersToGenerate,
uint32_t threadSeed)
{
std::mt19937_64 rng(threadSeed);
std::uniform_int_distribution<uint64_t> priceDist(PRICE_MIN, PRICE_MAX);
std::uniform_int_distribution<uint64_t> qtyDist (QTY_MIN, QTY_MAX);
std::uniform_int_distribution<int> typeDist (0, 1);
for (uint64_t i = 0; i < ordersToGenerate; ++i) {
Order order(
engine::nextOrderId(),
typeDist(rng) == 0 ? OrderType::BUY
: OrderType::SELL,
priceDist(rng),
qtyDist(rng),
std::chrono::high_resolution_clock::now()
);
book->processOrder(order);
}
}
std::string formatNumber(uint64_t n) {
std::string s = std::to_string(n);
int insertPos = static_cast<int>(s.size()) - 3;
while (insertPos > 0) {
s.insert(static_cast<size_t>(insertPos), ",");
insertPos -= 3;
}
return s;
}
int main() {
std::cout << "\n";
std::cout << " ╔══════════════════════════════════════════════════╗\n";
std::cout << " ║ Real-Time Order Matching Engine v1.0 ║\n";
std::cout << " ║ C++17 | HFT Portfolio Project ║\n";
std::cout << " ╚══════════════════════════════════════════════════╝\n\n";
std::cout << " Configuration:\n";
std::cout << " Total orders : " << formatNumber(TOTAL_ORDERS) << "\n";
std::cout << " Producer threads : " << NUM_THREADS << "\n";
std::cout << " Price range : [" << PRICE_MIN << ", " << PRICE_MAX << "] cents\n";
std::cout << " Qty range : [" << QTY_MIN << ", " << QTY_MAX << "]\n\n";
OrderBook book;
uint64_t baseLoad = TOTAL_ORDERS / NUM_THREADS;
uint64_t remainder = TOTAL_ORDERS % NUM_THREADS;
std::vector<std::thread> threads;
threads.reserve(NUM_THREADS);
auto startTime = std::chrono::high_resolution_clock::now();
std::cout << " Spawning " << NUM_THREADS
<< " producer threads… (trade log below)\n";
std::cout << " ─────────────────────────────────────────────────\n";
for (uint64_t t = 0; t < NUM_THREADS; ++t) {
uint64_t load = baseLoad + (t == NUM_THREADS - 1 ? remainder : 0);
uint32_t seed = static_cast<uint32_t>((t + 1) * 0xDEAD'BEEF);
threads.emplace_back(producerTask, &book, load, seed);
}
for (auto& th : threads) {
th.join();
}
auto endTime = std::chrono::high_resolution_clock::now();
double elapsedMs =
std::chrono::duration<double, std::milli>(endTime - startTime).count();
double elapsedSec = elapsedMs / 1000.0;
double throughput = static_cast<double>(TOTAL_ORDERS) / elapsedSec;
std::cout << "\n";
std::cout << " ══════════════════════════════════════════════════\n";
std::cout << " BENCHMARK RESULTS\n";
std::cout << " ══════════════════════════════════════════════════\n";
std::cout << std::fixed << std::setprecision(3);
std::cout << " Total orders submitted : "
<< formatNumber(TOTAL_ORDERS) << "\n";
std::cout << " Orders processed : "
<< formatNumber(book.ordersProcessed()) << "\n";
std::cout << " Trades executed : "
<< formatNumber(book.tradesExecuted()) << "\n";
std::cout << " Remaining buy depth : "
<< formatNumber(static_cast<uint64_t>(book.buyBookDepth())) << "\n";
std::cout << " Remaining sell depth : "
<< formatNumber(static_cast<uint64_t>(book.sellBookDepth())) << "\n";
std::cout << " Best bid (cents) : "
<< book.bestBid() << "\n";
std::cout << " Best ask (cents) : "
<< book.bestAsk() << "\n";
std::cout << " ─────────────────────────────────────────────\n";
std::cout << " Elapsed time : "
<< elapsedMs << " ms\n";
std::cout << " Throughput : "
<< formatNumber(static_cast<uint64_t>(throughput))
<< " orders/sec\n";
std::cout << " ══════════════════════════════════════════════════\n\n";
std::cout << " Tip: recompile with -O3 -march=native for peak throughput.\n\n";
return 0;
}