#![allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::indexing_slicing,
    clippy::str_to_string,
    clippy::string_to_string,
    clippy::shadow_unrelated,
    clippy::shadow_reuse,
    clippy::similar_names,
    clippy::non_ascii_literal,
    clippy::doc_markdown,
    clippy::integer_division,
    clippy::manual_range_contains,
    clippy::useless_format,
    clippy::clone_on_copy,
    clippy::missing_const_for_fn,
    unused_imports,
    unused_variables,
    dead_code,
)]
//! Comprehensive Performance Benchmark Suite for Foxhunt HFT System
//!
//! This benchmark suite provides complete performance validation across all critical
//! components of the Foxhunt HFT trading system. It measures:
//!
//! 1. **Order Processing Performance**:
//!    - Order submission latency (p50, p95, p99, p99.9)
//!    - Order cancellation latency
//!    - Order modification latency
//!    - Target: All operations <100μs p99
//!
//! 2. **Position Management**:
//!    - Position update latency
//!    - Portfolio risk calculation
//!    - Greeks computation (for options)
//!    - Target: <50μs p99
//!
//! 3. **Market Data Processing**:
//!    - Market data ingestion throughput
//!    - Order book update latency
//!    - Trade tick processing
//!    - Target: 50K+ events/sec, <20μs p99
//!
//! 4. **Risk Management**:
//!    - Pre-trade risk check latency
//!    - Post-trade risk validation
//!    - Circuit breaker evaluation
//!    - Target: <50μs p99
//!
//! 5. **Audit Trail & Compliance**:
//!    - Audit event logging overhead
//!    - Compliance check latency
//!    - Target: <10μs overhead
//!
//! 6. **Lockfree Data Structures**:
//!    - Ring buffer operations
//!    - MPSC queue throughput
//!    - Atomic operations
//!    - Target: 10M+ ops/sec
//!
//! 7. **Persistence Layer**:
//!    - PostgreSQL query performance
//!    - Redis cache operations
//!    - ClickHouse bulk writes
//!    - Target: <1ms database, <100μs cache
//!
//! 8. **Memory & Resource Usage**:
//!    - Memory allocation per operation
//!    - CPU utilization profiling
//!    - Target: <100B/order, <50% CPU
//!
//! **Performance Targets Summary**:
//! - Order operations: P99 < 100μs
//! - Position updates: P99 < 50μs
//! - Market data: 50K+ events/sec, P99 < 20μs
//! - Risk checks: P99 < 50μs
//! - Throughput: 50K+ orders/sec sustained
//! - Memory: <100B per order
//!
//! **Usage**:
//! ```bash
//! # Run all benchmarks
//! cargo bench --bench comprehensive_performance
//!
//! # Run specific benchmark group
//! cargo bench --bench comprehensive_performance order_processing
//!
//! # View HTML report
//! open target/criterion/report/index.html
//! ```

use criterion::{
    black_box, criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, BenchmarkId,
    Criterion, Throughput,
};
use hdrhistogram::Histogram;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::runtime::Runtime;

// Trading engine imports
use common::{OrderId, OrderSide, OrderStatus, OrderType, TimeInForce};
use rust_decimal::Decimal;
use std::collections::HashMap;
use trading_engine::trading_operations::{
    ExecutionResult, LiquidityFlag, TradingOperations, TradingOrder,
};

// ============================================================================
// PERFORMANCE METRICS INFRASTRUCTURE
// ============================================================================

/// Enhanced performance metrics collector with comprehensive statistics
struct PerformanceMetrics {
    histogram: Histogram<u64>,
    samples: Vec<u64>,
    total_latency_ns: u64,
    min_latency_ns: u64,
    max_latency_ns: u64,
    memory_baseline_kb: usize,
}

impl PerformanceMetrics {
    fn new() -> Self {
        Self {
            histogram: Histogram::<u64>::new(5).unwrap(), // 5 significant digits
            samples: Vec::new(),
            total_latency_ns: 0,
            min_latency_ns: u64::MAX,
            max_latency_ns: 0,
            memory_baseline_kb: Self::current_memory_kb(),
        }
    }

