voirs-evaluation 0.1.0-rc.1

Quality evaluation and assessment framework for VoiRS
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
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
//! Advanced Performance Monitoring for VoiRS Evaluation
//!
//! This module provides comprehensive performance monitoring and optimization
//! suggestions for evaluation workflows, helping identify bottlenecks and
//! optimize system performance.

use crate::EvaluationError;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

/// Performance monitoring configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceMonitorConfig {
    /// Maximum number of measurements to keep in history
    pub max_history_size: usize,
    /// Sampling interval for continuous monitoring
    pub sampling_interval_ms: u64,
    /// Threshold for slow operation warnings (milliseconds)
    pub slow_operation_threshold_ms: u64,
    /// Enable memory usage monitoring
    pub monitor_memory: bool,
    /// Enable CPU usage monitoring
    pub monitor_cpu: bool,
    /// Enable detailed per-metric timing
    pub detailed_metric_timing: bool,
}

impl Default for PerformanceMonitorConfig {
    fn default() -> Self {
        Self {
            max_history_size: 1000,
            sampling_interval_ms: 100,
            slow_operation_threshold_ms: 1000,
            monitor_memory: true,
            monitor_cpu: true,
            detailed_metric_timing: true,
        }
    }
}

/// Performance measurement for a single operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceMeasurement {
    /// Operation name/identifier
    pub operation: String,
    /// Start time
    pub start_time: chrono::DateTime<chrono::Utc>,
    /// Duration in milliseconds
    pub duration_ms: u64,
    /// Memory usage before operation (bytes)
    pub memory_before_bytes: Option<u64>,
    /// Memory usage after operation (bytes)
    pub memory_after_bytes: Option<u64>,
    /// CPU usage during operation (percentage)
    pub cpu_usage_percent: Option<f32>,
    /// Audio buffer size processed
    pub audio_buffer_size: Option<usize>,
    /// Sample rate of processed audio
    pub sample_rate: Option<u32>,
    /// Additional metadata
    pub metadata: HashMap<String, String>,
}

/// Aggregated performance statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceStats {
    /// Operation name
    pub operation: String,
    /// Number of measurements
    pub measurement_count: usize,
    /// Total duration across all measurements
    pub total_duration_ms: u64,
    /// Average duration
    pub avg_duration_ms: f64,
    /// Minimum duration
    pub min_duration_ms: u64,
    /// Maximum duration
    pub max_duration_ms: u64,
    /// Standard deviation of duration
    pub std_dev_duration_ms: f64,
    /// 95th percentile duration
    pub p95_duration_ms: u64,
    /// 99th percentile duration
    pub p99_duration_ms: u64,
    /// Average memory usage
    pub avg_memory_usage_mb: Option<f64>,
    /// Average CPU usage
    pub avg_cpu_usage_percent: Option<f32>,
    /// Operations per second
    pub ops_per_second: f64,
}

/// Performance optimization recommendation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationRecommendation {
    /// Recommendation category
    pub category: String,
    /// Severity level (1-10, 10 being most critical)
    pub severity: u8,
    /// Description of the issue
    pub description: String,
    /// Specific recommendation
    pub recommendation: String,
    /// Expected improvement
    pub expected_improvement: String,
    /// Implementation complexity (Low, Medium, High)
    pub complexity: String,
}

/// Performance alert
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceAlert {
    /// Alert type
    pub alert_type: PerformanceAlertType,
    /// Timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,
    /// Operation that triggered the alert
    pub operation: String,
    /// Alert message
    pub message: String,
    /// Current value that triggered the alert
    pub current_value: f64,
    /// Threshold that was exceeded
    pub threshold: f64,
}

/// Types of performance alerts
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum PerformanceAlertType {
    /// Operation taking too long
    SlowOperation,
    /// High memory usage
    HighMemoryUsage,
    /// High CPU usage
    HighCpuUsage,
    /// Performance degradation
    PerformanceDegradation,
    /// Memory leak detected
    MemoryLeak,
}

/// Performance monitoring system
pub struct PerformanceMonitor {
    config: PerformanceMonitorConfig,
    measurements: Arc<Mutex<VecDeque<PerformanceMeasurement>>>,
    stats_cache: Arc<RwLock<HashMap<String, PerformanceStats>>>,
    alerts: Arc<Mutex<VecDeque<PerformanceAlert>>>,
    baseline_stats: Arc<RwLock<HashMap<String, PerformanceStats>>>,
}

