quantum-pulse 0.1.13

A lightweight, customizable profiling library for Rust with support for custom categories and percentile statistics
Documentation
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
//! Example showing how a high-frequency trading system integrates with Quantum Pulse
//! This demonstrates microsecond-precision profiling for latency-sensitive applications using Operation trait

use quantum_pulse::{profile, profile_async, Category, Operation, ProfileCollector};
use std::thread;
use std::time::Duration;
use tokio::time::sleep;

/// Trading system operations that implement Operation trait
#[derive(Debug)]
enum TradingOperation {
    // Order Processing
    OrderValidation,
    OrderSubmission,
    OrderExecution,
    OrderConfirmation,
    OrderCancellation,

    // Market Data Processing
    PriceFeedParsing,
    MarketDataUpdate,
    VolatilityCalculation,
    TechnicalAnalysis,

    // Risk Management
    RiskAssessment,
    PositionUpdate,
    ExposureCalculation,
    LimitCheck,

    // Portfolio Management
    PnlCalculation,
    RebalanceOperation,
    PortfolioOptimization,

    // External Communications
    ExchangeConnection,
    MarketDataFeed,
    RegulatoryReporting,

    // High-Frequency Operations
    ArbitrageDetection,
    LatencyMeasurement,
    OrderBookUpdate,
}

/// Order processing category for trade lifecycle operations
#[derive(Debug)]
struct OrderProcessingCategory;

impl Category for OrderProcessingCategory {
    fn get_name(&self) -> &str {
        "OrderProcessing"
    }

    fn get_description(&self) -> &str {
        "Order lifecycle operations including validation, submission, and execution"
    }

    fn color_hint(&self) -> Option<&str> {
        Some("#FF6B6B")
    }

    fn priority(&self) -> i32 {
        1 // Highest priority - critical path
    }
}

/// Market data category for price feed and market information
#[derive(Debug)]
struct MarketDataCategory;

impl Category for MarketDataCategory {
    fn get_name(&self) -> &str {
        "MarketData"
    }

    fn get_description(&self) -> &str {
        "Market data processing, price feeds, and market analysis"
    }

    fn color_hint(&self) -> Option<&str> {
        Some("#4ECDC4")
    }

    fn priority(&self) -> i32 {
        2
    }
}

/// Risk management category for risk assessment and control
#[derive(Debug)]
struct RiskManagementCategory;

impl Category for RiskManagementCategory {
    fn get_name(&self) -> &str {
        "RiskManagement"
    }

    fn get_description(&self) -> &str {
        "Risk assessment, position monitoring, and exposure calculations"
    }

    fn color_hint(&self) -> Option<&str> {
        Some("#E74C3C")
    }

    fn priority(&self) -> i32 {
        1 // Also highest priority - risk critical
    }
}

/// Portfolio management category for portfolio operations
#[derive(Debug)]
struct PortfolioCategory;

impl Category for PortfolioCategory {
    fn get_name(&self) -> &str {
        "Portfolio"
    }

    fn get_description(&self) -> &str {
        "Portfolio management, PnL calculations, and optimization"
    }

    fn color_hint(&self) -> Option<&str> {
        Some("#96CEB4")
    }

    fn priority(&self) -> i32 {
        3
    }
}

/// External communication category for exchange and data connections
#[derive(Debug)]
struct ExternalCommCategory;

impl Category for ExternalCommCategory {
    fn get_name(&self) -> &str {
        "ExternalComm"
    }

    fn get_description(&self) -> &str {
        "External communications with exchanges, data providers, and regulators"
    }

    fn color_hint(&self) -> Option<&str> {
        Some("#F39C12")
    }

    fn priority(&self) -> i32 {
        4
    }
}

/// High-frequency operations category for latency-critical operations
#[derive(Debug)]
struct HighFrequencyCategory;

impl Category for HighFrequencyCategory {
    fn get_name(&self) -> &str {
        "HighFrequency"
    }

    fn get_description(&self) -> &str {
        "Ultra-low latency operations for high-frequency trading"
    }

    fn color_hint(&self) -> Option<&str> {
        Some("#9B59B6")
    }

    fn priority(&self) -> i32 {
        1 // Highest priority - latency critical
    }
}