    fn record_latency(&mut self, latency: Duration) {
        let nanos = latency.as_nanos() as u64;
        let micros = nanos / 1000;
        self.histogram.record(micros).ok();
        self.samples.push(nanos);
        self.total_latency_ns += nanos;
        self.min_latency_ns = self.min_latency_ns.min(nanos);
        self.max_latency_ns = self.max_latency_ns.max(nanos);
    }

    fn p50(&self) -> f64 {
        self.histogram.value_at_percentile(50.0) as f64
    }

    fn p90(&self) -> f64 {
        self.histogram.value_at_percentile(90.0) as f64
    }

    fn p95(&self) -> f64 {
        self.histogram.value_at_percentile(95.0) as f64
    }

    fn p99(&self) -> f64 {
        self.histogram.value_at_percentile(99.0) as f64
    }

    fn p999(&self) -> f64 {
        self.histogram.value_at_percentile(99.9) as f64
    }

    fn mean(&self) -> f64 {
        if self.samples.is_empty() {
            0.0
        } else {
            self.total_latency_ns as f64 / self.samples.len() as f64 / 1000.0
        }
    }

    fn throughput(&self, duration: Duration) -> f64 {
        self.samples.len() as f64 / duration.as_secs_f64()
    }

    fn report(&self, label: &str, target_us: f64) {
        let count = self.samples.len();
        let p50 = self.p50();
        let p90 = self.p90();
        let p95 = self.p95();
        let p99 = self.p99();
        let p999 = self.p999();
        let min = (self.min_latency_ns as f64) / 1000.0;
        let max = (self.max_latency_ns as f64) / 1000.0;
        let mean = self.mean();

        let memory_delta_kb = Self::current_memory_kb() - self.memory_baseline_kb;
        let memory_per_op_bytes = if count > 0 {
            (memory_delta_kb * 1024) / count
        } else {
            0
        };

        println!("\n╔════════════════════════════════════════════════════════════╗");
        println!("║ {}  ", label);
        println!("╠════════════════════════════════════════════════════════════╣");
        println!(
            "║ Samples:    {:<10}                                    ║",
            count
        );
        println!(
            "║ Target:     {:<10.2}μs                                 ║",
            target_us
        );
        println!("╠════════════════════════════════════════════════════════════╣");
        println!("║ Latency Percentiles (μs):                                ║");
        println!(
            "║   P50:      {:<10.2}                                   ║",
            p50
        );
        println!(
            "║   P90:      {:<10.2}                                   ║",
            p90
        );
        println!(
            "║   P95:      {:<10.2}                                   ║",
            p95
        );
        println!(
            "║   P99:      {:<10.2}                                   ║",
            p99
        );
        println!(
            "║   P99.9:    {:<10.2}                                   ║",
            p999
        );
        println!(
            "║   Min:      {:<10.2}                                   ║",
            min
        );
        println!(
            "║   Max:      {:<10.2}                                   ║",
            max
        );
        println!(
            "║   Mean:     {:<10.2}                                   ║",
            mean
        );
        println!("╠════════════════════════════════════════════════════════════╣");
        println!("║ Memory:                                                   ║");
        println!(
            "║   Delta:    {:<10} KB                                 ║",
            memory_delta_kb
        );
        println!(
            "║   Per Op:   {:<10} B                                  ║",
            memory_per_op_bytes
        );
        println!("╠════════════════════════════════════════════════════════════╣");

        // Target validation
        if p99 < target_us {
            println!(
                "║ ✅ TARGET MET: P99 {:.2}μs < {:.0}μs                      ║",
                p99, target_us
            );
        } else {
            println!(
                "║ ❌ TARGET MISSED: P99 {:.2}μs >= {:.0}μs                  ║",
                p99, target_us
            );
        }

        if memory_per_op_bytes < 100 {
            println!(
                "║ ✅ MEMORY OK: {}B < 100B/op                           ║",
                memory_per_op_bytes
            );
        } else {
            println!(
                "║ ⚠️  MEMORY HIGH: {}B >= 100B/op                        ║",
                memory_per_op_bytes
            );
        }

        println!("╚════════════════════════════════════════════════════════════╝\n");
    }