impl PerformanceMonitor {
    /// Create a new performance monitor
    pub fn new(config: PerformanceMonitorConfig) -> Self {
        Self {
            config,
            measurements: Arc::new(Mutex::new(VecDeque::new())),
            stats_cache: Arc::new(RwLock::new(HashMap::new())),
            alerts: Arc::new(Mutex::new(VecDeque::new())),
            baseline_stats: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Start monitoring an operation
    pub fn start_operation(&self, operation: &str) -> OperationTimer {
        OperationTimer::new(operation.to_string(), self.config.clone())
    }

    /// Record a completed measurement
    pub async fn record_measurement(&self, measurement: PerformanceMeasurement) {
        // Add to measurements history
        {
            let mut measurements = self
                .measurements
                .lock()
                .expect("lock should not be poisoned");
            measurements.push_back(measurement.clone());

            // Maintain history size limit
            while measurements.len() > self.config.max_history_size {
                measurements.pop_front();
            }
        }

        // Update stats cache
        self.update_stats_cache(&measurement).await;

        // Check for alerts
        self.check_performance_alerts(&measurement).await;
    }

    /// Get performance statistics for an operation
    pub async fn get_stats(&self, operation: &str) -> Option<PerformanceStats> {
        let stats_cache = self.stats_cache.read().await;
        stats_cache.get(operation).cloned()
    }

    /// Get all performance statistics
    pub async fn get_all_stats(&self) -> HashMap<String, PerformanceStats> {
        let stats_cache = self.stats_cache.read().await;
        stats_cache.clone()
    }

    /// Get recent alerts
    pub fn get_recent_alerts(&self, limit: usize) -> Vec<PerformanceAlert> {
        let alerts = self.alerts.lock().expect("lock should not be poisoned");
        alerts.iter().rev().take(limit).cloned().collect()
    }

    /// Generate optimization recommendations
    pub async fn generate_recommendations(&self) -> Vec<OptimizationRecommendation> {
        let mut recommendations = Vec::new();
        let stats = self.get_all_stats().await;

        for (operation, stat) in &stats {
            // Check for slow operations
            if stat.avg_duration_ms > self.config.slow_operation_threshold_ms as f64 {
                recommendations.push(OptimizationRecommendation {
                    category: String::from("Performance"),
                    severity: 7,
                    description: format!(
                        "Operation '{}' has high average duration ({:.2}ms)",
                        operation, stat.avg_duration_ms
                    ),
                    recommendation: String::from(
                        "Consider optimizing the algorithm or using parallel processing",
                    ),
                    expected_improvement: String::from("30-50% reduction in processing time"),
                    complexity: String::from("Medium"),
                });
            }

            // Check for high variation in timing
            if stat.std_dev_duration_ms > stat.avg_duration_ms * 0.5 {
                recommendations.push(OptimizationRecommendation {
                    category: String::from("Consistency"),
                    severity: 5,
                    description: format!(
                        "Operation '{}' has inconsistent timing (std dev: {:.2}ms)",
                        operation, stat.std_dev_duration_ms
                    ),
                    recommendation: "Investigate variable load factors and consider caching"
                        .to_string(),
                    expected_improvement: String::from("More predictable performance"),
                    complexity: String::from("Low"),
                });
            }

            // Check for low throughput
            if stat.ops_per_second < 1.0 {
                recommendations.push(OptimizationRecommendation {
                    category: String::from("Throughput"),
                    severity: 6,
                    description: format!(
                        "Operation '{}' has low throughput ({:.2} ops/sec)",
                        operation, stat.ops_per_second
                    ),
                    recommendation: "Consider batch processing or algorithm optimization"
                        .to_string(),
                    expected_improvement: String::from("2-10x increase in throughput"),
                    complexity: String::from("High"),
                });
            }

            // Check for high memory usage
            if let Some(avg_memory) = stat.avg_memory_usage_mb {
                if avg_memory > 100.0 {
                    recommendations.push(OptimizationRecommendation {
                        category: String::from("Memory"),
                        severity: 4,
                        description: format!(
                            "Operation '{}' uses high memory ({:.2} MB)",
                            operation, avg_memory
                        ),
                        recommendation: "Consider streaming processing or memory pooling"
                            .to_string(),
                        expected_improvement: String::from("50-70% reduction in memory usage"),
                        complexity: String::from("Medium"),
                    });
                }
            }
        }

        recommendations
    }

    /// Create a performance report
    pub async fn create_report(&self) -> String {
        let mut report = String::new();

        report.push_str("# VoiRS Evaluation Performance Report\n\n");

        let stats = self.get_all_stats().await;
        let recommendations = self.generate_recommendations().await;
        let recent_alerts = self.get_recent_alerts(10);

        // Performance Overview
        report.push_str("## Performance Overview\n\n");
        if stats.is_empty() {
            report.push_str("No performance data available.\n\n");
        } else {
            report
                .push_str("| Operation | Avg Duration | Ops/Sec | P95 Duration | Memory Usage |\n");
            report.push_str(
                "|-----------|--------------|---------|--------------|---------------|\n",
            );

            for (operation, stat) in &stats {
                let memory_str = stat
                    .avg_memory_usage_mb
                    .map(|m| format!("{:.1} MB", m))
                    .unwrap_or_else(|| String::from("N/A"));

                report.push_str(&format!(
                    "| {} | {:.2}ms | {:.2} | {}ms | {} |\n",
                    operation,
                    stat.avg_duration_ms,
                    stat.ops_per_second,
                    stat.p95_duration_ms,
                    memory_str
                ));
            }
            report.push_str("\n");
        }

        // Recent Alerts
        if !recent_alerts.is_empty() {
            report.push_str("## Recent Alerts\n\n");
            for alert in &recent_alerts {
                let icon = match alert.alert_type {
                    PerformanceAlertType::SlowOperation => "🐌",
                    PerformanceAlertType::HighMemoryUsage => "🧠",
                    PerformanceAlertType::HighCpuUsage => "",
                    PerformanceAlertType::PerformanceDegradation => "📉",
                    PerformanceAlertType::MemoryLeak => "🔴",
                };

                report.push_str(&format!(
                    "- {} **{}**: {} ({})\n",
                    icon,
                    alert.operation,
                    alert.message,
                    alert.timestamp.format("%Y-%m-%d %H:%M:%S")
                ));
            }
            report.push_str("\n");
        }

        // Optimization Recommendations
        if !recommendations.is_empty() {
            report.push_str("## Optimization Recommendations\n\n");

            // Sort by severity (highest first)
            let mut sorted_recommendations = recommendations;
            sorted_recommendations.sort_by_key(|b| std::cmp::Reverse(b.severity));

            for (i, rec) in sorted_recommendations.iter().enumerate() {
                let priority = match rec.severity {
                    8..=10 => "🔴 High",
                    5..=7 => "🟡 Medium",
                    1..=4 => "🟢 Low",
                    _ => "⚪ Unknown",
                };

                report.push_str(&format!(
                    "### {}. {} - {}\n\n",
                    i + 1,
                    rec.category,
                    priority
                ));
                report.push_str(&format!("**Issue:** {}\n\n", rec.description));
                report.push_str(&format!("**Recommendation:** {}\n\n", rec.recommendation));
                report.push_str(&format!(
                    "**Expected Improvement:** {}\n\n",
                    rec.expected_improvement
                ));
                report.push_str(&format!(
                    "**Implementation Complexity:** {}\n\n",
                    rec.complexity
                ));
            }
        }

        report
    }

    /// Set baseline performance statistics
    pub async fn set_baseline(&self, operation: &str, stats: PerformanceStats) {
        let mut baseline_stats = self.baseline_stats.write().await;
        baseline_stats.insert(operation.to_string(), stats);
    }

    /// Clear all performance data
    pub async fn clear_data(&self) {
        {
            let mut measurements = self
                .measurements
                .lock()
                .expect("lock should not be poisoned");
            measurements.clear();
        }

        {
            let mut stats_cache = self.stats_cache.write().await;
            stats_cache.clear();
        }

        {
            let mut alerts = self.alerts.lock().expect("lock should not be poisoned");
            alerts.clear();
        }
    }

    /// Update statistics cache
    async fn update_stats_cache(&self, measurement: &PerformanceMeasurement) {
        let mut stats_cache = self.stats_cache.write().await;

        // Get recent measurements for this operation
        let measurements = self
            .measurements
            .lock()
            .expect("lock should not be poisoned");
        let operation_measurements: Vec<_> = measurements
            .iter()
            .filter(|m| m.operation == measurement.operation)
            .collect();

        if operation_measurements.is_empty() {
            return;
        }

        // Calculate statistics
        let durations: Vec<u64> = operation_measurements
            .iter()
            .map(|m| m.duration_ms)
            .collect();
        let total_duration: u64 = durations.iter().sum();
        let count = durations.len();
        let avg_duration = total_duration as f64 / count as f64;

        let min_duration = *durations.iter().min().expect("value should be present");
        let max_duration = *durations.iter().max().expect("value should be present");

        // Calculate standard deviation
        let variance = durations
            .iter()
            .map(|&d| (d as f64 - avg_duration).powi(2))
            .sum::<f64>()
            / count as f64;
        let std_dev = variance.sqrt();

        // Calculate percentiles
        let mut sorted_durations = durations.clone();
        sorted_durations.sort_unstable();
        let p95_idx = ((count as f64 * 0.95) as usize).min(count - 1);
        let p99_idx = ((count as f64 * 0.99) as usize).min(count - 1);
        let p95_duration = sorted_durations[p95_idx];
        let p99_duration = sorted_durations[p99_idx];

        // Calculate memory usage
        let avg_memory_usage_mb = if operation_measurements
            .iter()
            .any(|m| m.memory_after_bytes.is_some())
        {
            let memory_values: Vec<u64> = operation_measurements
                .iter()
                .filter_map(|m| m.memory_after_bytes)
                .collect();

            if !memory_values.is_empty() {
                Some(
                    memory_values.iter().sum::<u64>() as f64
                        / memory_values.len() as f64
                        / 1_048_576.0,
                )
            } else {
                None
            }
        } else {
            None
        };

        // Calculate CPU usage
        let avg_cpu_usage = if operation_measurements
            .iter()
            .any(|m| m.cpu_usage_percent.is_some())
        {
            let cpu_values: Vec<f32> = operation_measurements
                .iter()
                .filter_map(|m| m.cpu_usage_percent)
                .collect();

            if !cpu_values.is_empty() {
                Some(cpu_values.iter().sum::<f32>() / cpu_values.len() as f32)
            } else {
                None
            }
        } else {
            None
        };

        // Calculate operations per second
        let ops_per_second = if avg_duration > 0.0 {
            1000.0 / avg_duration
        } else {
            0.0
        };

        let stats = PerformanceStats {
            operation: measurement.operation.clone(),
            measurement_count: count,
            total_duration_ms: total_duration,
            avg_duration_ms: avg_duration,
            min_duration_ms: min_duration,
            max_duration_ms: max_duration,
            std_dev_duration_ms: std_dev,
            p95_duration_ms: p95_duration,
            p99_duration_ms: p99_duration,
            avg_memory_usage_mb,
            avg_cpu_usage_percent: avg_cpu_usage,
            ops_per_second,
        };

        stats_cache.insert(measurement.operation.clone(), stats);
    }

    /// Check for performance alerts
    async fn check_performance_alerts(&self, measurement: &PerformanceMeasurement) {
        let mut alerts_to_add = Vec::new();

        // Check for slow operations
        if measurement.duration_ms > self.config.slow_operation_threshold_ms {
            alerts_to_add.push(PerformanceAlert {
                alert_type: PerformanceAlertType::SlowOperation,
                timestamp: chrono::Utc::now(),
                operation: measurement.operation.clone(),
                message: format!(
                    "Operation took {}ms (threshold: {}ms)",
                    measurement.duration_ms, self.config.slow_operation_threshold_ms
                ),
                current_value: measurement.duration_ms as f64,
                threshold: self.config.slow_operation_threshold_ms as f64,
            });
        }

        // Check for high memory usage
        if let (Some(before), Some(after)) = (
            measurement.memory_before_bytes,
            measurement.memory_after_bytes,
        ) {
            let memory_increase_mb = (after as i64 - before as i64) as f64 / 1_048_576.0;
            if memory_increase_mb > 50.0 {
                // 50MB threshold
                alerts_to_add.push(PerformanceAlert {
                    alert_type: PerformanceAlertType::HighMemoryUsage,
                    timestamp: chrono::Utc::now(),
                    operation: measurement.operation.clone(),
                    message: format!(
                        "Operation increased memory usage by {:.1}MB",
                        memory_increase_mb
                    ),
                    current_value: memory_increase_mb,
                    threshold: 50.0,
                });
            }
        }

        // Check for high CPU usage
        if let Some(cpu_usage) = measurement.cpu_usage_percent {
            if cpu_usage > 80.0 {
                alerts_to_add.push(PerformanceAlert {
                    alert_type: PerformanceAlertType::HighCpuUsage,
                    timestamp: chrono::Utc::now(),
                    operation: measurement.operation.clone(),
                    message: format!("Operation used {:.1}% CPU", cpu_usage),
                    current_value: cpu_usage as f64,
                    threshold: 80.0,
                });
            }
        }

        // Add alerts
        if !alerts_to_add.is_empty() {
            let mut alerts = self.alerts.lock().expect("lock should not be poisoned");
            for alert in alerts_to_add {
                alerts.push_back(alert);
            }

            // Maintain alert history size
            while alerts.len() > 100 {
                alerts.pop_front();
            }
        }
    }
}

/// Timer for measuring operation performance
pub struct OperationTimer {
    operation: String,
    start_time: Instant,
    start_timestamp: chrono::DateTime<chrono::Utc>,
    memory_before: Option<u64>,
    config: PerformanceMonitorConfig,
    metadata: HashMap<String, String>,
}

impl OperationTimer {
    fn new(operation: String, config: PerformanceMonitorConfig) -> Self {
        let memory_before = if config.monitor_memory {
            Self::get_memory_usage()
        } else {
            None
        };

        Self {
            operation,
            start_time: Instant::now(),
            start_timestamp: chrono::Utc::now(),
            memory_before,
            config,
            metadata: HashMap::new(),
        }
    }

    /// Add metadata to the measurement
    pub fn add_metadata(&mut self, key: &str, value: &str) {
        self.metadata.insert(key.to_string(), value.to_string());
    }

    /// Finish timing and create measurement
    pub async fn finish(self, monitor: &PerformanceMonitor) -> Result<(), EvaluationError> {
        let duration = self.start_time.elapsed();
        let duration_ms = duration.as_millis() as u64;

        let memory_after = if self.config.monitor_memory {
            Self::get_memory_usage()
        } else {
            None
        };

        let cpu_usage = if self.config.monitor_cpu {
            Self::get_cpu_usage()
        } else {
            None
        };

        let measurement = PerformanceMeasurement {
            operation: self.operation,
            start_time: self.start_timestamp,
            duration_ms,
            memory_before_bytes: self.memory_before,
            memory_after_bytes: memory_after,
            cpu_usage_percent: cpu_usage,
            audio_buffer_size: None, // Can be set via metadata
            sample_rate: None,       // Can be set via metadata
            metadata: self.metadata,
        };

        monitor.record_measurement(measurement).await;
        Ok(())
    }

    /// Get current memory usage (simplified implementation)
    fn get_memory_usage() -> Option<u64> {
        // This is a simplified implementation
        // In practice, you would use a proper memory monitoring library
        None
    }

    /// Get current CPU usage (simplified implementation)
    fn get_cpu_usage() -> Option<f32> {
        // This is a simplified implementation
        // In practice, you would use a proper CPU monitoring library
        None
    }
}

impl Default for PerformanceMonitor {
    fn default() -> Self {
        Self::new(PerformanceMonitorConfig::default())
    }
}

/// Advanced profiling system for detailed performance analysis
#[derive(Clone)]
pub struct AdvancedProfiler {
    monitor: Arc<PerformanceMonitor>,
    call_stack: Arc<Mutex<Vec<CallFrame>>>,
    hotspots: Arc<Mutex<HashMap<String, HotspotData>>>,
    sampling_enabled: Arc<std::sync::atomic::AtomicBool>,
}

/// Call frame for stack-based profiling
#[derive(Debug, Clone)]
pub struct CallFrame {
    /// Function or operation name
    pub name: String,
    /// Entry timestamp
    pub entry_time: Instant,
    /// Memory usage at entry
    pub memory_at_entry: Option<usize>,
    /// Parent frame index
    pub parent_index: Option<usize>,
}

/// Hotspot data for performance analysis
#[derive(Debug, Clone)]
pub struct HotspotData {
    /// Total time spent in this function/operation
    pub total_time: Duration,
    /// Number of times called
    pub call_count: usize,
    /// Average time per call
    pub avg_time: Duration,
    /// Maximum time spent in single call
    pub max_time: Duration,
    /// Total memory allocated
    pub total_memory: usize,
    /// Percentage of total runtime
    pub runtime_percentage: f64,
}

/// Performance regression detector
pub struct RegressionDetector {
    baseline_stats: HashMap<String, PerformanceStats>,
    sensitivity_threshold: f64,
    window_size: usize,
}

impl AdvancedProfiler {
    /// Create a new advanced profiler
    pub fn new(monitor: Arc<PerformanceMonitor>) -> Self {
        Self {
            monitor,
            call_stack: Arc::new(Mutex::new(Vec::new())),
            hotspots: Arc::new(Mutex::new(HashMap::new())),
            sampling_enabled: Arc::new(std::sync::atomic::AtomicBool::new(true)),
        }
    }

    /// Enter a function/operation for profiling
    pub fn enter_function(&self, name: &str) -> ProfilerScope {
        if !self
            .sampling_enabled
            .load(std::sync::atomic::Ordering::Relaxed)
        {
            return ProfilerScope::disabled();
        }

        let frame = CallFrame {
            name: name.to_string(),
            entry_time: Instant::now(),
            memory_at_entry: Self::get_current_memory(),
            parent_index: {
                let stack = self.call_stack.lock().expect("lock should not be poisoned");
                if stack.is_empty() {
                    None
                } else {
                    Some(stack.len() - 1)
                }
            },
        };

        let index = {
            let mut stack = self.call_stack.lock().expect("lock should not be poisoned");
            stack.push(frame);
            stack.len() - 1
        };

        ProfilerScope::new(self.clone(), index)
    }

    /// Exit a function/operation
    pub fn exit_function(&self, frame_index: usize) {
        let frame = {
            let mut stack = self.call_stack.lock().expect("lock should not be poisoned");
            if frame_index >= stack.len() {
                return;
            }
            stack.remove(frame_index)
        };

        let duration = frame.entry_time.elapsed();
        let memory_delta = Self::get_current_memory()
            .and_then(|current| {
                frame
                    .memory_at_entry
                    .map(|entry| current.saturating_sub(entry))
            })
            .unwrap_or(0);

        // Update hotspot data
        {
            let mut hotspots = self.hotspots.lock().expect("lock should not be poisoned");
            let hotspot = hotspots.entry(frame.name).or_insert_with(|| HotspotData {
                total_time: Duration::ZERO,
                call_count: 0,
                avg_time: Duration::ZERO,
                max_time: Duration::ZERO,
                total_memory: 0,
                runtime_percentage: 0.0,
            });

            hotspot.total_time += duration;
            hotspot.call_count += 1;
            hotspot.avg_time = hotspot.total_time / hotspot.call_count as u32;
            hotspot.max_time = hotspot.max_time.max(duration);
            hotspot.total_memory += memory_delta;
        }
    }

    /// Get hotspot analysis
    pub fn get_hotspots(&self) -> Vec<(String, HotspotData)> {
        let hotspots = self.hotspots.lock().expect("lock should not be poisoned");
        let total_time: Duration = hotspots.values().map(|h| h.total_time).sum();

        let mut results: Vec<_> = hotspots
            .iter()
            .map(|(name, data)| {
                let mut data = data.clone();
                data.runtime_percentage = if total_time.as_nanos() > 0 {
                    (data.total_time.as_nanos() as f64 / total_time.as_nanos() as f64) * 100.0
                } else {
                    0.0
                };
                (name.clone(), data)
            })
            .collect();

        // Sort by total time descending
        results.sort_by_key(|b| std::cmp::Reverse(b.1.total_time));
        results
    }

    /// Generate flame graph data
    pub fn generate_flame_graph(&self) -> String {
        let hotspots = self.get_hotspots();
        let mut flame_graph = String::new();

        flame_graph.push_str("Function,Time(ms),Calls,Avg(ms),Memory(KB)\n");

        for (name, data) in hotspots {
            flame_graph.push_str(&format!(
                "{},{:.2},{},{:.2},{}\n",
                name,
                data.total_time.as_secs_f64() * 1000.0,
                data.call_count,
                data.avg_time.as_secs_f64() * 1000.0,
                data.total_memory / 1024
            ));
        }

        flame_graph
    }

    /// Reset profiling data
    pub fn reset(&self) {
        self.call_stack
            .lock()
            .expect("lock should not be poisoned")
            .clear();
        self.hotspots
            .lock()
            .expect("lock should not be poisoned")
            .clear();
    }

    /// Enable or disable sampling
    pub fn set_sampling(&self, enabled: bool) {
        self.sampling_enabled
            .store(enabled, std::sync::atomic::Ordering::Relaxed);
    }

    fn get_current_memory() -> Option<usize> {
        // Platform-specific memory tracking would go here
        // For now, return None as a placeholder
        None
    }
}

/// RAII scope for automatic function profiling
pub struct ProfilerScope {
    profiler: Option<AdvancedProfiler>,
    frame_index: usize,
}

impl ProfilerScope {
    fn new(profiler: AdvancedProfiler, frame_index: usize) -> Self {
        Self {
            profiler: Some(profiler),
            frame_index,
        }
    }

    fn disabled() -> Self {
        Self {
            profiler: None,
            frame_index: 0,
        }
    }
}

impl Drop for ProfilerScope {
    fn drop(&mut self) {
        if let Some(profiler) = &self.profiler {
            profiler.exit_function(self.frame_index);
        }
    }
}

impl RegressionDetector {
    /// Create a new regression detector
    pub fn new(sensitivity_threshold: f64, window_size: usize) -> Self {
        Self {
            baseline_stats: HashMap::new(),
            sensitivity_threshold,
            window_size,
        }
    }

    /// Set baseline performance stats
    pub fn set_baseline(&mut self, stats: HashMap<String, PerformanceStats>) {
        self.baseline_stats = stats;
    }

    /// Detect performance regressions
    pub fn detect_regressions(
        &self,
        current_stats: &HashMap<String, PerformanceStats>,
    ) -> Vec<String> {
        let mut regressions = Vec::new();

        for (operation, current) in current_stats {
            if let Some(baseline) = self.baseline_stats.get(operation) {
                // Check duration regression
                let duration_change =
                    (current.avg_duration_ms - baseline.avg_duration_ms) / baseline.avg_duration_ms;
                if duration_change > self.sensitivity_threshold {
                    regressions.push(format!(
                        "Duration regression in '{}': {:.1}% slower ({:.2}ms → {:.2}ms)",
                        operation,
                        duration_change * 100.0,
                        baseline.avg_duration_ms,
                        current.avg_duration_ms
                    ));
                }

                // Check memory regression
                if let (Some(baseline_mem), Some(current_mem)) =
                    (baseline.avg_memory_usage_mb, current.avg_memory_usage_mb)
                {
                    let memory_change = (current_mem - baseline_mem) / baseline_mem;
                    if memory_change > self.sensitivity_threshold {
                        regressions.push(format!(
                            "Memory regression in '{}': {:.1}% increase ({:.2}MB → {:.2}MB)",
                            operation,
                            memory_change * 100.0,
                            baseline_mem,
                            current_mem
                        ));
                    }
                }

                // Check throughput regression
                let throughput_change =
                    (baseline.ops_per_second - current.ops_per_second) / baseline.ops_per_second;
                if throughput_change > self.sensitivity_threshold {
                    regressions.push(format!(
                        "Throughput regression in '{}': {:.1}% decrease ({:.2}{:.2} ops/sec)",
                        operation,
                        throughput_change * 100.0,
                        baseline.ops_per_second,
                        current.ops_per_second
                    ));
                }
            }
        }

        regressions
    }
}

/// Macro for easy function profiling
#[macro_export]
macro_rules! profile_function {
    ($profiler:expr, $name:expr, $body:block) => {{
        let _scope = $profiler.enter_function($name);
        $body
    }};
}

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

    #[tokio::test]
    async fn test_performance_monitor_creation() {
        let config = PerformanceMonitorConfig::default();
        let monitor = PerformanceMonitor::new(config);

        let stats = monitor.get_all_stats().await;
        assert!(stats.is_empty());
    }

    #[tokio::test]
    async fn test_operation_timing() {
        let monitor = PerformanceMonitor::default();

        {
            let timer = monitor.start_operation("test_operation");
            sleep(Duration::from_millis(10)).await;
            timer.finish(&monitor).await.unwrap();
        }

        // Allow time for async processing
        sleep(Duration::from_millis(50)).await;

        let stats = monitor.get_stats("test_operation").await;
        assert!(stats.is_some());
        let stats = stats.unwrap();
        assert_eq!(stats.operation, "test_operation");
        assert_eq!(stats.measurement_count, 1);
        assert!(stats.avg_duration_ms >= 10.0);
    }

    #[tokio::test]
    async fn test_multiple_measurements() {
        let monitor = PerformanceMonitor::default();

        // Record multiple measurements
        for i in 0..5 {
            let mut timer = monitor.start_operation("multi_test");
            timer.add_metadata("iteration", &i.to_string());
            sleep(Duration::from_millis(5 + i * 2)).await;
            timer.finish(&monitor).await.unwrap();
        }

        sleep(Duration::from_millis(50)).await;

        let stats = monitor.get_stats("multi_test").await;
        assert!(stats.is_some());
        let stats = stats.unwrap();
        assert_eq!(stats.measurement_count, 5);
        assert!(stats.avg_duration_ms > 0.0);
        assert!(stats.std_dev_duration_ms >= 0.0);
    }

    #[tokio::test]
    async fn test_performance_alerts() {
        let config = PerformanceMonitorConfig {
            slow_operation_threshold_ms: 10,
            ..Default::default()
        };
        let monitor = PerformanceMonitor::new(config);

        // Create a slow operation
        {
            let timer = monitor.start_operation("slow_operation");
            sleep(Duration::from_millis(20)).await;
            timer.finish(&monitor).await.unwrap();
        }

        sleep(Duration::from_millis(50)).await;

        let alerts = monitor.get_recent_alerts(10);
        assert!(!alerts.is_empty());
        assert_eq!(alerts[0].alert_type, PerformanceAlertType::SlowOperation);
    }

    #[tokio::test]
    async fn test_optimization_recommendations() {
        let config = PerformanceMonitorConfig {
            slow_operation_threshold_ms: 5,
            ..Default::default()
        };
        let monitor = PerformanceMonitor::new(config);

        // Create operations that will trigger recommendations
        {
            let timer = monitor.start_operation("slow_op");
            sleep(Duration::from_millis(10)).await;
            timer.finish(&monitor).await.unwrap();
        }

        sleep(Duration::from_millis(50)).await;

        let recommendations = monitor.generate_recommendations().await;
        assert!(!recommendations.is_empty());
        assert!(recommendations.iter().any(|r| r.category == "Performance"));
    }

    #[tokio::test]
    async fn test_performance_report() {
        let monitor = PerformanceMonitor::default();

        {
            let timer = monitor.start_operation("report_test");
            sleep(Duration::from_millis(5)).await;
            timer.finish(&monitor).await.unwrap();
        }

        sleep(Duration::from_millis(50)).await;

        let report = monitor.create_report().await;
        assert!(report.contains("Performance Report"));
        assert!(report.contains("report_test"));
    }

    #[test]
    fn test_performance_measurement() {
        let measurement = PerformanceMeasurement {
            operation: String::from("test"),
            start_time: chrono::Utc::now(),
            duration_ms: 100,
            memory_before_bytes: Some(1000),
            memory_after_bytes: Some(1200),
            cpu_usage_percent: Some(50.0),
            audio_buffer_size: Some(16000),
            sample_rate: Some(44100),
            metadata: HashMap::new(),
        };

        assert_eq!(measurement.operation, "test");
        assert_eq!(measurement.duration_ms, 100);
    }

    #[test]
    fn test_performance_stats() {
        let stats = PerformanceStats {
            operation: String::from("test_op"),
            measurement_count: 10,
            total_duration_ms: 1000,
            avg_duration_ms: 100.0,
            min_duration_ms: 50,
            max_duration_ms: 200,
            std_dev_duration_ms: 25.0,
            p95_duration_ms: 180,
            p99_duration_ms: 195,
            avg_memory_usage_mb: Some(50.0),
            avg_cpu_usage_percent: Some(25.0),
            ops_per_second: 10.0,
        };

        assert_eq!(stats.operation, "test_op");
        assert_eq!(stats.measurement_count, 10);
        assert_eq!(stats.ops_per_second, 10.0);
    }
}