impl Operation for TradingOperation {
    fn get_category(&self) -> &dyn Category {
        match self {
            TradingOperation::OrderValidation
            | TradingOperation::OrderSubmission
            | TradingOperation::OrderExecution
            | TradingOperation::OrderConfirmation
            | TradingOperation::OrderCancellation => &OrderProcessingCategory,

            TradingOperation::PriceFeedParsing
            | TradingOperation::MarketDataUpdate
            | TradingOperation::VolatilityCalculation
            | TradingOperation::TechnicalAnalysis => &MarketDataCategory,

            TradingOperation::RiskAssessment
            | TradingOperation::PositionUpdate
            | TradingOperation::ExposureCalculation
            | TradingOperation::LimitCheck => &RiskManagementCategory,

            TradingOperation::PnlCalculation
            | TradingOperation::RebalanceOperation
            | TradingOperation::PortfolioOptimization => &PortfolioCategory,

            TradingOperation::ExchangeConnection
            | TradingOperation::MarketDataFeed
            | TradingOperation::RegulatoryReporting => &ExternalCommCategory,

            TradingOperation::ArbitrageDetection
            | TradingOperation::LatencyMeasurement
            | TradingOperation::OrderBookUpdate => &HighFrequencyCategory,
        }
    }

    fn to_str(&self) -> String {
        match self {
            TradingOperation::OrderValidation => "order_validation".to_string(),
            TradingOperation::OrderSubmission => "order_submission".to_string(),
            TradingOperation::OrderExecution => "order_execution".to_string(),
            TradingOperation::OrderConfirmation => "order_confirmation".to_string(),
            TradingOperation::OrderCancellation => "order_cancellation".to_string(),
            TradingOperation::PriceFeedParsing => "price_feed_parsing".to_string(),
            TradingOperation::MarketDataUpdate => "market_data_update".to_string(),
            TradingOperation::VolatilityCalculation => "volatility_calculation".to_string(),
            TradingOperation::TechnicalAnalysis => "technical_analysis".to_string(),
            TradingOperation::RiskAssessment => "risk_assessment".to_string(),
            TradingOperation::PositionUpdate => "position_update".to_string(),
            TradingOperation::ExposureCalculation => "exposure_calculation".to_string(),
            TradingOperation::LimitCheck => "limit_check".to_string(),
            TradingOperation::PnlCalculation => "pnl_calculation".to_string(),
            TradingOperation::RebalanceOperation => "rebalance_operation".to_string(),
            TradingOperation::PortfolioOptimization => "portfolio_optimization".to_string(),
            TradingOperation::ExchangeConnection => "exchange_connection".to_string(),
            TradingOperation::MarketDataFeed => "market_data_feed".to_string(),
            TradingOperation::RegulatoryReporting => "regulatory_reporting".to_string(),
            TradingOperation::ArbitrageDetection => "arbitrage_detection".to_string(),
            TradingOperation::LatencyMeasurement => "latency_measurement".to_string(),
            TradingOperation::OrderBookUpdate => "order_book_update".to_string(),
        }
    }
}

/// Simulated order structure
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct Order {
    id: u64,
    symbol: String,
    quantity: i64,
    price: f64,
    order_type: OrderType,
}

#[derive(Debug, Clone)]
#[allow(dead_code)]
enum OrderType {
    Market,
    Limit,
    Stop,
}

/// Simulated market data
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct MarketData {
    symbol: String,
    bid: f64,
    ask: f64,
    volume: u64,
    timestamp: std::time::SystemTime,
}

#[tokio::main]
async fn main() {
    println!("=== High-Frequency Trading System - Quantum Pulse Integration ===\n");

    // Clear any existing profiling data
    ProfileCollector::clear_all();

    // Simulate various trading system operations
    println!("🚀 Starting trading system simulation...\n");

    // Example 1: Order processing workflow
    println!("1. Order Processing Workflow:");
    process_trading_orders().await;

    // Example 2: Market data processing
    println!("\n2. Market Data Processing:");
    process_market_data().await;

    // Example 3: Risk management operations
    println!("\n3. Risk Management Operations:");
    perform_risk_checks().await;

    // Example 4: High-frequency operations (microsecond precision)
    println!("\n4. High-Frequency Operations:");
    execute_hft_operations();

    // Example 5: Portfolio management
    println!("\n5. Portfolio Management:");
    manage_portfolio().await;

    // Example 6: Concurrent trading operations
    println!("\n6. Concurrent Trading Operations:");
    run_concurrent_trading_ops().await;

    // Example 7: Latency-critical path simulation
    println!("\n7. Critical Path Latency Test:");
    test_critical_path_latency();

    // Generate comprehensive trading system report
    generate_trading_system_report();
}