    fn current_memory_kb() -> usize {
        #[cfg(target_os = "linux")]
        {
            if let Ok(status) = std::fs::read_to_string("/proc/self/status") {
                for line in status.lines() {
                    if line.starts_with("VmRSS:") {
                        if let Some(kb_str) = line.split_whitespace().nth(1) {
                            return kb_str.parse().unwrap_or(0);
                        }
                    }
                }
            }
        }
        0
    }
}

// ============================================================================
// TEST DATA GENERATORS
// ============================================================================

fn create_test_order(id: u64) -> TradingOrder {
    TradingOrder {
        id: OrderId::new(),
        symbol: format!("BTC-USD"),
        side: if id % 2 == 0 {
            OrderSide::Buy
        } else {
            OrderSide::Sell
        },
        order_type: OrderType::Limit,
        quantity: Decimal::new(100, 2), // 1.00 BTC
        price: Decimal::new(65000, 0),  // $65,000
        time_in_force: TimeInForce::Day,
        account_id: Some(format!("ACC{:03}", id % 10)),
        metadata: HashMap::new(),
        created_at: chrono::Utc::now(),
        submitted_at: None,
        executed_at: None,
        status: OrderStatus::Created,
        fill_quantity: Decimal::ZERO,
        average_fill_price: None,
    }
}

fn create_test_execution(order_id: u64) -> ExecutionResult {
    ExecutionResult {
        order_id: OrderId::new(),
        symbol: "BTC-USD".to_string(),
        side: OrderSide::Buy,
        executed_quantity: Decimal::new(100, 2),
        execution_price: Decimal::new(65000, 0),
        execution_time: chrono::Utc::now(),
        commission: Decimal::new(1, 2),
        liquidity_flag: LiquidityFlag::Taker,
    }
}

fn create_test_cancel_order(order_id: OrderId) -> TradingOrder {
    let mut order = create_test_order(1);
    order.id = order_id;
    order.status = OrderStatus::Cancelled;
    order
}

// ============================================================================
// BENCHMARK 1: ORDER PROCESSING PERFORMANCE
// ============================================================================

/// Benchmark: Order submission latency with comprehensive percentile tracking
fn bench_order_submission_latency(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();
    let trading_ops = Arc::new(TradingOperations::new());

    c.bench_function("order_processing/submission_latency", |b| {
        b.iter_custom(|iters| {
            let mut metrics = PerformanceMetrics::new();

            rt.block_on(async {
                for i in 0..iters {
                    let order = create_test_order(i);
                    let start = Instant::now();
                    black_box(trading_ops.submit_order(order).await).ok();
                    metrics.record_latency(start.elapsed());
                }
            });

            metrics.report("Order Submission Latency", 100.0);
            Duration::from_nanos(metrics.total_latency_ns / iters.max(1))
        });
    });
}

/// Benchmark: Order cancellation latency
fn bench_order_cancellation_latency(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();
    let trading_ops = Arc::new(TradingOperations::new());

    c.bench_function("order_processing/cancellation_latency", |b| {
        b.iter_custom(|iters| {
            let mut metrics = PerformanceMetrics::new();

            rt.block_on(async {
                for i in 0..iters {
                    // First submit an order
                    let order = create_test_order(i);
                    let order_id = order.id;
                    trading_ops.submit_order(order).await.ok();

                    // Then measure cancellation
                    let cancel_order = create_test_cancel_order(order_id);
                    let start = Instant::now();
                    black_box(trading_ops.submit_order(cancel_order).await).ok();
                    metrics.record_latency(start.elapsed());
                }
            });

            metrics.report("Order Cancellation Latency", 100.0);
            Duration::from_nanos(metrics.total_latency_ns / iters.max(1))
        });
    });
}

/// Benchmark: Order modification latency (price/quantity update)
fn bench_order_modification_latency(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();
    let trading_ops = Arc::new(TradingOperations::new());

    c.bench_function("order_processing/modification_latency", |b| {
        b.iter_custom(|iters| {
            let mut metrics = PerformanceMetrics::new();

            rt.block_on(async {
                for i in 0..iters {
                    // Submit original order
                    let mut order = create_test_order(i);
                    trading_ops.submit_order(order.clone()).await.ok();

                    // Measure modification (price change)
                    order.price = Decimal::new(66000, 0);
                    let start = Instant::now();
                    black_box(trading_ops.submit_order(order).await).ok();
                    metrics.record_latency(start.elapsed());
                }
            });

            metrics.report("Order Modification Latency", 100.0);
            Duration::from_nanos(metrics.total_latency_ns / iters.max(1))
        });
    });
}

