|
| 1 | +/* |
| 2 | + * bench_latency.cpp - per-operation latency histogram for the matching engine. |
| 3 | + * |
| 4 | + * Measures the wall-clock cost of submit_order() across a long stream of |
| 5 | + * synthetic add/cancel/match operations and reports p50/p90/p95/p99/p999 |
| 6 | + * along with min/max. The intent is to give a quick "is anything regressing" |
| 7 | + * signal that can be wired into CI or run by hand before tagging a release. |
| 8 | + * |
| 9 | + * Usage: |
| 10 | + * ./bench_latency # 1M operations against a 10x5 seeded book |
| 11 | + * ./bench_latency --ops 5000000 # 5M operations |
| 12 | + * ./bench_latency --warmup 200000 # warmup count before timing starts |
| 13 | + */ |
| 14 | + |
| 15 | +#include "MatchingEngine.h" |
| 16 | +#include "OrderBook.h" |
| 17 | +#include "Order.h" |
| 18 | + |
| 19 | +#include <algorithm> |
| 20 | +#include <chrono> |
| 21 | +#include <cstdint> |
| 22 | +#include <cstring> |
| 23 | +#include <iomanip> |
| 24 | +#include <iostream> |
| 25 | +#include <random> |
| 26 | +#include <string> |
| 27 | +#include <vector> |
| 28 | + |
| 29 | +using namespace micro_exchange::core; |
| 30 | + |
| 31 | +namespace { |
| 32 | + |
| 33 | +struct CliArgs { |
| 34 | + size_t ops = 1'000'000; |
| 35 | + size_t warmup = 100'000; |
| 36 | +}; |
| 37 | + |
| 38 | +CliArgs parse(int argc, char** argv) { |
| 39 | + CliArgs a; |
| 40 | + for (int i = 1; i < argc; ++i) { |
| 41 | + std::string s = argv[i]; |
| 42 | + if (s == "--ops" && i + 1 < argc) a.ops = std::stoull(argv[++i]); |
| 43 | + else if (s == "--warmup" && i + 1 < argc) a.warmup = std::stoull(argv[++i]); |
| 44 | + else if (s == "--help") { |
| 45 | + std::cout << "usage: bench_latency [--ops N] [--warmup N]\n"; |
| 46 | + std::exit(0); |
| 47 | + } |
| 48 | + } |
| 49 | + return a; |
| 50 | +} |
| 51 | + |
| 52 | +void seed_book(MatchingEngine& engine, const char* sym, Price mid) { |
| 53 | + OrderId id = 1; |
| 54 | + for (int lvl = 1; lvl <= 10; ++lvl) { |
| 55 | + for (int j = 0; j < 5; ++j) { |
| 56 | + NewOrderRequest req{}; |
| 57 | + req.id = id++; |
| 58 | + req.side = Side::Buy; |
| 59 | + req.type = OrderType::Limit; |
| 60 | + req.tif = TimeInForce::GTC; |
| 61 | + req.price = mid - lvl; |
| 62 | + req.quantity = 100 + j * 50; |
| 63 | + std::strncpy(req.symbol, sym, 15); |
| 64 | + engine.submit_order(req); |
| 65 | + |
| 66 | + req.id = id++; |
| 67 | + req.side = Side::Sell; |
| 68 | + req.price = mid + lvl; |
| 69 | + engine.submit_order(req); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +double percentile(std::vector<uint64_t>& v, double p) { |
| 75 | + if (v.empty()) return 0; |
| 76 | + size_t idx = static_cast<size_t>(p * (v.size() - 1)); |
| 77 | + std::nth_element(v.begin(), v.begin() + idx, v.end()); |
| 78 | + return static_cast<double>(v[idx]); |
| 79 | +} |
| 80 | + |
| 81 | +} // namespace |
| 82 | + |
| 83 | +int main(int argc, char** argv) { |
| 84 | + CliArgs args = parse(argc, argv); |
| 85 | + |
| 86 | + std::cout << "\n MicroExchange — Latency Benchmark\n"; |
| 87 | + std::cout << " ─────────────────────────────────\n"; |
| 88 | + std::cout << " Operations: " << args.ops << "\n"; |
| 89 | + std::cout << " Warmup: " << args.warmup << "\n\n"; |
| 90 | + |
| 91 | + const char* sym = "BENCH"; |
| 92 | + MatchingEngine engine; |
| 93 | + engine.add_symbol(sym); |
| 94 | + seed_book(engine, sym, 10000); |
| 95 | + |
| 96 | + std::mt19937_64 rng(0xBEEFCAFE); |
| 97 | + std::uniform_int_distribution<int> side_dist(0, 1); |
| 98 | + std::uniform_int_distribution<int> type_dist(0, 9); |
| 99 | + std::uniform_int_distribution<Price> price_dist(9990, 10010); |
| 100 | + std::uniform_int_distribution<Quantity> qty_dist(1, 5); |
| 101 | + |
| 102 | + OrderId id = 100'000; |
| 103 | + auto submit_random = [&]() { |
| 104 | + NewOrderRequest req{}; |
| 105 | + req.id = id++; |
| 106 | + req.side = side_dist(rng) ? Side::Buy : Side::Sell; |
| 107 | + bool is_market = type_dist(rng) == 0; // 10% market |
| 108 | + req.type = is_market ? OrderType::Market : OrderType::Limit; |
| 109 | + req.tif = is_market ? TimeInForce::IOC : TimeInForce::GTC; |
| 110 | + req.price = is_market ? PRICE_MARKET : price_dist(rng); |
| 111 | + req.quantity = qty_dist(rng) * 100; |
| 112 | + std::strncpy(req.symbol, sym, 15); |
| 113 | + engine.submit_order(req); |
| 114 | + }; |
| 115 | + |
| 116 | + // Warmup |
| 117 | + for (size_t i = 0; i < args.warmup; ++i) submit_random(); |
| 118 | + |
| 119 | + std::vector<uint64_t> latencies; |
| 120 | + latencies.reserve(args.ops); |
| 121 | + |
| 122 | + auto t_start = std::chrono::steady_clock::now(); |
| 123 | + for (size_t i = 0; i < args.ops; ++i) { |
| 124 | + auto a = std::chrono::steady_clock::now(); |
| 125 | + submit_random(); |
| 126 | + auto b = std::chrono::steady_clock::now(); |
| 127 | + latencies.push_back( |
| 128 | + std::chrono::duration_cast<std::chrono::nanoseconds>(b - a).count()); |
| 129 | + } |
| 130 | + auto t_end = std::chrono::steady_clock::now(); |
| 131 | + |
| 132 | + double wall_sec = |
| 133 | + std::chrono::duration<double>(t_end - t_start).count(); |
| 134 | + |
| 135 | + double p50 = percentile(latencies, 0.50); |
| 136 | + double p90 = percentile(latencies, 0.90); |
| 137 | + double p95 = percentile(latencies, 0.95); |
| 138 | + double p99 = percentile(latencies, 0.99); |
| 139 | + double p999 = percentile(latencies, 0.999); |
| 140 | + auto mm = std::minmax_element(latencies.begin(), latencies.end()); |
| 141 | + |
| 142 | + std::cout << std::fixed << std::setprecision(0); |
| 143 | + std::cout << " Throughput: " << (args.ops / wall_sec) << " ops/sec\n\n"; |
| 144 | + |
| 145 | + std::cout << " Latency (nanoseconds)\n"; |
| 146 | + std::cout << " ─────────────────────\n"; |
| 147 | + std::cout << " min " << *mm.first << "\n"; |
| 148 | + std::cout << " p50 " << p50 << "\n"; |
| 149 | + std::cout << " p90 " << p90 << "\n"; |
| 150 | + std::cout << " p95 " << p95 << "\n"; |
| 151 | + std::cout << " p99 " << p99 << "\n"; |
| 152 | + std::cout << " p99.9 " << p999 << "\n"; |
| 153 | + std::cout << " max " << *mm.second << "\n\n"; |
| 154 | + |
| 155 | + return 0; |
| 156 | +} |
0 commit comments