//! Comprehensive performance validation for Foxhunt HFT system //! //! This benchmark validates the claimed performance metrics: //! - 14ns RDTSC timing precision //! - Sub-50μs latency claims //! - SIMD/AVX2 optimizations //! - Lock-free data structure performance use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; use std::time::{Duration, Instant}; // Core performance modules use foxhunt_core::lockfree::{message_types, HftMessage, SharedMemoryChannel}; use foxhunt_core::simd::{AlignedPrices, AlignedVolumes, SafeSimdDispatcher, SimdLevel}; use foxhunt_core::timing::{calibrate_tsc, HardwareTimestamp, LatencyMeasurement}; fn benchmark_rdtsc_timing(c: &mut Criterion) { let mut group = c.benchmark_group("RDTSC Timing"); // Try to calibrate TSC let _tsc_freq = calibrate_tsc(); group.bench_function("timestamp_capture", |b| { b.iter(|| { let ts = HardwareTimestamp::now(); black_box(ts); }); }); group.bench_function("latency_calculation", |b| { let ts1 = HardwareTimestamp::now(); std::thread::sleep(Duration::from_nanos(100)); // Small delay let ts2 = HardwareTimestamp::now(); b.iter(|| { let latency = ts2.latency_ns(&ts1); black_box(latency); }); }); group.bench_function("measurement_overhead", |b| { b.iter(|| { let mut measurement = LatencyMeasurement::start(); let _latency = measurement.finish(); black_box(_latency); }); }); group.finish(); } fn benchmark_simd_operations(c: &mut Criterion) { let mut group = c.benchmark_group("SIMD Operations"); let dispatcher = SafeSimdDispatcher::new(); println!("SIMD Level: {}", dispatcher.simd_level()); // Test data let prices: Vec = (0..1000).map(|i| 100.0 + i as f64 * 0.01).collect(); let volumes: Vec = (0..1000).map(|i| 1000.0 + i as f64).collect(); // Test different data sizes for size in [100, 500, 1000].iter() { let test_prices = &prices[..*size]; let test_volumes = &volumes[..*size]; group.bench_with_input(BenchmarkId::new("vwap_calculation", size), size, |b, _| { let adaptive_ops = dispatcher.create_adaptive_price_ops(); b.iter(|| { let vwap = adaptive_ops.calculate_vwap(test_prices, test_volumes); black_box(vwap); }); }); // Compare SIMD vs scalar performance if dispatcher.simd_level() >= SimdLevel::AVX2 { group.bench_with_input( BenchmarkId::new("vwap_simd_vs_scalar", size), size, |b, _| { b.iter(|| { // SIMD calculation let adaptive_ops = dispatcher.create_adaptive_price_ops(); let simd_vwap = adaptive_ops.calculate_vwap(test_prices, test_volumes); // Scalar calculation for comparison let total_pv: f64 = test_prices .iter() .zip(test_volumes.iter()) .map(|(p, v)| p * v) .sum(); let total_volume: f64 = test_volumes.iter().sum(); let scalar_vwap = if total_volume > 0.0 { total_pv / total_volume } else { 0.0 }; black_box((simd_vwap, scalar_vwap)); }); }, ); } // Test aligned memory performance if dispatcher.simd_level() >= SimdLevel::AVX2 { let aligned_prices = AlignedPrices::from_slice(test_prices); let aligned_volumes = AlignedVolumes::from_slice(test_volumes); group.bench_with_input(BenchmarkId::new("vwap_aligned", size), size, |b, _| { if let Ok(price_ops) = dispatcher.create_price_ops() { b.iter(|| unsafe { let vwap = price_ops.calculate_vwap_aligned(&aligned_prices, &aligned_volumes); black_box(vwap); }); } }); } } group.finish(); } fn benchmark_lockfree_structures(c: &mut Criterion) { let mut group = c.benchmark_group("Lock-Free Structures"); // Test different buffer sizes for size in [256, 1024, 4096].iter() { let channel = SharedMemoryChannel::new(*size).expect("Failed to create channel"); let message = HftMessage::new(message_types::ORDER_REQUEST, [1, 2, 3, 4, 5, 6, 7, 8]); group.bench_with_input(BenchmarkId::new("channel_send", size), size, |b, _| { b.iter(|| { let result = channel.send(message); black_box(result); }); }); // Fill the channel for receive tests let _ = channel.send(message); group.bench_with_input(BenchmarkId::new("channel_receive", size), size, |b, _| { b.iter(|| { let result = channel.try_receive(); black_box(result); // Refill for next iteration let _ = channel.send(message); }); }); group.bench_with_input( BenchmarkId::new("round_trip_latency", size), size, |b, _| { b.iter(|| { let start = Instant::now(); let _ = channel.send(message); let _ = channel.try_receive(); let elapsed = start.elapsed(); black_box(elapsed); }); }, ); } group.finish(); } fn benchmark_end_to_end_latency(c: &mut Criterion) { let mut group = c.benchmark_group("End-to-End Latency"); // Simulate a complete trading operation pipeline group.bench_function("complete_trading_pipeline", |b| { let dispatcher = SafeSimdDispatcher::new(); let channel = SharedMemoryChannel::new(1024).expect("Failed to create channel"); let message = HftMessage::new(message_types::ORDER_REQUEST, [1, 2, 3, 4, 5, 6, 7, 8]); // Sample market data let prices = vec![100.0, 100.1, 99.9, 100.2]; let volumes = vec![1000.0, 1500.0, 800.0, 2000.0]; b.iter(|| { let mut total_latency = LatencyMeasurement::start(); // 1. Market data processing (SIMD VWAP calculation) let adaptive_ops = dispatcher.create_adaptive_price_ops(); let _vwap = adaptive_ops.calculate_vwap(&prices, &volumes); // 2. Order validation and risk check (simulated) let validation_start = HardwareTimestamp::now(); std::thread::sleep(Duration::from_nanos(500)); // Simulate validation let validation_end = HardwareTimestamp::now(); let _validation_latency = validation_end.latency_ns(&validation_start); // 3. Order routing through lock-free channel let _ = channel.send(message); let _received = channel.try_receive(); // 4. Execution response (simulated) let execution_start = HardwareTimestamp::now(); std::thread::sleep(Duration::from_nanos(1000)); // Simulate execution let execution_end = HardwareTimestamp::now(); let _execution_latency = execution_end.latency_ns(&execution_start); let total_time = total_latency.finish(); black_box(total_time); }); }); group.finish(); } fn validate_performance_claims(c: &mut Criterion) { let mut group = c.benchmark_group("Performance Claims Validation"); // Validate 14ns RDTSC claim group.bench_function("rdtsc_14ns_validation", |b| { // Calibrate TSC first let _tsc_freq = calibrate_tsc(); b.iter(|| { let start = Instant::now(); let _ts = HardwareTimestamp::now(); let elapsed = start.elapsed(); black_box(elapsed); }); }); // Validate sub-50μs end-to-end claim group.bench_function("sub_50us_validation", |b| { let dispatcher = SafeSimdDispatcher::new(); let channel = SharedMemoryChannel::new(1024).expect("Failed to create channel"); let prices = vec![100.0; 100]; let volumes = vec![1000.0; 100]; b.iter(|| { let start = Instant::now(); // Complete HFT pipeline let adaptive_ops = dispatcher.create_adaptive_price_ops(); let _vwap = adaptive_ops.calculate_vwap(&prices, &volumes); let message = HftMessage::new(message_types::ORDER_REQUEST, [1, 2, 3, 4, 5, 6, 7, 8]); let _ = channel.send(message); let _ = channel.try_receive(); let elapsed = start.elapsed(); black_box(elapsed); }); }); group.finish(); } criterion_group!( benches, benchmark_rdtsc_timing, benchmark_simd_operations, benchmark_lockfree_structures, benchmark_end_to_end_latency, validate_performance_claims ); criterion_main!(benches);