-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbench_observer_overhead.cpp
More file actions
393 lines (303 loc) · 12.8 KB
/
Copy pathbench_observer_overhead.cpp
File metadata and controls
393 lines (303 loc) · 12.8 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
* @file bench_observer_overhead.cpp
* @brief Performance benchmarks for Observer notification overhead
*
* Measures the cost of:
* - Observer notifications (different observer counts)
* - Notification dispatch latency
* - Impact on orderbook operations
* - Snapshot emission overhead
*
* Target: < 50ns per observer notification
*/
#include <slick/orderbook/orderbook.hpp>
#include <benchmark/benchmark.h>
#include <random>
#include <vector>
#include <atomic>
using namespace slick::orderbook;
// ============================================================================
// Test Observer Implementations
// ============================================================================
// Minimal observer that just counts notifications
class CountingObserver : public IOrderBookObserver {
public:
void onPriceLevelUpdate([[maybe_unused]] const PriceLevelUpdate& update) override {
++level_update_count_;
}
void onOrderUpdate([[maybe_unused]] const OrderUpdate& update) override {
++order_update_count_;
}
void onTrade([[maybe_unused]] const Trade& trade) override {
++trade_count_;
}
void onTopOfBookUpdate([[maybe_unused]] const TopOfBook& tob) override {
++tob_change_count_;
}
void onSnapshotBegin([[maybe_unused]] SymbolId symbol, [[maybe_unused]] uint64_t seq_num, [[maybe_unused]] Timestamp timestamp) override {
++snapshot_begin_count_;
}
void onSnapshotEnd([[maybe_unused]] SymbolId symbol, [[maybe_unused]] uint64_t seq_num, [[maybe_unused]] Timestamp timestamp) override {
++snapshot_end_count_;
}
size_t level_update_count_ = 0;
size_t order_update_count_ = 0;
size_t trade_count_ = 0;
size_t tob_change_count_ = 0;
size_t snapshot_begin_count_ = 0;
size_t snapshot_end_count_ = 0;
};
// Observer that does some computation
class ComputingObserver : public IOrderBookObserver {
public:
void onPriceLevelUpdate(const PriceLevelUpdate& update) override {
// Simulate some work
computed_value_ += update.price * update.quantity;
benchmark::DoNotOptimize(computed_value_);
}
void onOrderUpdate(const OrderUpdate& update) override {
computed_value_ += update.price * update.quantity;
benchmark::DoNotOptimize(computed_value_);
}
void onTrade(const Trade& trade) override {
computed_value_ += trade.price * trade.quantity;
benchmark::DoNotOptimize(computed_value_);
}
void onTopOfBookUpdate(const TopOfBook& tob) override {
if (tob.best_bid && tob.best_ask) {
computed_value_ += (tob.best_bid + tob.best_ask) / 2;
}
benchmark::DoNotOptimize(computed_value_);
}
void onSnapshotBegin([[maybe_unused]] SymbolId symbol, [[maybe_unused]] uint64_t seq_num, [[maybe_unused]] Timestamp timestamp) override {
computed_value_ = 0;
}
void onSnapshotEnd([[maybe_unused]] SymbolId symbol, [[maybe_unused]] uint64_t seq_num, [[maybe_unused]] Timestamp timestamp) override {
benchmark::DoNotOptimize(computed_value_);
}
int64_t computed_value_ = 0;
};
// ============================================================================
// Benchmark: L2 Operations - No Observers
// ============================================================================
static void BM_L2_NoObservers(benchmark::State& state) {
OrderBookL2 book(1);
std::mt19937_64 rng(12345);
std::uniform_int_distribution<Quantity> qty_dist(100, 10000);
size_t operations = 0;
for (auto _ : state) {
Price price = 100000 + (operations % 100) * 10;
Side side = (operations % 2 == 0) ? Side::Buy : Side::Sell;
book.updateLevel(side, price, qty_dist(rng), 0, 0);
++operations;
}
state.SetItemsProcessed(operations);
}
BENCHMARK(BM_L2_NoObservers);
// ============================================================================
// Benchmark: L2 Operations - With Counting Observers
// ============================================================================
static void BM_L2_WithCountingObservers(benchmark::State& state) {
OrderBookL2 book(1);
const auto num_observers = state.range(0);
// Add observers
std::vector<std::shared_ptr<CountingObserver>> observers;
for (auto i = 0; i < num_observers; ++i) {
observers.push_back(std::make_shared<CountingObserver>());
book.addObserver(observers.back());
}
std::mt19937_64 rng(12345);
std::uniform_int_distribution<Quantity> qty_dist(100, 10000);
size_t operations = 0;
for (auto _ : state) {
Price price = 100000 + (operations % 100) * 10;
Side side = (operations % 2 == 0) ? Side::Buy : Side::Sell;
book.updateLevel(side, price, qty_dist(rng), 0, 0);
++operations;
}
state.SetItemsProcessed(operations);
}
BENCHMARK(BM_L2_WithCountingObservers)->Arg(1)->Arg(5)->Arg(10)->Arg(50)->Arg(100);
// ============================================================================
// Benchmark: L2 Operations - With Computing Observers
// ============================================================================
static void BM_L2_WithComputingObservers(benchmark::State& state) {
OrderBookL2 book(1);
const auto num_observers = state.range(0);
// Add observers
std::vector<std::shared_ptr<ComputingObserver>> observers;
for (auto i = 0; i < num_observers; ++i) {
observers.push_back(std::make_shared<ComputingObserver>());
book.addObserver(observers.back());
}
std::mt19937_64 rng(12345);
std::uniform_int_distribution<Quantity> qty_dist(100, 10000);
size_t operations = 0;
for (auto _ : state) {
Price price = 100000 + (operations % 100) * 10;
Side side = (operations % 2 == 0) ? Side::Buy : Side::Sell;
book.updateLevel(side, price, qty_dist(rng), 0, 0);
++operations;
}
state.SetItemsProcessed(operations);
}
BENCHMARK(BM_L2_WithComputingObservers)->Arg(1)->Arg(5)->Arg(10)->Arg(50)->Arg(100);
// ============================================================================
// Benchmark: L3 Operations - No Observers
// ============================================================================
static void BM_L3_NoObservers(benchmark::State& state) {
OrderBookL3 book(1);
std::mt19937_64 rng(54321);
std::uniform_int_distribution<Quantity> qty_dist(100, 1000);
std::uniform_int_distribution<uint64_t> priority_dist(0, 1000000);
OrderId order_id = 1;
size_t operations = 0;
for (auto _ : state) {
Price price = 100000 + (operations % 20) * 10;
Side side = (operations % 2 == 0) ? Side::Buy : Side::Sell;
book.addOrModifyOrder(order_id++, side, price, qty_dist(rng), 0, priority_dist(rng), 0);
++operations;
if (operations % 10000 == 0) {
book = OrderBookL3(1);
order_id = 1;
}
}
state.SetItemsProcessed(operations);
}
BENCHMARK(BM_L3_NoObservers);
// ============================================================================
// Benchmark: L3 Operations - With Counting Observers
// ============================================================================
static void BM_L3_WithCountingObservers(benchmark::State& state) {
OrderBookL3 book(1);
const auto num_observers = state.range(0);
// Add observers
std::vector<std::shared_ptr<CountingObserver>> observers;
for (auto i = 0; i < num_observers; ++i) {
observers.push_back(std::make_shared<CountingObserver>());
book.addObserver(observers.back());
}
std::mt19937_64 rng(54321);
std::uniform_int_distribution<Quantity> qty_dist(100, 1000);
std::uniform_int_distribution<uint64_t> priority_dist(0, 1000000);
OrderId order_id = 1;
size_t operations = 0;
for (auto _ : state) {
Price price = 100000 + (operations % 20) * 10;
Side side = (operations % 2 == 0) ? Side::Buy : Side::Sell;
book.addOrModifyOrder(order_id++, side, price, qty_dist(rng), 0, priority_dist(rng), 0);
++operations;
if (operations % 10000 == 0) {
book = OrderBookL3(1);
for (auto& obs : observers) {
book.addObserver(obs);
}
order_id = 1;
}
}
state.SetItemsProcessed(operations);
}
BENCHMARK(BM_L3_WithCountingObservers)->Arg(1)->Arg(5)->Arg(10)->Arg(50)->Arg(100);
// ============================================================================
// Benchmark: L3 Operations - With Computing Observers
// ============================================================================
static void BM_L3_WithComputingObservers(benchmark::State& state) {
OrderBookL3 book(1);
const auto num_observers = state.range(0);
// Add observers
std::vector<std::shared_ptr<ComputingObserver>> observers;
for (auto i = 0; i < num_observers; ++i) {
observers.push_back(std::make_shared<ComputingObserver>());
book.addObserver(observers.back());
}
std::mt19937_64 rng(54321);
std::uniform_int_distribution<Quantity> qty_dist(100, 1000);
std::uniform_int_distribution<uint64_t> priority_dist(0, 1000000);
OrderId order_id = 1;
size_t operations = 0;
for (auto _ : state) {
Price price = 100000 + (operations % 20) * 10;
Side side = (operations % 2 == 0) ? Side::Buy : Side::Sell;
book.addOrModifyOrder(order_id++, side, price, qty_dist(rng), 0, priority_dist(rng), 0);
++operations;
if (operations % 10000 == 0) {
book = OrderBookL3(1);
for (auto& obs : observers) {
book.addObserver(obs);
}
order_id = 1;
}
}
state.SetItemsProcessed(operations);
}
BENCHMARK(BM_L3_WithComputingObservers)->Arg(1)->Arg(5)->Arg(10)->Arg(50)->Arg(100);
// ============================================================================
// Benchmark: Snapshot Emission - L2
// ============================================================================
static void BM_L2_EmitSnapshot(benchmark::State& state) {
OrderBookL2 book(1);
const auto num_levels = state.range(0);
// Build book with num_levels on each side
for (auto i = 0; i < num_levels; ++i) {
book.updateLevel(Side::Buy, 100000 - static_cast<Price>(i) * 10, 1000, 0, 0);
book.updateLevel(Side::Sell, 100100 + static_cast<Price>(i) * 10, 1000, 0, 0);
}
auto observer = std::make_shared<CountingObserver>();
book.addObserver(observer);
for (auto _ : state) {
book.emitSnapshot(0);
}
state.SetItemsProcessed(state.iterations());
}
BENCHMARK(BM_L2_EmitSnapshot)->Arg(10)->Arg(50)->Arg(100)->Arg(500);
// ============================================================================
// Benchmark: Snapshot Emission - L3
// ============================================================================
static void BM_L3_EmitSnapshot(benchmark::State& state) {
OrderBookL3 book(1);
const auto num_orders = state.range(0);
// Build book with num_orders
std::mt19937_64 rng(99999);
std::uniform_int_distribution<Quantity> qty_dist(100, 1000);
std::uniform_int_distribution<Price> price_offset_dist(0, 10);
std::uniform_int_distribution<uint64_t> priority_dist(0, 1000000);
for (auto i = 0; i < num_orders; ++i) {
Price price = 100000 + static_cast<Price>(price_offset_dist(rng)) * 10;
Side side = (i % 2 == 0) ? Side::Buy : Side::Sell;
book.addOrModifyOrder(i + 1, side, price, qty_dist(rng), 0, priority_dist(rng), 0);
}
auto observer = std::make_shared<CountingObserver>();
book.addObserver(observer);
for (auto _ : state) {
book.emitSnapshot(0);
}
state.SetItemsProcessed(state.iterations());
}
BENCHMARK(BM_L3_EmitSnapshot)->Arg(100)->Arg(500)->Arg(1000)->Arg(5000);
// ============================================================================
// Benchmark: Observer Add/Remove Overhead
// ============================================================================
static void BM_AddRemoveObserver(benchmark::State& state) {
OrderBookL2 book(1);
const auto num_observers = state.range(0);
std::vector<std::shared_ptr<CountingObserver>> observers;
for (auto i = 0; i < num_observers; ++i) {
observers.push_back(std::make_shared<CountingObserver>());
}
for (auto _ : state) {
// Add all observers
for (auto& obs : observers) {
book.addObserver(obs);
}
// Remove all observers
for (auto& obs : observers) {
book.removeObserver(obs);
}
}
state.SetItemsProcessed(state.iterations() * num_observers * 2);
}
BENCHMARK(BM_AddRemoveObserver)->Arg(1)->Arg(10)->Arg(50)->Arg(100);
// ============================================================================
// Main
// ============================================================================
BENCHMARK_MAIN();