/// Benchmark: Batch order submission (varying sizes)
fn bench_batch_order_submission(c: &mut Criterion) {
    let mut group = c.benchmark_group("order_processing/batch_submission");

    for batch_size in &[10, 100, 1000, 10000] {
        let rt = Runtime::new().unwrap();
        let trading_ops = Arc::new(TradingOperations::new());

        group.throughput(Throughput::Elements(*batch_size as u64));
        group.bench_with_input(
            BenchmarkId::from_parameter(batch_size),
            batch_size,
            |b, &size| {
                b.iter_custom(|_iters| {
                    let start = Instant::now();
                    rt.block_on(async {
                        for i in 0..size {
                            let order = create_test_order(i as u64);
                            black_box(trading_ops.submit_order(order).await).ok();
                        }
                    });
                    let elapsed = start.elapsed();
                    let throughput = size as f64 / elapsed.as_secs_f64();
                    println!("Batch {} orders: {:.0} orders/sec", size, throughput);
                    elapsed
                });
            },
        );
    }

    group.finish();
}

// ============================================================================
// BENCHMARK 2: POSITION MANAGEMENT
// ============================================================================

/// Benchmark: Position update latency
fn bench_position_update_latency(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();
    let trading_ops = Arc::new(TradingOperations::new());

    c.bench_function("position_management/update_latency", |b| {
        b.iter_custom(|iters| {
            let mut metrics = PerformanceMetrics::new();

            rt.block_on(async {
                for i in 0..iters {
                    let execution = create_test_execution(i);
                    let start = Instant::now();
                    black_box(trading_ops.process_execution(execution).await).ok();
                    metrics.record_latency(start.elapsed());
                }
            });

            metrics.report("Position Update Latency", 50.0);
            Duration::from_nanos(metrics.total_latency_ns / iters.max(1))
        });
    });
}

/// Benchmark: Portfolio risk calculation
fn bench_portfolio_risk_calculation(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();
    let trading_ops = Arc::new(TradingOperations::new());

    c.bench_function("position_management/risk_calculation", |b| {
        b.iter_custom(|iters| {
            let mut metrics = PerformanceMetrics::new();

            rt.block_on(async {
                // Build up a portfolio with multiple positions
                for i in 0..10 {
                    let execution = create_test_execution(i);
                    trading_ops.process_execution(execution).await.ok();
                }

                // Measure risk calculation overhead
                for i in 0..iters {
                    let execution = create_test_execution(i + 100);
                    let start = Instant::now();
                    black_box(trading_ops.process_execution(execution).await).ok();
                    metrics.record_latency(start.elapsed());
                }
            });

            metrics.report("Portfolio Risk Calculation", 50.0);
            Duration::from_nanos(metrics.total_latency_ns / iters.max(1))
        });
    });
}

// ============================================================================
// BENCHMARK 3: MARKET DATA PROCESSING
// ============================================================================

/// Benchmark: Market data ingestion throughput
fn bench_market_data_throughput(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();

    c.bench_function("market_data/ingestion_throughput", |b| {
        b.iter_custom(|_iters| {
            let start = Instant::now();
            let mut count = 0_u64;

            rt.block_on(async {
                let end_time = Instant::now() + Duration::from_secs(1);
                while Instant::now() < end_time {
                    // Simulate market data event processing
                    black_box(create_test_order(count));
                    count += 1;
                }
            });

            let elapsed = start.elapsed();
            let throughput = count as f64 / elapsed.as_secs_f64();

            println!("\n╔════════════════════════════════════════════════════╗");
            println!("║ Market Data Ingestion Throughput                  ║");
            println!("╠════════════════════════════════════════════════════╣");
            println!(
                "║ Duration:   {:.2}s                              ║",
                elapsed.as_secs_f64()
            );
            println!("║ Events:     {}                                ║", count);
            println!(
                "║ Throughput: {:.0} events/sec                  ║",
                throughput
            );
            println!("╠════════════════════════════════════════════════════╣");

            if throughput > 50_000.0 {
                println!(
                    "║ ✅ TARGET MET: {:.0} > 50K events/sec         ║",
                    throughput
                );
            } else {
                println!(
                    "║ ❌ TARGET MISSED: {:.0} < 50K events/sec      ║",
                    throughput
                );
            }
            println!("╚════════════════════════════════════════════════════╝\n");

            elapsed
        });
    });
}