async fn process_trading_orders() {
    let orders = vec![
        Order {
            id: 1001,
            symbol: "AAPL".to_string(),
            quantity: 100,
            price: 150.25,
            order_type: OrderType::Limit,
        },
        Order {
            id: 1002,
            symbol: "MSFT".to_string(),
            quantity: -200,
            price: 0.0, // Market order
            order_type: OrderType::Market,
        },
        Order {
            id: 1003,
            symbol: "GOOGL".to_string(),
            quantity: 50,
            price: 2800.0,
            order_type: OrderType::Stop,
        },
    ];

    for order in orders {
        println!("   Processing order {} for {}...", order.id, order.symbol);

        // Step 1: Validate order
        let validation_op = TradingOperation::OrderValidation;
        let is_valid = profile!(validation_op, {
            // Simulate validation logic
            thread::sleep(Duration::from_micros(50));
            validate_order(&order)
        });

        if !is_valid {
            println!("   ❌ Order {} failed validation", order.id);
            continue;
        }

        // Step 2: Risk assessment
        let risk_op = TradingOperation::RiskAssessment;
        let risk_approved = profile!(risk_op, {
            thread::sleep(Duration::from_micros(30));
            assess_order_risk(&order)
        });

        if !risk_approved {
            println!("   ⚠️  Order {} rejected by risk management", order.id);
            continue;
        }

        // Step 3: Submit order
        let submission_op = TradingOperation::OrderSubmission;
        profile_async!(submission_op, async {
            sleep(Duration::from_micros(100)).await;
            println!("   📤 Order {} submitted to exchange", order.id);
        })
        .await;

        // Step 4: Simulate execution
        let execution_op = TradingOperation::OrderExecution;
        profile_async!(execution_op, async {
            sleep(Duration::from_micros(200)).await;
            println!("   ✅ Order {} executed", order.id);
        })
        .await;

        // Step 5: Confirm execution
        let confirmation_op = TradingOperation::OrderConfirmation;
        profile!(confirmation_op, {
            thread::sleep(Duration::from_micros(25));
            println!("   📋 Order {} confirmed", order.id);
        });

        // Step 6: Update position
        let position_op = TradingOperation::PositionUpdate;
        profile!(position_op, {
            thread::sleep(Duration::from_micros(40));
            println!("   📊 Position updated for {}", order.symbol);
        });
    }
}

async fn process_market_data() {
    let market_data = vec![
        MarketData {
            symbol: "AAPL".to_string(),
            bid: 150.20,
            ask: 150.25,
            volume: 1000000,
            timestamp: std::time::SystemTime::now(),
        },
        MarketData {
            symbol: "MSFT".to_string(),
            bid: 310.15,
            ask: 310.20,
            volume: 800000,
            timestamp: std::time::SystemTime::now(),
        },
        MarketData {
            symbol: "GOOGL".to_string(),
            bid: 2799.50,
            ask: 2800.00,
            volume: 500000,
            timestamp: std::time::SystemTime::now(),
        },
    ];

    for data in market_data {
        println!("   Processing market data for {}...", data.symbol);

        // Parse price feed
        let parsing_op = TradingOperation::PriceFeedParsing;
        profile!(parsing_op, {
            // Ultra-fast parsing - microsecond precision
            thread::sleep(Duration::from_micros(5));
        });

        // Update market data
        let update_op = TradingOperation::MarketDataUpdate;
        profile!(update_op, {
            thread::sleep(Duration::from_micros(10));
        });

        // Calculate volatility
        let volatility_op = TradingOperation::VolatilityCalculation;
        profile_async!(volatility_op, async {
            sleep(Duration::from_micros(80)).await;
            let volatility = calculate_volatility(&data);
            println!("   📈 Volatility for {}: {:.2}%", data.symbol, volatility);
        })
        .await;

        // Technical analysis
        let analysis_op = TradingOperation::TechnicalAnalysis;
        profile!(analysis_op, {
            thread::sleep(Duration::from_micros(120));
            println!("   🔍 Technical analysis completed for {}", data.symbol);
        });
    }
}

async fn perform_risk_checks() {
    println!("   Performing comprehensive risk assessment...");

    // Risk assessment
    let risk_op = TradingOperation::RiskAssessment;
    profile!(risk_op, {
        thread::sleep(Duration::from_micros(150));
        println!("   🛡️  Risk assessment completed");
    });

    // Calculate exposure
    let exposure_op = TradingOperation::ExposureCalculation;
    profile_async!(exposure_op, async {
        sleep(Duration::from_micros(200)).await;
        println!("   📊 Market exposure calculated");
    })
    .await;

    // Perform limit checks
    for limit_type in &["position_limit", "var_limit", "concentration_limit"] {
        let limit_op = TradingOperation::LimitCheck;
        profile!(limit_op, {
            thread::sleep(Duration::from_micros(30));
            println!("{} check passed", limit_type);
        });
    }
}

