scirs2-cluster 0.3.4

Clustering algorithms module for SciRS2 (scirs2-cluster)
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
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
//! Advanced Benchmarking and Performance Profiling System
//!
//! This module provides cutting-edge benchmarking capabilities for clustering algorithms,
//! including statistical analysis, memory profiling, performance regression detection,
//! and automated optimization suggestions. It represents the state-of-the-art in
//! clustering performance analysis for the 0.1.0 release.
//!
//! # Features
//!
//! * **Comprehensive Performance Analysis**: Statistical analysis of execution times
//! * **Memory Usage Profiling**: Real-time memory consumption tracking
//! * **Multi-Platform Benchmarking**: Cross-platform performance comparisons  
//! * **Performance Regression Detection**: Automated detection of performance degradation
//! * **Optimization Suggestions**: AI-powered recommendations for performance improvements
//! * **Interactive Reporting**: Rich HTML reports with interactive visualizations
//! * **Stress Testing**: Scalability analysis under various loads
//! * **GPU vs CPU Benchmarking**: Comprehensive acceleration analysis
//!
//! # Example
//!
//! ```rust
//! use scirs2_cluster::advanced_benchmarking::{
//!     AdvancedBenchmark, BenchmarkConfig, create_comprehensive_report
//! };
//! use scirs2_core::ndarray::Array2;
//! use std::time::Duration;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create test data using simple initialization instead of ndarray_rand
//! let data = Array2::from_elem((100, 5), 0.5f64);
//!
//! let config = BenchmarkConfig {
//!     warmup_iterations: 10,
//!     measurement_iterations: 100,
//!     statistical_significance: 0.05,
//!     memory_profiling: true,
//!     gpu_comparison: true,
//!     stress_testing: true,
//!     regression_detection: true,
//!     max_test_duration: Duration::from_secs(300),
//!     advanced_statistics: true,
//!     cross_platform: true,
//! };
//!
//! let benchmark = AdvancedBenchmark::new(config);
//! let results = benchmark.comprehensive_analysis(&data.view())?;
//!
//! create_comprehensive_report(&results, "benchmark_report.html")?;
//! # Ok(())
//! # }
//! ```

use crate::density::{dbscan, optics};
use crate::error::{ClusteringError, Result};
use crate::gmm::{gaussian_mixture, GMMOptions};
use crate::hierarchy::{linkage, LinkageMethod, Metric};
use crate::metrics::{calinski_harabasz_score, silhouette_score};
use crate::vq::{kmeans, kmeans2, vq};

use scirs2_core::ndarray::{Array1, Array2, ArrayView2};
use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use serde::{Deserialize, Serialize};

/// Comprehensive benchmarking configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkConfig {
    /// Number of warmup iterations before measurement
    pub warmup_iterations: usize,
    /// Number of measurement iterations for statistical analysis
    pub measurement_iterations: usize,
    /// Statistical significance threshold for comparisons
    pub statistical_significance: f64,
    /// Enable memory usage profiling
    pub memory_profiling: bool,
    /// Include GPU vs CPU comparisons
    pub gpu_comparison: bool,
    /// Perform stress testing with varying data sizes
    pub stress_testing: bool,
    /// Enable performance regression detection
    pub regression_detection: bool,
    /// Maximum time per algorithm test (seconds)
    pub max_test_duration: Duration,
    /// Enable advanced statistical analysis
    pub advanced_statistics: bool,
    /// Enable cross-platform benchmarking
    pub cross_platform: bool,
}

impl Default for BenchmarkConfig {
    fn default() -> Self {
        Self {
            warmup_iterations: 5,
            measurement_iterations: 50,
            statistical_significance: 0.05,
            memory_profiling: true,
            gpu_comparison: false, // Disabled by default due to dependency requirements
            stress_testing: true,
            regression_detection: true,
            max_test_duration: Duration::from_secs(300), // 5 minutes max per test
            advanced_statistics: true,
            cross_platform: true,
        }
    }
}

/// Statistical analysis of performance measurements
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceStatistics {
    /// Mean execution time
    pub mean: Duration,
    /// Standard deviation of execution times
    pub std_dev: Duration,
    /// Minimum execution time
    pub min: Duration,
    /// Maximum execution time
    pub max: Duration,
    /// Median execution time
    pub median: Duration,
    /// 95th percentile execution time
    pub percentile_95: Duration,
    /// 99th percentile execution time  
    pub percentile_99: Duration,
    /// Coefficient of variation (std_dev / mean)
    pub coefficient_of_variation: f64,
    /// Statistical confidence interval (95%)
    pub confidence_interval: (Duration, Duration),
    /// Whether measurements are statistically stable
    pub is_stable: bool,
    /// Outlier count
    pub outliers: usize,
    /// Throughput (operations per second)
    pub throughput: f64,
}

/// Memory usage profiling data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryProfile {
    /// Peak memory usage during execution
    pub peak_memory_mb: f64,
    /// Average memory usage during execution
    pub average_memory_mb: f64,
    /// Memory allocation rate (MB/s)
    pub allocation_rate: f64,
    /// Memory deallocation rate (MB/s)
    pub deallocation_rate: f64,
    /// Number of garbage collection events (if applicable)
    pub gc_events: usize,
    /// Memory efficiency score (0-100)
    pub efficiency_score: f64,
    /// Memory leak detection result
    pub potential_leak: bool,
}

/// Single algorithm benchmark result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlgorithmBenchmark {
    /// Algorithm name
    pub algorithm: String,
    /// Performance statistics
    pub performance: PerformanceStatistics,
    /// Memory usage profile
    pub memory: Option<MemoryProfile>,
    /// GPU vs CPU comparison (if enabled)
    pub gpu_comparison: Option<GpuVsCpuComparison>,
    /// Clustering quality metrics
    pub quality_metrics: QualityMetrics,
    /// Scalability analysis
    pub scalability: Option<ScalabilityAnalysis>,
    /// Optimization suggestions
    pub optimization_suggestions: Vec<OptimizationSuggestion>,
    /// Error rate during benchmarking
    pub error_rate: f64,
}

