Tick-to-Trade 延迟测量
Tick To Trade Measurer
题目详情
在分布式交易组件中测量延迟是性能调优和维持高频交易竞争优势的关键。通过从行情处理器到交易网关的整条链路埋点,开发者可以定位瓶颈并优化执行速度。
任务:基于日志事件流,计算完整订单链路的平均延迟。实现 measureLatency 方法,接收 5 个等长 std::vector<double> 数组,分别表示行情到达、策略决策、订单提交、交易所确认和网关发出各阶段的时间戳,计算每个完整链路的总延迟和各阶段的 P50/P99 统计值。
英文原题
Profiling latency across distributed trading components is critical for performance tuning and maintaining a competitive edge in high-frequency trading. By instrumenting the pipeline from the feed handler to the gateway, developers can identify bottlenecks and optimize execution speed.
Task
Calculate the average latencies for fully completed order pipelines based on a stream of logged events. Implement the measureLatency method, which takes 5 std::vector<double> arrays of equal length represent
解析
问题分析
Tick-to-Trade 延迟是衡量交易系统性能的关键指标——从行情到达网卡到订单发出网卡的时间。测量需要硬件时间戳或高精度时钟(如 clock_gettime(CLOCK_MONOTONIC))。
解法
class LatencyMeasurer {
std::vector<uint64_t> samples_ns_;
public:
struct Timestamp { uint64_t tick_arrival_ns, order_send_ns; };
void record(const Timestamp& ts) { samples_ns_.push_back(ts.order_send_ns - ts.tick_arrival_ns); }
struct Stats { uint64_t p50, p99, max, min; double avg; };
Stats report() const {
auto sorted = samples_ns_; std::sort(sorted.begin(), sorted.end());
return {sorted[sorted.size()/2], sorted[sorted.size()*99/100], sorted.back(), sorted.front(),
std::accumulate(sorted.begin(),sorted.end(),0.0)/sorted.size()};
}
};复杂度与边界
- 时间复杂度:record O(1),report O(N log N)
- 边界条件:(1) 时钟精度受硬件限制(通常 ~20ns) (2) PTP 时钟同步提供更精确时间戳 (3) 中位数比平均值更能反映典型延迟
英文解析
Analysis
Tick-to-Trade latency is a key metric for trading system performance — the time from market data arriving at the NIC to the order departing the NIC. Measurement requires hardware timestamps or high-precision clocks like `clock_gettime(CLOCK_MONOTONIC)`.
Solution
class LatencyMeasurer {
std::vector<uint64_t> samples_ns_;
public:
struct Timestamp { uint64_t tick_arrival_ns, order_send_ns; };
void record(const Timestamp& ts) { samples_ns_.push_back(ts.order_send_ns - ts.tick_arrival_ns); }
struct Stats { uint64_t p50, p99, max, min; double avg; };
Stats report() const {
auto sorted = samples_ns_; std::sort(sorted.begin(), sorted.end());
return {sorted[sorted.size()/2], sorted[sorted.size()*99/100], sorted.back(), sorted.front(),
std::accumulate(sorted.begin(),sorted.end(),0.0)/sorted.size()};
}
};Complexity & Edge Cases
- Time complexity: record O(1), report O(N log N)
- Edge cases: (1) Clock precision limited by hardware (typically ~20ns) (2) PTP clock sync provides more accurate timestamps (3) Median better represents typical latency than average
Key Considerations
- Clock synchronization: Tick-to-trade measurement requires synchronized clocks across data feed and execution paths; NTP drift >1ms invalidates measurements
- Pipeline stages: Latency decomposes into: feed processing, signal generation, order construction, network transmission, exchange acknowledgment — measure each independently
- Outlier filtering: Infrequent GC pauses or OS scheduling spikes produce outlier measurements; use percentile (P99) not mean for SLA reporting
- Warm-up effect: First measurements after restart include JIT compilation and cache warming latency; exclude initial measurements from benchmarks