fn execute_hft_operations() {
    println!("   Executing high-frequency trading operations...");

    // Ultra-low latency operations (sub-microsecond targeting)
    for i in 0..10 {
        // Arbitrage detection
        let arb_op = TradingOperation::ArbitrageDetection;
        profile!(arb_op, {
            // Simulate ultra-fast arbitrage detection
            thread::sleep(Duration::from_nanos(500)); // Sub-microsecond
        });

        // Order book update
        let book_op = TradingOperation::OrderBookUpdate;
        profile!(book_op, {
            thread::sleep(Duration::from_nanos(300)); // Sub-microsecond
        });

        // Latency measurement
        let latency_op = TradingOperation::LatencyMeasurement;
        profile!(latency_op, {
            thread::sleep(Duration::from_nanos(100)); // Sub-microsecond
        });

        if i % 3 == 0 {
            println!("   ⚡ HFT batch {} completed", i / 3 + 1);
        }
    }
}

async fn manage_portfolio() {
    println!("   Managing portfolio...");

    // PnL calculation
    let pnl_op = TradingOperation::PnlCalculation;
    profile_async!(pnl_op, async {
        sleep(Duration::from_micros(300)).await;
        println!("   💰 PnL calculation completed: $125,430.50");
    })
    .await;

    // Portfolio rebalancing
    let rebalance_op = TradingOperation::RebalanceOperation;
    profile_async!(rebalance_op, async {
        sleep(Duration::from_millis(2)).await;
        println!("   ⚖️  Portfolio rebalanced");
    })
    .await;

    // Portfolio optimization
    let optimization_op = TradingOperation::PortfolioOptimization;
    profile_async!(optimization_op, async {
        sleep(Duration::from_millis(5)).await;
        println!("   🎯 Portfolio optimization completed");
    })
    .await;
}

async fn run_concurrent_trading_ops() {
    println!("   Running concurrent trading operations...");

    // Using the OrderCancellation variant to avoid unused warning
    if false {
        profile!(TradingOperation::OrderCancellation, {
            println!("Cancelling order");
        });
    }

    let feed_op = TradingOperation::MarketDataFeed;
    let exchange_op = TradingOperation::ExchangeConnection;
    let reporting_op = TradingOperation::RegulatoryReporting;

    let (feed_result, exchange_result, reporting_result) = tokio::join!(
        profile_async!(feed_op, async {
            sleep(Duration::from_micros(500)).await;
            "Market data feed active"
        }),
        profile_async!(exchange_op, async {
            sleep(Duration::from_micros(800)).await;
            "Exchange connection established"
        }),
        profile_async!(reporting_op, async {
            sleep(Duration::from_millis(10)).await;
            "Regulatory reports submitted"
        })
    );

    println!("   📡 {}", feed_result);
    println!("   🔗 {}", exchange_result);
    println!("   📋 {}", reporting_result);
}

fn test_critical_path_latency() {
    println!("   Testing critical path latency (Order -> Execution)...");

    let iterations = 1000;

    for i in 0..iterations {
        // Simulate the critical path: Validation -> Risk -> Submission -> Execution
        let validation_op = TradingOperation::OrderValidation;
        profile!(validation_op, {
            // Target: < 10 microseconds
            thread::sleep(Duration::from_nanos(200));
        });

        let risk_op = TradingOperation::RiskAssessment;
        profile!(risk_op, {
            // Target: < 5 microseconds
            thread::sleep(Duration::from_nanos(100));
        });

        let submission_op = TradingOperation::OrderSubmission;
        profile!(submission_op, {
            // Target: < 50 microseconds
            thread::sleep(Duration::from_nanos(800));
        });

        let execution_op = TradingOperation::OrderExecution;
        profile!(execution_op, {
            // Target: < 100 microseconds
            thread::sleep(Duration::from_micros(2));
        });

        if i % 100 == 0 && i > 0 {
            println!("   ⚡ Completed {} critical path iterations", i);
        }
    }

    println!(
        "   🎯 Critical path latency test completed ({} iterations)",
        iterations
    );
}

