Repository navigation
Expand file tree
/
Copy pathbenchmarks.cpp
More file actions
181 lines (155 loc) · 6.75 KB
/
Copy pathbenchmarks.cpp
File metadata and controls
181 lines (155 loc) · 6.75 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <benchmark/benchmark.h>
#include "Orderbook.h"
#include <random>
#include <vector>
#include <algorithm>
#include <chrono>
#include <numeric>
// Pre-generate RNG data to eliminate overhead from the timer loop
struct AddAction {
Side side;
Price price;
Quantity qty;
};
std::vector<AddAction> GenerateRandomOrders(size_t n, int seed = 42) {
std::mt19937 gen(seed);
std::uniform_int_distribution<Price> priceDist(90, 110);
std::uniform_int_distribution<Quantity> qtyDist(1, 100);
std::uniform_int_distribution<int> sideDist(0, 1);
std::vector<AddAction> actions;
actions.reserve(n);
for (size_t i = 0; i < n; ++i) {
actions.push_back({
sideDist(gen) ? Side::Buy : Side::Sell,
priceDist(gen),
qtyDist(gen)
});
}
return actions;
}
// ── BM_AddOrders ──────────────────────────────────────────────────────────
static void BM_AddOrders(benchmark::State& state) {
auto actions = GenerateRandomOrders(state.range(0));
double total_p99 = 0.0;
double total_avg = 0.0;
for (auto _ : state) {
state.PauseTiming();
Orderbook orderbook;
OrderId id = 0;
std::vector<double> latencies;
latencies.reserve(state.range(0));
state.ResumeTiming();
for (const auto& action : actions) {
auto t1 = std::chrono::high_resolution_clock::now();
orderbook.AddOrder(OrderType::GoodTillCancel, ++id, action.side, action.price, action.qty);
auto t2 = std::chrono::high_resolution_clock::now();
latencies.push_back(std::chrono::duration<double, std::nano>(t2 - t1).count());
}
benchmark::DoNotOptimize(orderbook);
state.PauseTiming();
std::sort(latencies.begin(), latencies.end());
double p99 = latencies[static_cast<size_t>(latencies.size() * 0.99)];
double sum = std::accumulate(latencies.begin(), latencies.end(), 0.0);
double avg = sum / latencies.size();
total_p99 += p99;
total_avg += avg;
state.ResumeTiming();
}
state.SetItemsProcessed(state.iterations() * state.range(0));
state.counters["Avg_ns"] = total_avg / state.iterations();
state.counters["P99_ns"] = total_p99 / state.iterations();
}
BENCHMARK(BM_AddOrders)->RangeMultiplier(10)->Range(1000, 100000)->Unit(benchmark::kMillisecond);
// ── BM_CancelOrders ───────────────────────────────────────────────────────
static void BM_CancelOrders(benchmark::State& state) {
size_t numOrders = state.range(0);
auto actions = GenerateRandomOrders(numOrders);
double total_p99 = 0.0;
double total_avg = 0.0;
for (auto _ : state) {
state.PauseTiming();
Orderbook orderbook;
OrderId id = 0;
std::vector<OrderId> activeIds;
activeIds.reserve(numOrders);
std::vector<double> latencies;
latencies.reserve(numOrders);
for (const auto& action : actions) {
OrderId oid = ++id;
orderbook.AddOrder(OrderType::GoodTillCancel, oid, action.side, action.price, action.qty);
if (orderbook.HasOrder(oid)) activeIds.push_back(oid);
}
state.ResumeTiming();
// Time the cancellations
for (OrderId oid : activeIds) {
auto t1 = std::chrono::high_resolution_clock::now();
orderbook.CancelOrder(oid);
auto t2 = std::chrono::high_resolution_clock::now();
latencies.push_back(std::chrono::duration<double, std::nano>(t2 - t1).count());
}
benchmark::DoNotOptimize(orderbook);
state.PauseTiming();
if (!latencies.empty()) {
std::sort(latencies.begin(), latencies.end());
double p99 = latencies[static_cast<size_t>(latencies.size() * 0.99)];
double sum = std::accumulate(latencies.begin(), latencies.end(), 0.0);
double avg = sum / latencies.size();
total_p99 += p99;
total_avg += avg;
}
state.ResumeTiming();
}
state.SetItemsProcessed(state.iterations() * state.range(0));
state.counters["Avg_ns"] = total_avg / state.iterations();
state.counters["P99_ns"] = total_p99 / state.iterations();
}
BENCHMARK(BM_CancelOrders)->RangeMultiplier(10)->Range(1000, 100000)->Unit(benchmark::kMicrosecond);
// ── BM_MatchOrders ────────────────────────────────────────────────────────
static void BM_MatchOrders(benchmark::State& state) {
size_t numOrders = state.range(0);
// Build a resting book
std::mt19937 gen(42);
std::uniform_int_distribution<Quantity> qtyDist(1, 100);
double total_p99 = 0.0;
double total_avg = 0.0;
for (auto _ : state) {
state.PauseTiming();
Orderbook orderbook;
OrderId id = 0;
std::vector<double> latencies;
latencies.reserve(numOrders / 2);
// Add passive buys
for (size_t i = 0; i < numOrders / 2; ++i) {
orderbook.AddOrder(OrderType::GoodTillCancel, ++id, Side::Buy, 100, qtyDist(gen));
}
// Pre-generate aggressive sells
std::vector<Quantity> aggressiveQts;
aggressiveQts.reserve(numOrders / 2);
for (size_t i = 0; i < numOrders / 2; ++i) {
aggressiveQts.push_back(qtyDist(gen));
}
state.ResumeTiming();
for (Quantity q : aggressiveQts) {
auto t1 = std::chrono::high_resolution_clock::now();
orderbook.AddOrder(OrderType::GoodTillCancel, ++id, Side::Sell, 100, q);
auto t2 = std::chrono::high_resolution_clock::now();
latencies.push_back(std::chrono::duration<double, std::nano>(t2 - t1).count());
}
benchmark::DoNotOptimize(orderbook);
state.PauseTiming();
if (!latencies.empty()) {
std::sort(latencies.begin(), latencies.end());
double p99 = latencies[static_cast<size_t>(latencies.size() * 0.99)];
double sum = std::accumulate(latencies.begin(), latencies.end(), 0.0);
double avg = sum / latencies.size();
total_p99 += p99;
total_avg += avg;
}
state.ResumeTiming();
}
state.SetItemsProcessed(state.iterations() * (state.range(0) / 2));
state.counters["Avg_ns"] = total_avg / state.iterations();
state.counters["P99_ns"] = total_p99 / state.iterations();
}
BENCHMARK(BM_MatchOrders)->RangeMultiplier(10)->Range(1000, 100000)->Unit(benchmark::kMillisecond);
BENCHMARK_MAIN();