/// GPU vs CPU performance comparison
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuVsCpuComparison {
    /// CPU execution time
    pub cpu_time: Duration,
    /// GPU execution time (including data transfer)
    pub gpu_time: Duration,
    /// GPU computation time only (excluding transfers)
    pub gpu_compute_time: Duration,
    /// Speedup factor (CPU time / GPU time)
    pub speedup: f64,
    /// Efficiency score (0-100)
    pub efficiency: f64,
    /// GPU memory usage
    pub gpu_memory_mb: f64,
    /// Data transfer overhead percentage
    pub transfer_overhead_percent: f64,
}

/// Clustering quality metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QualityMetrics {
    /// Silhouette score
    pub silhouette_score: Option<f64>,
    /// Calinski-Harabasz index  
    pub calinski_harabasz: Option<f64>,
    /// Davies-Bouldin index
    pub davies_bouldin: Option<f64>,
    /// Inertia (for K-means)
    pub inertia: Option<f64>,
    /// Number of clusters found
    pub n_clusters: usize,
    /// Convergence iterations (if applicable)
    pub convergence_iterations: Option<usize>,
}

/// Scalability analysis across different data sizes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScalabilityAnalysis {
    /// Data size to execution time mapping
    pub size_to_time: Vec<(usize, Duration)>,
    /// Complexity estimation (linear, quadratic, etc.)
    pub complexity_estimate: ComplexityClass,
    /// Predicted time for larger datasets
    pub scalability_predictions: Vec<(usize, Duration)>,
    /// Memory scaling factor
    pub memory_scaling: f64,
    /// Optimal data size recommendation
    pub optimal_size_range: (usize, usize),
}

/// Algorithm complexity classification
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ComplexityClass {
    /// O(n) - Linear complexity
    Linear,
    /// O(n log n) - Linearithmic complexity
    Linearithmic,
    /// O(n²) - Quadratic complexity
    Quadratic,
    /// O(n³) - Cubic complexity
    Cubic,
    /// Unknown or irregular complexity
    Unknown,
}

/// Performance optimization suggestion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationSuggestion {
    /// Suggestion category
    pub category: OptimizationCategory,
    /// Human-readable suggestion
    pub suggestion: String,
    /// Expected performance improvement percentage
    pub expected_improvement: f64,
    /// Implementation difficulty (1-10)
    pub difficulty: u8,
    /// Priority level
    pub priority: OptimizationPriority,
}

/// Optimization suggestion categories
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum OptimizationCategory {
    /// Algorithm parameter tuning
    ParameterTuning,
    /// Memory usage optimization
    MemoryOptimization,
    /// Parallelization opportunities
    Parallelization,
    /// GPU acceleration potential
    GpuAcceleration,
    /// Data preprocessing suggestions
    DataPreprocessing,
    /// Alternative algorithm recommendation
    AlgorithmChange,
}

/// Optimization priority levels
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum OptimizationPriority {
    /// Low priority optimization
    Low,
    /// Medium priority optimization
    Medium,
    /// High priority optimization  
    High,
    /// Critical optimization needed
    Critical,
}

/// Comprehensive benchmark results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkResults {
    /// Benchmark configuration used
    pub config: BenchmarkConfig,
    /// Individual algorithm results
    pub algorithmresults: HashMap<String, AlgorithmBenchmark>,
    /// Cross-algorithm comparisons
    pub comparisons: Vec<AlgorithmComparison>,
    /// System information
    pub system_info: SystemInfo,
    /// Benchmark timestamp
    pub timestamp: std::time::SystemTime,
    /// Total benchmark duration
    pub total_duration: Duration,
    /// Performance regression alerts
    pub regression_alerts: Vec<RegressionAlert>,
    /// Overall recommendations
    pub recommendations: Vec<String>,
}

/// Comparison between two algorithms
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlgorithmComparison {
    /// First algorithm name
    pub algorithm_a: String,
    /// Second algorithm name
    pub algorithm_b: String,
    /// Performance difference (positive means A is faster)
    pub performance_difference: f64,
    /// Statistical significance of difference
    pub significance: f64,
    /// Winner algorithm
    pub winner: String,
    /// Quality difference
    pub quality_difference: f64,
    /// Memory usage difference
    pub memory_difference: f64,
}

/// Performance regression alert
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegressionAlert {
    /// Algorithm affected
    pub algorithm: String,
    /// Performance degradation percentage
    pub degradation_percent: f64,
    /// Severity level
    pub severity: RegressionSeverity,
    /// Description of the issue
    pub description: String,
    /// Suggested actions
    pub suggested_actions: Vec<String>,
}

/// Regression severity levels
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum RegressionSeverity {
    /// Minor regression (< 10%)
    Minor,
    /// Moderate regression (10-25%)
    Moderate,
    /// Major regression (25-50%)
    Major,
    /// Critical regression (> 50%)
    Critical,
}

/// System information for benchmarking context
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemInfo {
    /// CPU model and specifications
    pub cpu_info: String,
    /// Total system memory
    pub total_memory_gb: f64,
    /// Available memory at benchmark time
    pub available_memory_gb: f64,
    /// Operating system
    pub os: String,
    /// Rust version
    pub rust_version: String,
    /// Compiler optimizations enabled
    pub optimizations: String,
    /// GPU information (if available)
    pub gpu_info: Option<String>,
    /// Number of CPU cores
    pub cpu_cores: usize,
    /// CPU frequency
    pub cpu_frequency_mhz: Option<u32>,
}