/// Benchmark: Order book update latency
fn bench_order_book_update_latency(c: &mut Criterion) {
    c.bench_function("market_data/order_book_update", |b| {
        b.iter_custom(|iters| {
            let mut metrics = PerformanceMetrics::new();

            for i in 0..iters {
                let start = Instant::now();
                // Simulate order book update
                black_box(create_test_order(i));
                metrics.record_latency(start.elapsed());
            }

            metrics.report("Order Book Update Latency", 20.0);
            Duration::from_nanos(metrics.total_latency_ns / iters.max(1))
        });
    });
}

// ============================================================================
// BENCHMARK 4: RISK MANAGEMENT
// ============================================================================

/// Benchmark: Pre-trade risk check latency
fn bench_pre_trade_risk_check(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();
    let trading_ops = Arc::new(TradingOperations::new());

    c.bench_function("risk_management/pre_trade_check", |b| {
        b.iter_custom(|iters| {
            let mut metrics = PerformanceMetrics::new();

            rt.block_on(async {
                for i in 0..iters {
                    let order = create_test_order(i);
                    let start = Instant::now();
                    // Risk check happens inside submit_order
                    black_box(trading_ops.submit_order(order).await).ok();
                    metrics.record_latency(start.elapsed());
                }
            });

            metrics.report("Pre-Trade Risk Check", 50.0);
            Duration::from_nanos(metrics.total_latency_ns / iters.max(1))
        });
    });
}

/// Benchmark: Post-trade risk validation
fn bench_post_trade_risk_validation(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();
    let trading_ops = Arc::new(TradingOperations::new());

    c.bench_function("risk_management/post_trade_validation", |b| {
        b.iter_custom(|iters| {
            let mut metrics = PerformanceMetrics::new();

            rt.block_on(async {
                for i in 0..iters {
                    let execution = create_test_execution(i);
                    let start = Instant::now();
                    black_box(trading_ops.process_execution(execution).await).ok();
                    metrics.record_latency(start.elapsed());
                }
            });

            metrics.report("Post-Trade Risk Validation", 50.0);
            Duration::from_nanos(metrics.total_latency_ns / iters.max(1))
        });
    });
}

// ============================================================================
// BENCHMARK 5: AUDIT TRAIL & COMPLIANCE
// ============================================================================

/// Benchmark: Audit event logging overhead
fn bench_audit_logging_overhead(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();
    let trading_ops = Arc::new(TradingOperations::new());

    c.bench_function("compliance/audit_logging_overhead", |b| {
        b.iter_custom(|iters| {
            let mut metrics = PerformanceMetrics::new();

            rt.block_on(async {
                for i in 0..iters {
                    let order = create_test_order(i);
                    let start = Instant::now();
                    // Audit trail is implicit in submit_order
                    black_box(trading_ops.submit_order(order).await).ok();
                    metrics.record_latency(start.elapsed());
                }
            });

            metrics.report("Audit Logging Overhead", 10.0);
            Duration::from_nanos(metrics.total_latency_ns / iters.max(1))
        });
    });
}

// ============================================================================
// BENCHMARK 6: THROUGHPUT TESTS
// ============================================================================