fn generate_trading_system_report() {
    println!("\n{}", "=".repeat(80));
    println!("TRADING SYSTEM PERFORMANCE REPORT");
    println!("{}", "=".repeat(80));

    ProfileCollector::report_stats();

    let summary = ProfileCollector::get_summary();
    println!("\nSYSTEM SUMMARY:");
    println!("- Total trading operations: {}", summary.total_operations);
    println!("- Unique operation types: {}", summary.unique_operations);
    println!("- Total processing time: {}μs", summary.total_time_micros);
    println!(
        "- Average operation time: {:.2}μs",
        summary.total_time_micros as f64 / summary.total_operations as f64
    );

    println!("\n{}", "=".repeat(80));
    println!("PERFORMANCE ANALYSIS BY TRADING CATEGORY");
    println!("{}", "=".repeat(80));

    let all_stats = ProfileCollector::get_all_stats();

    // Group by category
    let mut categories: std::collections::HashMap<
        String,
        Vec<(String, quantum_pulse::OperationStats)>,
    > = std::collections::HashMap::new();

    for (key, stats) in all_stats {
        if let Some((category, operation)) = key.split_once("::") {
            categories
                .entry(category.to_string())
                .or_insert_with(Vec::new)
                .push((operation.to_string(), stats));
        }
    }

    // Display by category with trading-specific insights
    let category_order = vec![
        "HighFrequency",
        "OrderProcessing",
        "RiskManagement",
        "MarketData",
        "Portfolio",
        "ExternalComm",
    ];

    for category_name in category_order {
        if let Some(ops) = categories.get(category_name) {
            println!("\n📊 {} Operations:", category_name);

            let mut total_calls = 0;
            let mut total_time = Duration::ZERO;
            let mut min_time = Duration::MAX;
            let mut max_time = Duration::ZERO;

            for (op_name, stats) in ops {
                let avg_time = stats.mean();
                println!(
                    "   {} - {} calls, avg: {:?}",
                    op_name, stats.count, avg_time
                );

                total_calls += stats.count;
                total_time += stats.total;
                min_time = min_time.min(avg_time);
                max_time = max_time.max(avg_time);
            }

            let avg_category_time = if total_calls > 0 {
                total_time / total_calls as u32
            } else {
                Duration::ZERO
            };

            println!(
                "   📈 Category Summary: {} calls, {:?} total, {:?} avg",
                total_calls, total_time, avg_category_time
            );

            // Category-specific performance insights
            match category_name {
                "HighFrequency" => {
                    if avg_category_time > Duration::from_micros(1) {
                        println!("   ⚠️  WARNING: HFT operations averaging > 1μs");
                    } else {
                        println!("   ✅ HFT latency within target");
                    }
                }
                "OrderProcessing" => {
                    if avg_category_time > Duration::from_micros(100) {
                        println!("   ⚠️  WARNING: Order processing > 100μs average");
                    } else {
                        println!("   ✅ Order processing within SLA");
                    }
                }
                "RiskManagement" => {
                    if avg_category_time > Duration::from_micros(50) {
                        println!("   ⚠️  WARNING: Risk checks > 50μs average");
                    } else {
                        println!("   ✅ Risk management performance good");
                    }
                }
                _ => {}
            }
        }
    }

    println!("\n{}", "=".repeat(80));
    println!("LATENCY ANALYSIS - TOP PERFORMERS");
    println!("{}", "=".repeat(80));

    let all_stats = ProfileCollector::get_all_stats();
    let mut sorted_by_avg: Vec<_> = all_stats.iter().collect();
    sorted_by_avg.sort_by(|a, b| a.1.mean().cmp(&b.1.mean()));

    println!("\n🏆 FASTEST Operations (Top 10):");
    for (i, (name, stats)) in sorted_by_avg.iter().take(10).enumerate() {
        println!(
            "  {}. {} - avg: {:?} ({} calls)",
            i + 1,
            name,
            stats.mean(),
            stats.count
        );
    }

    println!("\n🐌 SLOWEST Operations (Top 5):");
    for (i, (name, stats)) in sorted_by_avg.iter().rev().take(5).enumerate() {
        println!(
            "  {}. {} - avg: {:?} ({} calls)",
            i + 1,
            name,
            stats.mean(),
            stats.count
        );
    }

    // Trading-specific performance recommendations
    println!("\n{}", "=".repeat(80));
    println!("PERFORMANCE RECOMMENDATIONS");
    println!("{}", "=".repeat(80));

    let high_freq_ops: Vec<_> = all_stats
        .iter()
        .filter(|(key, _)| key.starts_with("HighFrequency::"))
        .collect();

    if !high_freq_ops.is_empty() {
        let avg_hf_time: Duration = high_freq_ops
            .iter()
            .map(|(_, stats)| stats.mean())
            .sum::<Duration>()
            / high_freq_ops.len() as u32;

        println!("\n🚀 High-Frequency Trading Performance:");
        println!("   Average HFT operation time: {:?}", avg_hf_time);

        if avg_hf_time > Duration::from_micros(1) {
            println!("   📋 RECOMMENDATION: Optimize HFT operations to < 1μs");
            println!("      - Consider CPU affinity optimization");
            println!("      - Review memory allocation patterns");
            println!("      - Implement lock-free data structures");
        } else {
            println!("   ✅ HFT performance excellent!");
        }
    }

    println!("\n💡 General Recommendations:");
    println!("   - Monitor order processing latency closely");
    println!("   - Consider async processing for non-critical path operations");
    println!("   - Implement circuit breakers for risk management");
    println!("   - Use dedicated hardware for HFT operations");
}