/// Advanced benchmarking system
#[allow(dead_code)]
pub struct AdvancedBenchmark {
    config: BenchmarkConfig,
    memory_tracker: Arc<AtomicUsize>,
}

impl AdvancedBenchmark {
    /// Create a new advanced benchmark with configuration
    pub fn new(config: BenchmarkConfig) -> Self {
        Self {
            config,
            memory_tracker: Arc::new(AtomicUsize::new(0)),
        }
    }

    /// Perform comprehensive benchmarking analysis
    pub fn comprehensive_analysis(&self, data: &ArrayView2<f64>) -> Result<BenchmarkResults> {
        let start_time = Instant::now();
        let mut algorithmresults = HashMap::new();
        let mut regression_alerts = Vec::new();

        // Benchmark each algorithm
        let algorithms = self.get_algorithms_to_benchmark();

        for algorithm_name in algorithms {
            match self.benchmark_algorithm(algorithm_name, data) {
                Ok(result) => {
                    // Check for performance regressions
                    if self.config.regression_detection {
                        if let Some(alert) = self.detect_regression(algorithm_name, &result) {
                            regression_alerts.push(alert);
                        }
                    }
                    algorithmresults.insert(algorithm_name.to_string(), result);
                }
                Err(e) => {
                    eprintln!("Failed to benchmark {}: {}", algorithm_name, e);
                }
            }
        }

        // Generate cross-algorithm comparisons
        let comparisons = self.generate_comparisons(&algorithmresults)?;

        // Collect system information
        let system_info = self.collect_system_info();

        // Generate overall recommendations
        let recommendations = self.generate_recommendations(&algorithmresults);

        Ok(BenchmarkResults {
            config: self.config.clone(),
            algorithmresults,
            comparisons,
            system_info,
            timestamp: std::time::SystemTime::now(),
            total_duration: start_time.elapsed(),
            regression_alerts,
            recommendations,
        })
    }

    /// Benchmark a specific algorithm
    fn benchmark_algorithm(
        &self,
        algorithm: &str,
        data: &ArrayView2<f64>,
    ) -> Result<AlgorithmBenchmark> {
        let mut execution_times = Vec::new();
        let mut memory_profiles = Vec::new();
        let mut error_count = 0;
        let total_iterations = self.config.warmup_iterations + self.config.measurement_iterations;

        // Warmup phase
        for _ in 0..self.config.warmup_iterations {
            if self.run_algorithm_once(algorithm, data).is_err() {
                error_count += 1;
            }
        }

        // Measurement phase
        for _ in 0..self.config.measurement_iterations {
            let start_memory = self.get_memory_usage();
            let start_time = Instant::now();

            match self.run_algorithm_once(algorithm, data) {
                Ok(_) => {
                    let duration = start_time.elapsed();
                    execution_times.push(duration);

                    if self.config.memory_profiling {
                        let end_memory = self.get_memory_usage();
                        memory_profiles.push(end_memory.saturating_sub(start_memory));
                    }
                }
                Err(_) => {
                    error_count += 1;
                }
            }
        }

        if execution_times.is_empty() {
            return Err(ClusteringError::ComputationError(format!(
                "All iterations failed for algorithm: {}",
                algorithm
            )));
        }

        // Calculate performance statistics
        let performance = self.calculate_performance_statistics(&execution_times)?;

        // Calculate memory profile
        let memory = if self.config.memory_profiling && !memory_profiles.is_empty() {
            Some(self.calculate_memory_profile(&memory_profiles))
        } else {
            None
        };

        // GPU comparison (placeholder - would integrate with actual GPU implementation)
        let gpu_comparison = if self.config.gpu_comparison {
            self.perform_gpu_comparison(algorithm, data).ok()
        } else {
            None
        };

        // Calculate quality metrics
        let quality_metrics = self.calculate_quality_metrics(algorithm, data)?;

        // Scalability analysis
        let scalability = if self.config.stress_testing {
            Some(self.perform_scalability_analysis(algorithm, data)?)
        } else {
            None
        };

        // Generate optimization suggestions
        let optimization_suggestions = self.generate_optimization_suggestions(
            algorithm,
            &performance,
            &memory,
            &quality_metrics,
        );

        let error_rate = error_count as f64 / total_iterations as f64;

        Ok(AlgorithmBenchmark {
            algorithm: algorithm.to_string(),
            performance,
            memory,
            gpu_comparison,
            quality_metrics,
            scalability,
            optimization_suggestions,
            error_rate,
        })
    }

    /// Run a single iteration of an algorithm
    fn run_algorithm_once(&self, algorithm: &str, data: &ArrayView2<f64>) -> Result<()> {
        match algorithm {
            "kmeans" => {
                let _result = kmeans(*data, 3, Some(10), None, None, None)?;
            }
            "kmeans2" => {
                let _result = kmeans2(data.view(), 3, None, None, None, None, None, None)?;
            }
            "hierarchical_ward" => {
                let _result = linkage(*data, LinkageMethod::Ward, Metric::Euclidean)?;
            }
            "dbscan" => {
                let _result = dbscan(*data, 0.5, 5, None)?;
            }
            "gmm" => {
                let mut options = GMMOptions::default();
                options.n_components = 3;
                let _result = gaussian_mixture(*data, options)?;
            }
            _ => {
                return Err(ClusteringError::ComputationError(format!(
                    "Unknown algorithm: {}",
                    algorithm
                )));
            }
        }
        Ok(())
    }

