-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbench_memory_usage.cpp
More file actions
398 lines (308 loc) · 12.9 KB
/
bench_memory_usage.cpp
File metadata and controls
398 lines (308 loc) · 12.9 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
394
395
396
397
398
/**
* @file bench_memory_usage.cpp
* @brief Memory usage profiling and benchmarks
*
* Measures memory footprint and allocation patterns:
* - L2 orderbook memory per symbol
* - L3 orderbook memory per symbol
* - Memory growth patterns
* - Object pool efficiency
* - Memory locality and cache performance
*
* Target: L2 < 1KB, L3 < 10KB per symbol (1000 orders)
*/
#include <slick/orderbook/orderbook.hpp>
#include <benchmark/benchmark.h>
#include <random>
#include <vector>
using namespace slick::orderbook;
// ============================================================================
// Memory Tracking Utilities
// ============================================================================
// Note: For accurate memory measurements, run with tools like:
// - Valgrind massif: valgrind --tool=massif ./bench_memory_usage
// - heaptrack: heaptrack ./bench_memory_usage
// - perf: perf mem record ./bench_memory_usage
// These benchmarks measure runtime and can give relative memory comparisons
// For absolute memory usage, use external profiling tools
// ============================================================================
// Benchmark: L2 Memory Footprint (Different Sizes)
// ============================================================================
static void BM_L2_MemoryFootprint(benchmark::State& state) {
const auto num_levels = state.range(0);
for (auto _ : state) {
state.PauseTiming();
OrderBookL2 book(1);
state.ResumeTiming();
// 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);
}
benchmark::DoNotOptimize(book);
state.PauseTiming();
// book destroyed here
state.ResumeTiming();
}
state.SetItemsProcessed(state.iterations());
state.SetLabel(std::to_string(num_levels) + " levels/side");
}
BENCHMARK(BM_L2_MemoryFootprint)
->Arg(10)
->Arg(50)
->Arg(100)
->Arg(500)
->Arg(1000);
// ============================================================================
// Benchmark: L3 Memory Footprint (Different Sizes)
// ============================================================================
static void BM_L3_MemoryFootprint(benchmark::State& state) {
const auto num_orders = state.range(0);
std::mt19937_64 rng(12345);
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 _ : state) {
state.PauseTiming();
OrderBookL3 book(1);
state.ResumeTiming();
// Build book with num_orders
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);
}
benchmark::DoNotOptimize(book);
state.PauseTiming();
// book destroyed here
state.ResumeTiming();
}
state.SetItemsProcessed(state.iterations());
state.SetLabel(std::to_string(num_orders) + " orders");
}
BENCHMARK(BM_L3_MemoryFootprint)
->Arg(100)
->Arg(500)
->Arg(1000)
->Arg(5000)
->Arg(10000);
// ============================================================================
// Benchmark: Memory Growth Pattern - L2
// ============================================================================
static void BM_L2_MemoryGrowth(benchmark::State& state) {
for (auto _ : state) {
OrderBookL2 book(1);
// Grow from 0 to 1000 levels incrementally
for (size_t i = 0; i < 1000; ++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);
}
benchmark::DoNotOptimize(book);
}
state.SetItemsProcessed(state.iterations());
}
BENCHMARK(BM_L2_MemoryGrowth);
// ============================================================================
// Benchmark: Memory Growth Pattern - L3
// ============================================================================
static void BM_L3_MemoryGrowth(benchmark::State& state) {
std::mt19937_64 rng(12345);
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 _ : state) {
OrderBookL3 book(1);
// Grow from 0 to 10000 orders incrementally
for (size_t i = 0; i < 10000; ++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);
}
benchmark::DoNotOptimize(book);
}
state.SetItemsProcessed(state.iterations());
}
BENCHMARK(BM_L3_MemoryGrowth);
// ============================================================================
// Benchmark: Memory Churn - L2 (Add/Delete Cycles)
// ============================================================================
static void BM_L2_MemoryChurn(benchmark::State& state) {
OrderBookL2 book(1);
const auto num_levels = state.range(0);
size_t cycle = 0;
for (auto _ : state) {
// Add levels
for (auto i = 0; i < num_levels; ++i) {
Price offset = static_cast<Price>(cycle * 1000 + i);
book.updateLevel(Side::Buy, 100000 - offset * 10, 1000, 0, 0);
book.updateLevel(Side::Sell, 100100 + offset * 10, 1000, 0, 0);
}
// Delete levels
for (auto i = 0; i < num_levels; ++i) {
Price offset = static_cast<Price>(cycle * 1000 + i);
book.deleteLevel(Side::Buy, 100000 - offset * 10);
book.deleteLevel(Side::Sell, 100100 + offset * 10);
}
++cycle;
}
state.SetItemsProcessed(state.iterations() * num_levels * 2);
}
BENCHMARK(BM_L2_MemoryChurn)->Arg(10)->Arg(50)->Arg(100);
// ============================================================================
// Benchmark: Memory Churn - L3 (Add/Delete Cycles)
// ============================================================================
static void BM_L3_MemoryChurn(benchmark::State& state) {
OrderBookL3 book(1);
const auto num_orders = state.range(0);
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;
std::vector<OrderId> order_ids;
order_ids.reserve(num_orders);
for (auto _ : state) {
order_ids.clear();
// Add orders
for (auto i = 0; i < num_orders; ++i) {
Price price = 100000 + (i % 10) * 10;
Side side = (i % 2 == 0) ? Side::Buy : Side::Sell;
book.addOrModifyOrder(order_id, side, price, qty_dist(rng), 0, priority_dist(rng), 0);
order_ids.push_back(order_id);
++order_id;
}
// Delete orders
for (OrderId id : order_ids) {
book.deleteOrder(id, 0);
}
}
state.SetItemsProcessed(state.iterations() * num_orders);
}
BENCHMARK(BM_L3_MemoryChurn)->Arg(100)->Arg(500)->Arg(1000);
// ============================================================================
// Benchmark: Multi-Symbol Memory Footprint
// ============================================================================
static void BM_Manager_MemoryFootprint_L2(benchmark::State& state) {
const auto num_symbols = state.range(0);
for (auto _ : state) {
state.PauseTiming();
OrderBookManager<OrderBookL2> manager;
state.ResumeTiming();
// Create orderbooks for num_symbols
for (SymbolId i = 1; i <= num_symbols; ++i) {
auto* book = manager.getOrCreateOrderBook(i);
// Populate with 10 levels each
for (int j = 0; j < 10; ++j) {
book->updateLevel(Side::Buy, 100000 - j * 10, 1000, 0, 0);
book->updateLevel(Side::Sell, 100100 + j * 10, 1000, 0, 0);
}
}
benchmark::DoNotOptimize(manager);
state.PauseTiming();
// manager destroyed here
state.ResumeTiming();
}
state.SetItemsProcessed(state.iterations());
state.SetLabel(std::to_string(num_symbols) + " symbols");
}
BENCHMARK(BM_Manager_MemoryFootprint_L2)
->Arg(10)
->Arg(100)
->Arg(1000)
->Arg(10000);
// ============================================================================
// Benchmark: Multi-Symbol Memory Footprint - L3
// ============================================================================
static void BM_Manager_MemoryFootprint_L3(benchmark::State& state) {
const auto num_symbols = state.range(0);
std::mt19937_64 rng(99999);
std::uniform_int_distribution<Quantity> qty_dist(100, 1000);
std::uniform_int_distribution<uint64_t> priority_dist(0, 1000000);
for (auto _ : state) {
state.PauseTiming();
OrderBookManager<OrderBookL3> manager;
OrderId order_id = 1;
state.ResumeTiming();
// Create orderbooks for num_symbols
for (SymbolId i = 1; i <= num_symbols; ++i) {
auto* book = manager.getOrCreateOrderBook(i);
// Populate with 100 orders each
for (int j = 0; j < 100; ++j) {
Price price = 100000 + (j % 10) * 10;
Side side = (j % 2 == 0) ? Side::Buy : Side::Sell;
book->addOrModifyOrder(order_id++, side, price, qty_dist(rng), 0, priority_dist(rng), 0);
}
}
benchmark::DoNotOptimize(manager);
state.PauseTiming();
// manager destroyed here
state.ResumeTiming();
}
state.SetItemsProcessed(state.iterations());
state.SetLabel(std::to_string(num_symbols) + " symbols");
}
BENCHMARK(BM_Manager_MemoryFootprint_L3)
->Arg(10)
->Arg(100)
->Arg(1000);
// ============================================================================
// Benchmark: Cache Locality - Sequential Access
// ============================================================================
static void BM_L2_CacheLocality_Sequential(benchmark::State& state) {
OrderBookL2 book(1);
// Build book with 100 levels
for (size_t i = 0; i < 100; ++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);
}
for (auto _ : state) {
// Sequential iteration through all levels
const auto& bids = book.getLevels(Side::Buy);
const auto& asks = book.getLevels(Side::Sell);
Quantity total_bid_qty = 0;
Quantity total_ask_qty = 0;
for (const auto& level : bids) {
total_bid_qty += level.quantity;
}
for (const auto& level : asks) {
total_ask_qty += level.quantity;
}
benchmark::DoNotOptimize(total_bid_qty);
benchmark::DoNotOptimize(total_ask_qty);
}
state.SetItemsProcessed(state.iterations());
}
BENCHMARK(BM_L2_CacheLocality_Sequential);
// ============================================================================
// Benchmark: Cache Locality - Random Access
// ============================================================================
static void BM_L2_CacheLocality_Random(benchmark::State& state) {
OrderBookL2 book(1);
std::vector<Price> bid_prices;
std::vector<Price> ask_prices;
// Build book with 100 levels
for (size_t i = 0; i < 100; ++i) {
Price bid_price = 100000 - static_cast<Price>(i) * 10;
Price ask_price = 100100 + static_cast<Price>(i) * 10;
book.updateLevel(Side::Buy, bid_price, 1000, 0, 0);
book.updateLevel(Side::Sell, ask_price, 1000, 0, 0);
bid_prices.push_back(bid_price);
ask_prices.push_back(ask_price);
}
std::mt19937_64 rng(77777);
std::uniform_int_distribution<Quantity> qty_dist(500, 1500);
std::uniform_int_distribution<size_t> price_dist(0, bid_prices.size() - 1);
for (auto _ : state) {
// Random access updates
for (int i = 0; i < 10; ++i) {
size_t idx = price_dist(rng);
book.updateLevel(Side::Buy, bid_prices[idx], qty_dist(rng), 0, 0);
book.updateLevel(Side::Sell, ask_prices[idx], qty_dist(rng), 0, 0);
}
}
state.SetItemsProcessed(state.iterations() * 20);
}
BENCHMARK(BM_L2_CacheLocality_Random);
// ============================================================================
// Main
// ============================================================================
BENCHMARK_MAIN();