// Utility functions
fn validate_order(order: &Order) -> bool {
    // Simulate order validation logic
    !order.symbol.is_empty() && order.quantity != 0
}

fn assess_order_risk(order: &Order) -> bool {
    // Simulate risk assessment
    order.quantity.abs() <= 10000 // Simple position size limit
}

fn calculate_volatility(market_data: &MarketData) -> f64 {
    // Simulate volatility calculation
    let spread = market_data.ask - market_data.bid;
    spread / market_data.bid * 100.0
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_trading_operation_categories() {
        let order_op = TradingOperation::OrderValidation;
        assert_eq!(order_op.get_category().get_name(), "OrderProcessing");
        assert_eq!(order_op.to_str(), "order_validation");

        let risk_op = TradingOperation::RiskAssessment;
        assert_eq!(risk_op.get_category().get_name(), "RiskManagement");
        assert_eq!(risk_op.to_str(), "risk_assessment");

        let hft_op = TradingOperation::ArbitrageDetection;
        assert_eq!(hft_op.get_category().get_name(), "HighFrequency");
        assert_eq!(hft_op.to_str(), "arbitrage_detection");
    }

    #[test]
    fn test_category_priorities() {
        let order_cat = OrderProcessingCategory;
        assert_eq!(order_cat.priority(), 1);

        let risk_cat = RiskManagementCategory;
        assert_eq!(risk_cat.priority(), 1);

        let hft_cat = HighFrequencyCategory;
        assert_eq!(hft_cat.priority(), 1);

        let portfolio_cat = PortfolioCategory;
        assert_eq!(portfolio_cat.priority(), 3);
    }

    #[test]
    fn test_order_validation() {
        let valid_order = Order {
            id: 1,
            symbol: "AAPL".to_string(),
            quantity: 100,
            price: 150.0,
            order_type: OrderType::Limit,
        };

        let invalid_order = Order {
            id: 2,
            symbol: "".to_string(),
            quantity: 0,
            price: 0.0,
            order_type: OrderType::Market,
        };

        assert!(validate_order(&valid_order));
        assert!(!validate_order(&invalid_order));
    }

    #[test]
    fn test_risk_assessment() {
        let safe_order = Order {
            id: 1,
            symbol: "AAPL".to_string(),
            quantity: 1000,
            price: 150.0,
            order_type: OrderType::Limit,
        };

        let risky_order = Order {
            id: 2,
            symbol: "AAPL".to_string(),
            quantity: 50000, // Exceeds limit
            price: 150.0,
            order_type: OrderType::Limit,
        };

        assert!(assess_order_risk(&safe_order));
        assert!(!assess_order_risk(&risky_order));
    }

    #[test]
    fn test_volatility_calculation() {
        let market_data = MarketData {
            symbol: "AAPL".to_string(),
            bid: 150.0,
            ask: 150.5,
            volume: 1000,
            timestamp: std::time::SystemTime::now(),
        };

        let volatility = calculate_volatility(&market_data);
        assert!((volatility - 0.3333).abs() < 0.01); // Should be ~0.33%
    }

    #[tokio::test]
    async fn test_trading_operation_profiling() {
        ProfileCollector::clear_all();

        let op = TradingOperation::OrderValidation;
        profile!(op, {
            thread::sleep(Duration::from_micros(1));
        });

        assert!(ProfileCollector::has_data());
        let stats = ProfileCollector::get_stats("OrderProcessing::order_validation");
        assert!(stats.is_some());
        assert_eq!(stats.unwrap().count, 1);
    }
}