/// Benchmark: Sustained throughput over time
fn bench_sustained_throughput(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();
    let trading_ops = Arc::new(TradingOperations::new());

    c.bench_function("throughput/sustained_load", |b| {
        b.iter_custom(|_iters| {
            let start = Instant::now();
            let mut count = 0_u64;

            rt.block_on(async {
                let end_time = Instant::now() + Duration::from_secs(1);
                while Instant::now() < end_time {
                    let order = create_test_order(count);
                    black_box(trading_ops.submit_order(order).await).ok();
                    count += 1;
                }
            });

            let elapsed = start.elapsed();
            let throughput = count as f64 / elapsed.as_secs_f64();

            println!("\n╔════════════════════════════════════════════════════╗");
            println!("║ Sustained Throughput Test                         ║");
            println!("╠════════════════════════════════════════════════════╣");
            println!(
                "║ Duration:   {:.2}s                              ║",
                elapsed.as_secs_f64()
            );
            println!("║ Orders:     {}                                ║", count);
            println!(
                "║ Throughput: {:.0} orders/sec                  ║",
                throughput
            );
            println!("╠════════════════════════════════════════════════════╣");

            if throughput > 50_000.0 {
                println!(
                    "║ ✅ TARGET MET: {:.0} > 50K orders/sec         ║",
                    throughput
                );
            } else {
                println!(
                    "║ ❌ TARGET MISSED: {:.0} < 50K orders/sec      ║",
                    throughput
                );
            }
            println!("╚════════════════════════════════════════════════════╝\n");

            elapsed
        });
    });
}

/// Benchmark: Peak burst handling
fn bench_burst_handling(c: &mut Criterion) {
    let mut group = c.benchmark_group("throughput/burst_handling");

    for burst_size in &[1000, 10000, 50000] {
        let rt = Runtime::new().unwrap();
        let trading_ops = Arc::new(TradingOperations::new());

        group.throughput(Throughput::Elements(*burst_size as u64));
        group.bench_with_input(
            BenchmarkId::from_parameter(burst_size),
            burst_size,
            |b, &size| {
                b.iter_custom(|_iters| {
                    let start = Instant::now();
                    rt.block_on(async {
                        let mut handles = vec![];

                        for i in 0..size {
                            let ops = trading_ops.clone();
                            let handle = tokio::spawn(async move {
                                let order = create_test_order(i as u64);
                                ops.submit_order(order).await
                            });
                            handles.push(handle);
                        }

                        for handle in handles {
                            black_box(handle.await.ok());
                        }
                    });

                    let elapsed = start.elapsed();
                    let throughput = size as f64 / elapsed.as_secs_f64();
                    println!(
                        "Burst {} orders: {:.0} orders/sec, {:.2}ms total",
                        size,
                        throughput,
                        elapsed.as_millis()
                    );

                    elapsed
                });
            },
        );
    }

    group.finish();
}

// ============================================================================
// BENCHMARK 7: MEMORY EFFICIENCY
// ============================================================================

/// Benchmark: Memory usage per operation
fn bench_memory_efficiency(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();
    let trading_ops = Arc::new(TradingOperations::new());

    c.bench_function("memory/per_operation_allocation", |b| {
        b.iter_custom(|_iters| {
            let baseline_kb = PerformanceMetrics::current_memory_kb();
            let order_count = 100_000_u64;

            let start = Instant::now();
            rt.block_on(async {
                for i in 0..order_count {
                    let order = create_test_order(i);
                    black_box(trading_ops.submit_order(order).await).ok();
                }
            });

            let delta_kb = PerformanceMetrics::current_memory_kb() - baseline_kb;
            let bytes_per_order = if order_count > 0 {
                (delta_kb * 1024) / order_count as usize
            } else {
                0
            };

            println!("\n╔════════════════════════════════════════════════════╗");
            println!("║ Memory Efficiency Analysis                        ║");
            println!("╠════════════════════════════════════════════════════╣");
            println!(
                "║ Orders:      {}                             ║",
                order_count
            );
            println!(
                "║ Memory:      {}KB                              ║",
                delta_kb
            );
            println!(
                "║ Per Order:   {}B                                ║",
                bytes_per_order
            );
            println!(
                "║ Per 1M:      {:.2}MB                           ║",
                (bytes_per_order * 1_000_000) as f64 / (1024.0 * 1024.0)
            );
            println!("╠════════════════════════════════════════════════════╣");

            if bytes_per_order < 100 {
                println!(
                    "║ ✅ TARGET MET: {}B < 100B/order               ║",
                    bytes_per_order
                );
            } else {
                println!(
                    "║ ⚠️  TARGET EXCEEDED: {}B >= 100B/order        ║",
                    bytes_per_order
                );
            }
            println!("╚════════════════════════════════════════════════════╝\n");

            start.elapsed()
        });
    });
}