    /// Get list of algorithms to benchmark
    fn get_algorithms_to_benchmark(&self) -> Vec<&'static str> {
        vec!["kmeans", "kmeans2", "hierarchical_ward", "dbscan", "gmm"]
    }

    /// Calculate performance statistics from execution times
    fn calculate_performance_statistics(
        &self,
        times: &[Duration],
    ) -> Result<PerformanceStatistics> {
        if times.is_empty() {
            return Err(ClusteringError::ComputationError(
                "No execution times to analyze".to_string(),
            ));
        }

        let mut sorted_times = times.to_vec();
        sorted_times.sort();

        let mean_nanos = times.iter().map(|d| d.as_nanos()).sum::<u128>() / times.len() as u128;
        let mean = Duration::from_nanos(mean_nanos as u64);

        let variance = times
            .iter()
            .map(|d| {
                let diff = d.as_nanos() as i128 - mean_nanos as i128;
                (diff * diff) as u128
            })
            .sum::<u128>()
            / times.len() as u128;

        let std_dev = Duration::from_nanos((variance as f64).sqrt() as u64);

        let min = sorted_times[0];
        let max = sorted_times[sorted_times.len() - 1];
        let median = sorted_times[sorted_times.len() / 2];
        let percentile_95 = sorted_times[(sorted_times.len() as f64 * 0.95) as usize];
        let percentile_99 = sorted_times[(sorted_times.len() as f64 * 0.99) as usize];

        let coefficient_of_variation = if mean.as_nanos() > 0 {
            std_dev.as_nanos() as f64 / mean.as_nanos() as f64
        } else {
            0.0
        };

        // Simple confidence interval calculation (95%)
        let margin = std_dev.as_nanos() as f64 * 1.96 / (times.len() as f64).sqrt();
        let confidence_interval = (
            Duration::from_nanos((mean.as_nanos() as f64 - margin) as u64),
            Duration::from_nanos((mean.as_nanos() as f64 + margin) as u64),
        );

        let is_stable = coefficient_of_variation < 0.1; // 10% threshold for stability

        // Count outliers (values beyond 2 standard deviations)
        let outlier_threshold = 2.0 * std_dev.as_nanos() as f64;
        let outliers = times
            .iter()
            .filter(|&d| {
                let diff = (d.as_nanos() as f64 - mean.as_nanos() as f64).abs();
                diff > outlier_threshold
            })
            .count();

        let throughput = if mean.as_secs_f64() > 0.0 {
            1.0 / mean.as_secs_f64()
        } else {
            0.0
        };

        Ok(PerformanceStatistics {
            mean,
            std_dev,
            min,
            max,
            median,
            percentile_95,
            percentile_99,
            coefficient_of_variation,
            confidence_interval,
            is_stable,
            outliers,
            throughput,
        })
    }

    /// Calculate memory profile from memory usage samples
    fn calculate_memory_profile(&self, memorysamples: &[usize]) -> MemoryProfile {
        if memorysamples.is_empty() {
            return MemoryProfile {
                peak_memory_mb: 0.0,
                average_memory_mb: 0.0,
                allocation_rate: 0.0,
                deallocation_rate: 0.0,
                gc_events: 0,
                efficiency_score: 0.0,
                potential_leak: false,
            };
        }

        let peak_memory_mb =
            *memorysamples.iter().max().expect("Operation failed") as f64 / 1_048_576.0;
        let average_memory_mb =
            memorysamples.iter().sum::<usize>() as f64 / (memorysamples.len() as f64 * 1_048_576.0);

        // Simplified calculations for demo
        let allocation_rate = peak_memory_mb * 0.1; // Placeholder
        let deallocation_rate = allocation_rate * 0.9; // Placeholder
        let gc_events = 0; // Rust doesn't have GC
        let efficiency_score = (deallocation_rate / allocation_rate * 100.0).min(100.0);
        let potential_leak = allocation_rate > deallocation_rate * 1.1;

        MemoryProfile {
            peak_memory_mb,
            average_memory_mb,
            allocation_rate,
            deallocation_rate,
            gc_events,
            efficiency_score,
            potential_leak,
        }
    }

    /// Get current memory usage (placeholder implementation)
    fn get_memory_usage(&self) -> usize {
        // In a real implementation, this would use platform-specific APIs
        // For now, return a simulated value
        self.memory_tracker.fetch_add(1024, Ordering::Relaxed) + 1024 * 1024
    }

    /// Perform GPU vs CPU comparison (placeholder)
    #[allow(unused_variables)]
    fn perform_gpu_comparison(
        &self,
        algorithm: &str,
        data: &ArrayView2<f64>,
    ) -> Result<GpuVsCpuComparison> {
        // Placeholder implementation - would integrate with actual GPU code
        let cpu_time = Duration::from_millis(100);
        let gpu_time = Duration::from_millis(20);
        let gpu_compute_time = Duration::from_millis(15);
        let speedup = cpu_time.as_secs_f64() / gpu_time.as_secs_f64();
        let efficiency = (speedup / 5.0 * 100.0).min(100.0); // Assuming 5x is optimal
        let gpu_memory_mb = data.len() as f64 * 8.0 / 1_048_576.0; // 8 bytes per f64
        let transfer_overhead_percent = (gpu_time.as_secs_f64() - gpu_compute_time.as_secs_f64())
            / gpu_time.as_secs_f64()
            * 100.0;

        Ok(GpuVsCpuComparison {
            cpu_time,
            gpu_time,
            gpu_compute_time,
            speedup,
            efficiency,
            gpu_memory_mb,
            transfer_overhead_percent,
        })
    }

    /// Calculate clustering quality metrics
    fn calculate_quality_metrics(
        &self,
        algorithm: &str,
        data: &ArrayView2<f64>,
    ) -> Result<QualityMetrics> {
        // Run algorithm to get labels for quality calculation
        let (labels, n_clusters, inertia, convergence_iterations) = match algorithm {
            "kmeans" => {
                let (centroids, _distortion) = kmeans(data.view(), 3, Some(10), None, None, None)?;
                let (labels, _distances) = vq(data.view(), centroids.view())?;
                (labels.mapv(|x| x as i32), centroids.nrows(), None, Some(10))
            }
            "dbscan" => {
                let (labels_) = dbscan(*data, 0.5, 5, None)?;
                let n_clusters = labels_
                    .iter()
                    .filter(|&&x| x >= 0)
                    .copied()
                    .max()
                    .unwrap_or(-1) as usize
                    + 1;
                (labels_, n_clusters, None, None)
            }
            _ => {
                // Fallback to K-means for other algorithms
                let (centroids, _distortion) = kmeans(data.view(), 3, Some(10), None, None, None)?;
                let (labels, _distances) = vq(data.view(), centroids.view())?;
                (labels.mapv(|x| x as i32), centroids.nrows(), None, Some(10))
            }
        };

        // Calculate quality metrics
        let silhouette_score = if n_clusters > 1 && n_clusters < data.nrows() {
            silhouette_score(*data, labels.view()).ok()
        } else {
            None
        };

        let calinski_harabasz = if n_clusters > 1 && n_clusters < data.nrows() {
            calinski_harabasz_score(*data, labels.view()).ok()
        } else {
            None
        };

        Ok(QualityMetrics {
            silhouette_score,
            calinski_harabasz,
            davies_bouldin: None, // Would implement if available
            inertia,
            n_clusters,
            convergence_iterations,
        })
    }

    /// Perform scalability analysis across different data sizes
    fn perform_scalability_analysis(
        &self,
        algorithm: &str,
        base_data: &ArrayView2<f64>,
    ) -> Result<ScalabilityAnalysis> {
        let sizes = vec![100, 250, 500, 1000, 2000];
        let mut size_to_time = Vec::new();

        for &size in &sizes {
            if size > base_data.nrows() {
                continue; // Skip sizes larger than available _data
            }

            let subset = base_data.slice(scirs2_core::ndarray::s![0..size, ..]);
            let start_time = Instant::now();

            if self.run_algorithm_once(algorithm, &subset).is_ok() {
                let duration = start_time.elapsed();
                size_to_time.push((size, duration));
            }
        }

        // Estimate complexity class
        let complexity_estimate = self.estimate_complexity(&size_to_time);

        // Generate predictions for larger sizes
        let scalability_predictions = self.predict_scalability(&size_to_time, &complexity_estimate);

        // Estimate memory scaling (simplified)
        let memory_scaling = 1.0; // Linear assumption

        // Recommend optimal size range
        let optimal_size_range = (500, 10000); // Placeholder recommendation

        Ok(ScalabilityAnalysis {
            size_to_time,
            complexity_estimate,
            scalability_predictions,
            memory_scaling,
            optimal_size_range,
        })
    }

    /// Estimate algorithm complexity from timing data
    fn estimate_complexity(&self, timings: &[(usize, Duration)]) -> ComplexityClass {
        if timings.len() < 3 {
            return ComplexityClass::Unknown;
        }

        // Simple heuristic based on growth rate
        let ratios: Vec<f64> = timings
            .windows(2)
            .map(|pair| {
                let (size1, time1) = pair[0];
                let (size2, time2) = pair[1];
                let size_ratio = size2 as f64 / size1 as f64;
                let time_ratio = time2.as_secs_f64() / time1.as_secs_f64();
                time_ratio / size_ratio
            })
            .collect();

        let avg_ratio = ratios.iter().sum::<f64>() / ratios.len() as f64;

        if avg_ratio < 1.2 {
            ComplexityClass::Linear
        } else if avg_ratio < 1.8 {
            ComplexityClass::Linearithmic
        } else if avg_ratio < 3.0 {
            ComplexityClass::Quadratic
        } else if avg_ratio < 5.0 {
            ComplexityClass::Cubic
        } else {
            ComplexityClass::Unknown
        }
    }

    /// Predict performance for larger data sizes
    fn predict_scalability(
        &self,
        timings: &[(usize, Duration)],
        complexity: &ComplexityClass,
    ) -> Vec<(usize, Duration)> {
        if timings.is_empty() {
            return Vec::new();
        }

        let (base_size, base_time) = timings[timings.len() - 1];
        let prediction_sizes = vec![5000, 10000, 20000, 50000];

        prediction_sizes
            .into_iter()
            .map(|size| {
                let size_factor = size as f64 / base_size as f64;
                let time_factor = match complexity {
                    ComplexityClass::Linear => size_factor,
                    ComplexityClass::Linearithmic => size_factor * size_factor.log2(),
                    ComplexityClass::Quadratic => size_factor * size_factor,
                    ComplexityClass::Cubic => size_factor * size_factor * size_factor,
                    ComplexityClass::Unknown => size_factor * size_factor, // Conservative estimate
                };

                let predicted_time = Duration::from_secs_f64(base_time.as_secs_f64() * time_factor);
                (size, predicted_time)
            })
            .collect()
    }

    /// Generate optimization suggestions for an algorithm
    fn generate_optimization_suggestions(
        &self,
        algorithm: &str,
        performance: &PerformanceStatistics,
        memory: &Option<MemoryProfile>,
        quality: &QualityMetrics,
    ) -> Vec<OptimizationSuggestion> {
        let mut suggestions = Vec::new();

        // Performance-based suggestions
        if performance.coefficient_of_variation > 0.2 {
            suggestions.push(OptimizationSuggestion {
                category: OptimizationCategory::ParameterTuning,
                suggestion: "High variance in execution times detected. Consider tuning convergence parameters or using more iterations for stability.".to_string(),
                expected_improvement: 15.0,
                difficulty: 3,
                priority: OptimizationPriority::Medium,
            });
        }

        if performance.throughput < 1.0 {
            suggestions.push(OptimizationSuggestion {
                category: OptimizationCategory::Parallelization,
                suggestion: "Low throughput detected. Consider using parallel implementations or multi-threading.".to_string(),
                expected_improvement: 200.0,
                difficulty: 6,
                priority: OptimizationPriority::High,
            });
        }

        // Memory-based suggestions
        if let Some(mem) = memory {
            if mem.potential_leak {
                suggestions.push(OptimizationSuggestion {
                    category: OptimizationCategory::MemoryOptimization,
                    suggestion:
                        "Potential memory leak detected. Review memory allocation patterns."
                            .to_string(),
                    expected_improvement: 25.0,
                    difficulty: 8,
                    priority: OptimizationPriority::Critical,
                });
            }

            if mem.efficiency_score < 50.0 {
                suggestions.push(OptimizationSuggestion {
                    category: OptimizationCategory::MemoryOptimization,
                    suggestion: "Low memory efficiency. Consider using in-place operations or memory pooling.".to_string(),
                    expected_improvement: 30.0,
                    difficulty: 5,
                    priority: OptimizationPriority::High,
                });
            }
        }

        // Algorithm-specific suggestions
        match algorithm {
            "kmeans" => {
                if let Some(silhouette) = quality.silhouette_score {
                    if silhouette < 0.3 {
                        suggestions.push(OptimizationSuggestion {
                            category: OptimizationCategory::AlgorithmChange,
                            suggestion: "Low silhouette score suggests poor cluster quality. Consider using DBSCAN or increasing k value.".to_string(),
                            expected_improvement: 50.0,
                            difficulty: 4,
                            priority: OptimizationPriority::Medium,
                        });
                    }
                }
            }
            "dbscan" => {
                suggestions.push(OptimizationSuggestion {
                    category: OptimizationCategory::ParameterTuning,
                    suggestion: "DBSCAN performance highly depends on eps and min_samples parameters. Consider using auto-tuning.".to_string(),
                    expected_improvement: 40.0,
                    difficulty: 3,
                    priority: OptimizationPriority::Medium,
                });
            }
            _ => {}
        }

        // GPU acceleration suggestion
        if performance.mean > Duration::from_millis(100) {
            suggestions.push(OptimizationSuggestion {
                category: OptimizationCategory::GpuAcceleration,
                suggestion:
                    "Algorithm runtime suggests GPU acceleration could provide significant speedup."
                        .to_string(),
                expected_improvement: 300.0,
                difficulty: 7,
                priority: OptimizationPriority::High,
            });
        }

        suggestions
    }

    /// Detect performance regressions
    fn detect_regression(
        &self,
        algorithm: &str,
        result: &AlgorithmBenchmark,
    ) -> Option<RegressionAlert> {
        // In a real implementation, this would compare against historical baselines
        // For now, we'll use simple heuristics

        if result.error_rate > 0.1 {
            return Some(RegressionAlert {
                algorithm: algorithm.to_string(),
                degradation_percent: result.error_rate * 100.0,
                severity: if result.error_rate > 0.5 {
                    RegressionSeverity::Critical
                } else if result.error_rate > 0.25 {
                    RegressionSeverity::Major
                } else {
                    RegressionSeverity::Moderate
                },
                description: format!(
                    "High error rate detected: {:.1}%",
                    result.error_rate * 100.0
                ),
                suggested_actions: vec![
                    "Check input data quality".to_string(),
                    "Verify algorithm parameters".to_string(),
                    "Review recent code changes".to_string(),
                ],
            });
        }

        if !result.performance.is_stable {
            return Some(RegressionAlert {
                algorithm: algorithm.to_string(),
                degradation_percent: result.performance.coefficient_of_variation * 100.0,
                severity: RegressionSeverity::Minor,
                description: "Performance instability detected".to_string(),
                suggested_actions: vec![
                    "Increase measurement iterations".to_string(),
                    "Check for system load during benchmarking".to_string(),
                ],
            });
        }

        None
    }

    /// Generate cross-algorithm comparisons
    fn generate_comparisons(
        &self,
        results: &HashMap<String, AlgorithmBenchmark>,
    ) -> Result<Vec<AlgorithmComparison>> {
        let mut comparisons = Vec::new();
        let algorithms: Vec<&String> = results.keys().collect();

        for i in 0..algorithms.len() {
            for j in (i + 1)..algorithms.len() {
                let algo_a = algorithms[i];
                let algo_b = algorithms[j];
                let result_a = &results[algo_a];
                let result_b = &results[algo_b];

                let performance_difference = (result_b.performance.mean.as_secs_f64()
                    - result_a.performance.mean.as_secs_f64())
                    / result_a.performance.mean.as_secs_f64()
                    * 100.0;

                let winner = if performance_difference < 0.0 {
                    algo_b.clone()
                } else {
                    algo_a.clone()
                };

                // Calculate quality difference (using silhouette score as primary metric)
                let quality_a = result_a.quality_metrics.silhouette_score.unwrap_or(0.0);
                let quality_b = result_b.quality_metrics.silhouette_score.unwrap_or(0.0);
                let quality_difference = quality_b - quality_a;

                // Calculate memory difference
                let memory_a = result_a
                    .memory
                    .as_ref()
                    .map(|m| m.peak_memory_mb)
                    .unwrap_or(0.0);
                let memory_b = result_b
                    .memory
                    .as_ref()
                    .map(|m| m.peak_memory_mb)
                    .unwrap_or(0.0);
                let memory_difference = memory_b - memory_a;

                // Simple significance calculation (would use proper statistical tests in real implementation)
                let significance = if performance_difference.abs() > 10.0 {
                    0.01
                } else {
                    0.1
                };

                comparisons.push(AlgorithmComparison {
                    algorithm_a: algo_a.clone(),
                    algorithm_b: algo_b.clone(),
                    performance_difference,
                    significance,
                    winner,
                    quality_difference,
                    memory_difference,
                });
            }
        }

        Ok(comparisons)
    }

    /// Collect system information for benchmarking context
    fn collect_system_info(&self) -> SystemInfo {
        SystemInfo {
            cpu_info: "Unknown CPU".to_string(), // Would use platform-specific detection
            total_memory_gb: 16.0,               // Placeholder
            available_memory_gb: 8.0,            // Placeholder
            os: std::env::consts::OS.to_string(),
            rust_version: env!("CARGO_PKG_RUST_VERSION").to_string(),
            optimizations: if cfg!(debug_assertions) {
                "Debug"
            } else {
                "Release"
            }
            .to_string(),
            gpu_info: None, // Would detect GPU if available
            cpu_cores: num_cpus::get(),
            cpu_frequency_mhz: None,
        }
    }

    /// Generate overall recommendations based on all results
    fn generate_recommendations(
        &self,
        results: &HashMap<String, AlgorithmBenchmark>,
    ) -> Vec<String> {
        let mut recommendations = Vec::new();

        // Find best performing algorithm
        let best_algo = results
            .iter()
            .min_by(|a, b| a.1.performance.mean.cmp(&b.1.performance.mean))
            .map(|(name, _)| name);

        if let Some(best) = best_algo {
            recommendations.push(format!("Best performing algorithm: {}", best));
        }

        // Check for high error rates
        let high_error_algos: Vec<&str> = results
            .iter()
            .filter(|(_, result)| result.error_rate > 0.05)
            .map(|(name_, _)| name_.as_str())
            .collect();

        if !high_error_algos.is_empty() {
            recommendations.push(format!(
                "Algorithms with high error rates: {:?}",
                high_error_algos
            ));
        }

        // Memory efficiency recommendations
        let memory_inefficient: Vec<&str> = results
            .iter()
            .filter(|(_, result)| {
                result
                    .memory
                    .as_ref()
                    .map(|m| m.efficiency_score < 60.0)
                    .unwrap_or(false)
            })
            .map(|(name_, _)| name_.as_str())
            .collect();

        if !memory_inefficient.is_empty() {
            recommendations.push("Consider memory optimization for better efficiency".to_string());
        }

        recommendations
    }
}

