-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_hft_engine.cpp
More file actions
95 lines (83 loc) · 2.54 KB
/
benchmark_hft_engine.cpp
File metadata and controls
95 lines (83 loc) · 2.54 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
#include "quanux/MarketTick.hpp"
#include "quanux/SPSCQueue.hpp"
#include <atomic>
#include <chrono>
#include <immintrin.h>
#include <iostream>
#include <thread>
#include <vector>
using namespace quanux;
// Signal simplified for benchmark
struct Signal {
uint64_t tick_ts;
uint32_t instrument_id;
double z_score;
double volatility;
};
int main() {
SPSCQueue<Signal> queue(4096);
std::atomic<bool> running{true};
std::vector<uint64_t> latencies;
latencies.reserve(1000000);
// Consumer Thread (Execution)
std::thread consumer([&]() {
Signal sig;
while (running) {
if (queue.pop(sig)) {
auto now = std::chrono::steady_clock::now();
auto tick_time = std::chrono::steady_clock::time_point(
std::chrono::nanoseconds(sig.tick_ts));
auto latency = std::chrono::duration_cast<std::chrono::nanoseconds>(
now - tick_time)
.count();
latencies.push_back(latency);
} else {
_mm_pause();
}
}
});
// Producer Loop (Simulation)
std::cout << "Running Micro-Benchmark (1M messages)..." << std::endl;
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < 1000000; ++i) {
auto now = std::chrono::steady_clock::now();
uint64_t ts = std::chrono::duration_cast<std::chrono::nanoseconds>(
now.time_since_epoch())
.count();
Signal sig{ts, 1, 3.0, 0.5}; // Always trigger
while (!queue.push(sig)) {
_mm_pause(); // Spin with pause
}
}
// Wait for drain
std::this_thread::sleep_for(std::chrono::milliseconds(200));
running = false;
consumer.join();
auto end = std::chrono::steady_clock::now();
std::cout << "Done." << std::endl;
// Calculate Stats
uint64_t sum = 0;
uint64_t min_lat = -1;
uint64_t max_lat = 0;
for (auto lat : latencies) {
sum += lat;
if (lat < min_lat)
min_lat = lat;
if (lat > max_lat)
max_lat = lat;
}
if (!latencies.empty()) {
std::cout << "Avg Latency: " << (sum / latencies.size()) << " ns"
<< std::endl;
std::cout << "Min Latency: " << min_lat << " ns" << std::endl;
std::cout << "Max Latency: " << max_lat << " ns" << std::endl;
std::cout << "Throughput: "
<< (1000000.0 /
std::chrono::duration_cast<std::chrono::milliseconds>(end -
start)
.count() *
1000)
<< " msg/sec" << std::endl;
}
return 0;
}