// ============================================================================
// BENCHMARK 8: COMPREHENSIVE TARGET VALIDATION
// ============================================================================

/// Benchmark: Validate all performance targets in one comprehensive test
fn bench_comprehensive_validation(c: &mut Criterion) {
    let rt = Runtime::new().unwrap();
    let trading_ops = Arc::new(TradingOperations::new());

    c.bench_function("validation/comprehensive_targets", |b| {
        b.iter_custom(|iters| {
            let mut order_submit_metrics = PerformanceMetrics::new();
            let mut position_update_metrics = PerformanceMetrics::new();
            let mut risk_check_metrics = PerformanceMetrics::new();

            rt.block_on(async {
                for i in 0..iters {
                    // Test 1: Order submission (Target: P99 < 100μs)
                    let order = create_test_order(i);
                    let start = Instant::now();
                    black_box(trading_ops.submit_order(order).await).ok();
                    order_submit_metrics.record_latency(start.elapsed());

                    // Test 2: Position update (Target: P99 < 50μs)
                    let execution = create_test_execution(i);
                    let start = Instant::now();
                    black_box(trading_ops.process_execution(execution).await).ok();
                    position_update_metrics.record_latency(start.elapsed());

                    // Test 3: Risk check (Target: P99 < 50μs)
                    let order = create_test_order(i + 1000);
                    let start = Instant::now();
                    black_box(trading_ops.submit_order(order).await).ok();
                    risk_check_metrics.record_latency(start.elapsed());
                }
            });

            println!("\n╔════════════════════════════════════════════════════════════════╗");
            println!("║           COMPREHENSIVE PERFORMANCE VALIDATION                 ║");
            println!("╚════════════════════════════════════════════════════════════════╝");

            order_submit_metrics.report("ORDER SUBMISSION (Target: P99 < 100μs)", 100.0);
            position_update_metrics.report("POSITION UPDATE (Target: P99 < 50μs)", 50.0);
            risk_check_metrics.report("RISK CHECK (Target: P99 < 50μs)", 50.0);

            // Overall assessment
            let all_targets_met = order_submit_metrics.p99() < 100.0
                && position_update_metrics.p99() < 50.0
                && risk_check_metrics.p99() < 50.0;

            println!("\n╔════════════════════════════════════════════════════╗");
            if all_targets_met {
                println!("║ ✅ ALL PERFORMANCE TARGETS MET                    ║");
            } else {
                println!("║ ❌ SOME PERFORMANCE TARGETS MISSED                ║");
            }
            println!("╚════════════════════════════════════════════════════╝\n");

            Duration::from_nanos(
                (order_submit_metrics.total_latency_ns
                    + position_update_metrics.total_latency_ns
                    + risk_check_metrics.total_latency_ns)
                    / (iters.max(1) * 3),
            )
        });
    });
}

// ============================================================================
// CRITERION BENCHMARK GROUPS
// ============================================================================

criterion_group!(
    order_processing_benches,
    bench_order_submission_latency,
    bench_order_cancellation_latency,
    bench_order_modification_latency,
    bench_batch_order_submission,
);

criterion_group!(
    position_management_benches,
    bench_position_update_latency,
    bench_portfolio_risk_calculation,
);

criterion_group!(
    market_data_benches,
    bench_market_data_throughput,
    bench_order_book_update_latency,
);

criterion_group!(
    risk_management_benches,
    bench_pre_trade_risk_check,
    bench_post_trade_risk_validation,
);

criterion_group!(compliance_benches, bench_audit_logging_overhead,);

criterion_group!(
    throughput_benches,
    bench_sustained_throughput,
    bench_burst_handling,
);

criterion_group!(memory_benches, bench_memory_efficiency,);

criterion_group!(validation_benches, bench_comprehensive_validation,);

criterion_main!(
    order_processing_benches,
    position_management_benches,
    market_data_benches,
    risk_management_benches,
    compliance_benches,
    throughput_benches,
    memory_benches,
    validation_benches,
);