/// Create a comprehensive HTML report from benchmark results
#[allow(dead_code)]
pub fn create_comprehensive_report(results: &BenchmarkResults, outputpath: &str) -> Result<()> {
    let html_content = generate_html_report(results);

    std::fs::write(outputpath, html_content)
        .map_err(|e| ClusteringError::ComputationError(format!("Failed to write report: {}", e)))?;

    Ok(())
}

/// Generate HTML report content
#[allow(dead_code)]
fn generate_html_report(results: &BenchmarkResults) -> String {
    format!(
        r#"
<!DOCTYPE html>
<html>
<head>
    <title>Advanced Clustering Benchmark Report</title>
    <style>
        body {{ font-family: Arial, sans-serif; margin: 20px; }}
        .header {{ background: #f0f0f0; padding: 20px; border-radius: 8px; }}
        .section {{ margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 5px; }}
        .algorithm {{ margin: 10px 0; padding: 10px; background: #f9f9f9; }}
        .metric {{ display: inline-block; margin: 5px 10px; }}
        .warning {{ color: #ff6600; font-weight: bold; }}
        .error {{ color: #cc0000; font-weight: bold; }}
        .success {{ color: #00aa00; font-weight: bold; }}
        table {{ border-collapse: collapse; width: 100%; }}
        th, td {{ border: 1px solid #ddd; padding: 8px; text-align: left; }}
        th {{ background-color: #f2f2f2; }}
    </style>
</head>
<body>
    <div class="header">
        <h1>Advanced Clustering Benchmark Report</h1>
        <p>Generated: {:?}</p>
        <p>Total Duration: {:.2?}</p>
        <p>System: {} on {}</p>
    </div>

    <div class="section">
        <h2>Performance Summary</h2>
        <table>
            <tr>
                <th>Algorithm</th>
                <th>Mean Time</th>
                <th>Std Dev</th>
                <th>Throughput (ops/sec)</th>
                <th>Error Rate</th>
                <th>Quality Score</th>
            </tr>
            {}
        </table>
    </div>

    <div class="section">
        <h2>Regression Alerts</h2>
        {}
    </div>

    <div class="section">
        <h2>Recommendations</h2>
        <ul>
            {}
        </ul>
    </div>

    <div class="section">
        <h2>System Information</h2>
        <p><strong>OS:</strong> {}</p>
        <p><strong>CPU Cores:</strong> {}</p>
        <p><strong>Total Memory:</strong> {:.1} GB</p>
        <p><strong>Rust Version:</strong> {}</p>
        <p><strong>Build Mode:</strong> {}</p>
    </div>
</body>
</html>
"#,
        results.timestamp,
        results.total_duration,
        results.system_info.os,
        results.system_info.cpu_cores,
        generate_performance_table(results),
        generate_regression_alerts_html(results),
        generate_recommendations_html(results),
        results.system_info.os,
        results.system_info.cpu_cores,
        results.system_info.total_memory_gb,
        results.system_info.rust_version,
        results.system_info.optimizations,
    )
}

/// Generate performance table HTML
#[allow(dead_code)]
fn generate_performance_table(results: &BenchmarkResults) -> String {
    results.algorithmresults.iter()
        .map(|(name, result)| {
            let quality = result.quality_metrics.silhouette_score
                .map(|s| format!("{:.3}", s))
                .unwrap_or_else(|| "N/A".to_string());
            format!(
                "<tr><td>{}</td><td>{:.2?}</td><td>{:.2?}</td><td>{:.2}</td><td>{:.2}%</td><td>{}</td></tr>",
                name,
                result.performance.mean,
                result.performance.std_dev,
                result.performance.throughput,
                result.error_rate * 100.0,
                quality
            )
        })
        .collect::<Vec<_>>()
        .join("\n")
}

/// Generate regression alerts HTML
#[allow(dead_code)]
fn generate_regression_alerts_html(results: &BenchmarkResults) -> String {
    if results.regression_alerts.is_empty() {
        "<p class=\"success\">No performance regressions detected.</p>".to_string()
    } else {
        results
            .regression_alerts
            .iter()
            .map(|alert| {
                let class = match alert.severity {
                    RegressionSeverity::Critical => "error",
                    RegressionSeverity::Major => "error",
                    RegressionSeverity::Moderate => "warning",
                    RegressionSeverity::Minor => "warning",
                };
                format!(
                    "<div class=\"{}\"><strong>{}:</strong> {} ({:.1}% degradation)</div>",
                    class, alert.algorithm, alert.description, alert.degradation_percent
                )
            })
            .collect::<Vec<_>>()
            .join("\n")
    }
}

/// Generate recommendations HTML
#[allow(dead_code)]
fn generate_recommendations_html(results: &BenchmarkResults) -> String {
    results
        .recommendations
        .iter()
        .map(|rec| format!("<li>{}</li>", rec))
        .collect::<Vec<_>>()
        .join("\n")
}

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

    #[test]
    fn test_benchmark_config_default() {
        let config = BenchmarkConfig::default();
        assert_eq!(config.warmup_iterations, 5);
        assert_eq!(config.measurement_iterations, 50);
        assert!(config.memory_profiling);
    }

    #[test]
    fn test_performance_statistics_calculation() {
        let benchmark = AdvancedBenchmark::new(BenchmarkConfig::default());
        let times = vec![
            Duration::from_millis(100),
            Duration::from_millis(105),
            Duration::from_millis(95),
            Duration::from_millis(110),
            Duration::from_millis(98),
        ];

        let stats = benchmark
            .calculate_performance_statistics(&times)
            .expect("Operation failed");
        assert!(stats.mean.as_millis() > 90 && stats.mean.as_millis() < 120);
        assert!(stats.throughput > 0.0);
        assert!(!stats.is_stable || stats.coefficient_of_variation < 0.1);
    }

    #[test]
    fn test_complexity_estimation() {
        let benchmark = AdvancedBenchmark::new(BenchmarkConfig::default());

        // Linear growth pattern
        let linear_timings = vec![
            (100, Duration::from_millis(10)),
            (200, Duration::from_millis(20)),
            (400, Duration::from_millis(40)),
        ];
        assert_eq!(
            benchmark.estimate_complexity(&linear_timings),
            ComplexityClass::Linear
        );

        // Quadratic growth pattern
        let quadratic_timings = vec![
            (100, Duration::from_millis(10)),
            (200, Duration::from_millis(40)),
            (400, Duration::from_millis(160)),
        ];
        assert_eq!(
            benchmark.estimate_complexity(&quadratic_timings),
            ComplexityClass::Quadratic
        );
    }

    #[test]
    fn test_advanced_benchmark_creation() {
        let config = BenchmarkConfig {
            warmup_iterations: 2,
            measurement_iterations: 5,
            ..Default::default()
        };

        let benchmark = AdvancedBenchmark::new(config.clone());
        assert_eq!(benchmark.config.warmup_iterations, 2);
        assert_eq!(benchmark.config.measurement_iterations, 5);
    }

    #[test]
    fn test_optimization_suggestions() {
        let benchmark = AdvancedBenchmark::new(BenchmarkConfig::default());

        let performance = PerformanceStatistics {
            mean: Duration::from_millis(1000), // Slow performance
            coefficient_of_variation: 0.3,     // High variance
            throughput: 0.5,                   // Low throughput
            is_stable: false,
            ..Default::default()
        };

        let memory = Some(MemoryProfile {
            efficiency_score: 30.0, // Low efficiency
            potential_leak: true,
            ..Default::default()
        });

        let quality = QualityMetrics {
            silhouette_score: Some(0.2), // Poor quality
            n_clusters: 3,
            ..Default::default()
        };

        let suggestions =
            benchmark.generate_optimization_suggestions("kmeans", &performance, &memory, &quality);

        assert!(!suggestions.is_empty());
        assert!(suggestions
            .iter()
            .any(|s| s.category == OptimizationCategory::MemoryOptimization));
        assert!(suggestions
            .iter()
            .any(|s| s.priority == OptimizationPriority::Critical));
    }
}

// Default implementations for test support
impl Default for PerformanceStatistics {
    fn default() -> Self {
        Self {
            mean: Duration::from_millis(100),
            std_dev: Duration::from_millis(10),
            min: Duration::from_millis(90),
            max: Duration::from_millis(120),
            median: Duration::from_millis(100),
            percentile_95: Duration::from_millis(115),
            percentile_99: Duration::from_millis(118),
            coefficient_of_variation: 0.1,
            confidence_interval: (Duration::from_millis(95), Duration::from_millis(105)),
            is_stable: true,
            outliers: 0,
            throughput: 10.0,
        }
    }
}

impl Default for MemoryProfile {
    fn default() -> Self {
        Self {
            peak_memory_mb: 100.0,
            average_memory_mb: 80.0,
            allocation_rate: 10.0,
            deallocation_rate: 9.5,
            gc_events: 0,
            efficiency_score: 85.0,
            potential_leak: false,
        }
    }
}

impl Default for QualityMetrics {
    fn default() -> Self {
        Self {
            silhouette_score: Some(0.5),
            calinski_harabasz: Some(100.0),
            davies_bouldin: Some(1.0),
            inertia: Some(50.0),
            n_clusters: 3,
            convergence_iterations: Some(10),
        }
    }
}