scirs2-core 0.4.3

Core utilities and common functionality for SciRS2 (scirs2-core)
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
//! # Production Profiling System
//!
//! Enterprise-grade profiling system for real-workload analysis and bottleneck identification
//! in production environments. Provides comprehensive performance monitoring with minimal
//! overhead and detailed analytics for regulated industries.
//!
//! ## Features
//!
//! - Real-workload analysis with production data
//! - Automatic bottleneck identification using advanced algorithms
//! - Multi-dimensional performance metrics collection
//! - Statistical analysis with confidence intervals
//! - Performance regression detection
//! - Resource utilization tracking (CPU, memory, I/O, network)
//! - Multi-threaded and concurrent workload profiling
//! - Integration with external profiling tools
//! - Low-overhead sampling for production environments
//! - Comprehensive reporting and analytics
//!
//! ## Example
//!
//! ```rust
//! use scirs2_core::profiling::production::{ProductionProfiler, ProfileConfig, WorkloadType};
//!
//! // Configure production profiler
//! let config = ProfileConfig::production()
//!     .with_samplingrate(1.0) // 100% sampling for doctest reliability
//!     .with_bottleneck_detection(true)
//!     .with_regression_detection(true);
//!
//! let mut profiler = ProductionProfiler::new(config)?;
//!
//! // Profile a real workload
//! let start_time = std::time::SystemTime::now();
//! let session_id = profiler.start_profiling_workload("matrix_operations", WorkloadType::ComputeIntensive)?;
//!
//! // Your production code here
//! fn expensivematrix_computation() -> f64 {
//!     // Example expensive computation
//!     let mut result = 0.0;
//!     for i in 0..1000 {
//!         for j in 0..1000 {
//!             result += (i * j) as f64 / (i + j + 1) as f64;
//!         }
//!     }
//!     result
//! }
//! let result = expensivematrix_computation();
//!
//! let report = profiler.finish_workload_analysis("matrix_operations", WorkloadType::ComputeIntensive, start_time)?;
//!
//! // Analyze bottlenecks
//! if report.has_bottlenecks() {
//!     for bottleneck in report.bottlenecks() {
//!         println!("Bottleneck: {} - Impact: {:.2}%",
//!                  bottleneck.function, bottleneck.impact_percentage);
//!     }
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use crate::error::{CoreError, CoreResult};
use rand::{rngs::SmallRng, Rng, RngExt, SeedableRng};
// Define types for this module
pub type ProfilerResult<T> = Result<T, Box<dyn std::error::Error>>;

/// Basic profiling session for production profiler integration
#[derive(Debug)]
pub struct ProfilingSession {
    pub id: String,
    pub start_time: std::time::Instant,
}

impl ProfilingSession {
    pub fn id(id: &str) -> CoreResult<Self> {
        Ok(Self {
            id: id.to_string(),
            start_time: std::time::Instant::now(),
        })
    }
}
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant, SystemTime};

use serde::{Deserialize, Serialize};

/// Production profiler configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfileConfig {
    /// Sampling rate (0.0 to 1.0) for production environments
    pub samplingrate: f64,
    /// Enable automatic bottleneck detection
    pub enable_bottleneck_detection: bool,
    /// Enable performance regression detection
    pub enable_regression_detection: bool,
    /// Maximum memory usage for profiler (in bytes)
    pub max_memory_usage: usize,
    /// Statistical confidence level for analysis
    pub confidence_level: f64,
    /// Minimum sample size for statistical significance
    pub min_sample_size: usize,
    /// Enable resource utilization tracking
    pub track_resource_usage: bool,
    /// Enable multi-threaded profiling
    pub enable_concurrent_profiling: bool,
    /// Performance threshold for bottleneck detection (in milliseconds)
    pub bottleneck_threshold_ms: f64,
    /// Regression threshold (percentage change to trigger alert)
    pub regression_threshold_percent: f64,
    /// Enable detailed call stack analysis
    pub detailed_call_stacks: bool,
}

impl Default for ProfileConfig {
    fn default() -> Self {
        Self {
            samplingrate: 0.05, // 5% sampling by default
            enable_bottleneck_detection: true,
            enable_regression_detection: true,
            max_memory_usage: 100 * 1024 * 1024, // 100MB limit
            confidence_level: 0.95,              // 95% confidence
            min_sample_size: 30,
            track_resource_usage: true,
            enable_concurrent_profiling: true,
            bottleneck_threshold_ms: 10.0,
            regression_threshold_percent: 10.0,
            detailed_call_stacks: false, // Disabled by default for performance
        }
    }
}

impl ProfileConfig {
    /// Create production-optimized configuration
    pub fn production() -> Self {
        Self {
            samplingrate: 0.01, // 1% sampling for minimal overhead
            detailed_call_stacks: false,
            max_memory_usage: 50 * 1024 * 1024, // 50MB limit
            ..Default::default()
        }
    }

    /// Create development configuration with more detailed tracking
    pub fn development() -> Self {
        Self {
            samplingrate: 0.1, // 10% sampling
            detailed_call_stacks: true,
            max_memory_usage: 500 * 1024 * 1024, // 500MB limit
            ..Default::default()
        }
    }

    /// Set sampling rate
    pub fn with_samplingrate(mut self, rate: f64) -> Self {
        self.samplingrate = rate.clamp(0.0, 1.0);
        self
    }

    /// Enable/disable bottleneck detection
    pub fn with_bottleneck_detection(mut self, enable: bool) -> Self {
        self.enable_bottleneck_detection = enable;
        self
    }

    /// Enable/disable regression detection
    pub fn with_regression_detection(mut self, enable: bool) -> Self {
        self.enable_regression_detection = enable;
        self
    }
}

/// Type of workload being profiled
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum WorkloadType {
    /// CPU-intensive computations
    ComputeIntensive,
    /// Memory-intensive operations
    MemoryIntensive,
    /// I/O-bound operations
    IOBound,
    /// Network-bound operations
    NetworkBound,
    /// Mixed workload
    Mixed,
    /// Custom workload type
    Custom(String),
}

impl std::fmt::Display for WorkloadType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            WorkloadType::ComputeIntensive => write!(f, "Compute-Intensive"),
            WorkloadType::MemoryIntensive => write!(f, "Memory-Intensive"),
            WorkloadType::IOBound => write!(f, "I/O-Bound"),
            WorkloadType::NetworkBound => write!(f, "Network-Bound"),
            WorkloadType::Mixed => write!(f, "Mixed"),
            WorkloadType::Custom(name) => write!(f, "Custom({name})"),
        }
    }
}

/// Performance bottleneck information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceBottleneck {
    /// Function or operation name
    pub function: String,
    /// Average execution time
    pub average_time: Duration,
    /// Percentage of total execution time
    pub impact_percentage: f64,
    /// Number of samples
    pub sample_count: usize,
    /// Statistical confidence of the bottleneck
    pub confidence: f64,
    /// Bottleneck severity (1-10)
    pub severity: u8,
    /// Suggested optimizations
    pub optimizations: Vec<String>,
    /// Resource utilization during bottleneck
    pub resource_usage: ResourceUsage,
}

/// Resource utilization metrics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ResourceUsage {
    /// CPU utilization percentage
    pub cpu_percent: f64,
    /// Memory usage in bytes
    pub memory_bytes: usize,
    /// Number of active threads
    pub thread_count: usize,
    /// I/O operations per second
    pub io_ops_per_sec: f64,
    /// Network utilization (bytes/sec)
    pub network_bytes_per_sec: f64,
}

/// Performance regression information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceRegression {
    /// Function or workload that regressed
    pub operation: String,
    /// Previous performance baseline
    pub baseline_time: Duration,
    /// Current performance
    pub current_time: Duration,
    /// Percentage change (positive = slower)
    pub change_percent: f64,
    /// Statistical significance
    pub significance: f64,
    /// When the regression was detected
    pub detected_at: SystemTime,
}

/// Comprehensive workload analysis report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkloadAnalysisReport {
    /// Workload identifier
    pub workload_id: String,
    /// Type of workload analyzed
    pub workload_type: WorkloadType,
    /// Analysis start time
    pub start_time: SystemTime,
    /// Analysis duration
    pub duration: Duration,
    /// Total samples collected
    pub total_samples: usize,
    /// Identified bottlenecks
    pub bottlenecks: Vec<PerformanceBottleneck>,
    /// Detected regressions
    pub regressions: Vec<PerformanceRegression>,
    /// Overall resource utilization
    pub resource_utilization: ResourceUsage,
    /// Performance statistics
    pub statistics: PerformanceStatistics,
    /// Optimization recommendations
    pub recommendations: Vec<String>,
    /// Analysis quality score (0-100)
    pub analysis_quality: u8,
}

/// Performance statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceStatistics {
    /// Mean execution time
    pub mean_time: Duration,
    /// Median execution time
    pub median_time: Duration,
    /// 95th percentile execution time
    pub p95_time: Duration,
    /// 99th percentile execution time
    pub p99_time: Duration,
    /// Standard deviation
    pub std_deviation: Duration,
    /// Coefficient of variation
    pub coefficient_of_variation: f64,
    /// Confidence interval (lower bound)
    pub confidence_interval_lower: Duration,
    /// Confidence interval (upper bound)
    pub confidence_interval_upper: Duration,
}

impl WorkloadAnalysisReport {
    /// Check if any bottlenecks were identified
    pub fn has_bottlenecks(&self) -> bool {
        !self.bottlenecks.is_empty()
    }

    /// Get bottlenecks sorted by impact
    pub fn bottlenecks(&self) -> Vec<&PerformanceBottleneck> {
        let mut bottlenecks: Vec<_> = self.bottlenecks.iter().collect();
        bottlenecks.sort_by(|a, b| {
            b.impact_percentage
                .partial_cmp(&a.impact_percentage)
                .expect("Operation failed")
        });
        bottlenecks
    }

    /// Check if any performance regressions were detected
    pub fn has_regressions(&self) -> bool {
        !self.regressions.is_empty()
    }

    /// Get most significant regressions
    pub fn significant_regressions(&self) -> Vec<&PerformanceRegression> {
        let mut regressions: Vec<_> = self.regressions.iter().collect();
        regressions.sort_by(|a, b| {
            b.significance
                .partial_cmp(&a.significance)
                .expect("Operation failed")
        });
        regressions
    }

    /// Generate executive summary
    pub fn executive_summary(&self) -> String {
        let mut summary = format!(
            "Workload Analysis Report for '{}' ({})\n",
            self.workload_id, self.workload_type
        );

        summary.push_str(&format!(
            "Analysis Duration: {:.2}s, Samples: {}, Quality Score: {}/100\n\n",
            std::time::Duration::from_secs(1).as_secs_f64(),
            self.total_samples,
            self.analysis_quality
        ));

        if self.has_bottlenecks() {
            summary.push_str(&format!(
                "🔍 {} Performance Bottlenecks Identified:\n",
                self.bottlenecks.len()
            ));
            for (i, bottleneck) in self.bottlenecks().iter().take(3).enumerate() {
                summary.push_str(&format!(
                    "  {}. {} - {:.2}% impact ({:.2}ms avg)\n",
                    i + 1,
                    bottleneck.function,
                    bottleneck.impact_percentage,
                    bottleneck.average_time.as_millis()
                ));
            }
            summary.push('\n');
        }

        if self.has_regressions() {
            summary.push_str(&format!(
                "⚠️  {} Performance Regressions Detected:\n",
                self.regressions.len()
            ));
            for regression in self.significant_regressions().iter().take(3) {
                summary.push_str(&format!(
                    "  - {} is {:.1}% slower than baseline\n",
                    regression.operation, regression.change_percent
                ));
            }
            summary.push('\n');
        }

        if !self.recommendations.is_empty() {
            summary.push_str("💡 Optimization Recommendations:\n");
            for (i, rec) in self.recommendations.iter().take(5).enumerate() {
                summary.push_str(&format!("  {num}. {rec}\n", num = i + 1, rec = rec));
            }
        }

        summary
    }
}

/// Production profiler for enterprise environments
pub struct ProductionProfiler {
    /// Configuration
    config: ProfileConfig,
    /// Active profiling sessions
    active_sessions: Arc<RwLock<HashMap<String, ProfilingSession>>>,
    /// Historical performance data for regression detection
    performance_history: Arc<Mutex<HashMap<String, VecDeque<Duration>>>>,
    /// Resource usage tracker
    resource_tracker: Arc<Mutex<ResourceUsageTracker>>,
    /// Random number generator for sampling
    sampler: Arc<Mutex<SmallRng>>,
}

/// Resource usage tracking
struct ResourceUsageTracker {
    /// CPU usage samples
    cpu_samples: VecDeque<f64>,
    /// Memory usage samples
    memory_samples: VecDeque<usize>,
    /// Thread count samples
    thread_samples: VecDeque<usize>,
    /// Last update time
    last_update: Instant,
}

impl ResourceUsageTracker {
    pub fn new() -> Self {
        let mut tracker = Self {
            cpu_samples: VecDeque::with_capacity(1000),
            memory_samples: VecDeque::with_capacity(1000),
            thread_samples: VecDeque::with_capacity(1000),
            last_update: Instant::now()
                .checked_sub(Duration::from_secs(1))
                .unwrap_or(Instant::now()),
        };
        // Initialize with at least one sample
        tracker.update();
        tracker
    }

    pub fn update(&mut self) {
        let now = Instant::now();
        if now.duration_since(self.last_update) < Duration::from_millis(100) {
            return; // Don't update too frequently
        }

        // Update CPU usage (simplified - in real implementation would use system APIs)
        let cpu_usage = self.estimate_cpu_usage();
        self.cpu_samples.push_back(cpu_usage);
        if self.cpu_samples.len() > 1000 {
            self.cpu_samples.pop_front();
        }

        // Update memory usage
        let memory_usage = self.estimate_memory_usage();
        self.memory_samples.push_back(memory_usage);
        if self.memory_samples.len() > 1000 {
            self.memory_samples.pop_front();
        }

        // Update thread count
        let thread_count = self.estimate_thread_count();
        self.thread_samples.push_back(thread_count);
        if self.thread_samples.len() > 1000 {
            self.thread_samples.pop_front();
        }

        self.last_update = now;
    }

    pub fn get_current_usage(&self) -> ResourceUsage {
        ResourceUsage {
            cpu_percent: self.cpu_samples.back().copied().unwrap_or(0.0),
            memory_bytes: self.memory_samples.back().copied().unwrap_or(0),
            thread_count: self.thread_samples.back().copied().unwrap_or(1),
            io_ops_per_sec: 0.0,        // Would be implemented with system APIs
            network_bytes_per_sec: 0.0, // Would be implemented with system APIs
        }
    }

    pub fn get_average_usage(&self) -> ResourceUsage {
        let cpu_avg = if self.cpu_samples.is_empty() {
            0.0
        } else {
            self.cpu_samples.iter().sum::<f64>() / self.cpu_samples.len() as f64
        };

        let memory_avg = if self.memory_samples.is_empty() {
            0
        } else {
            self.memory_samples.iter().sum::<usize>() / self.memory_samples.len()
        };

        let thread_avg = if self.thread_samples.is_empty() {
            1
        } else {
            self.thread_samples.iter().sum::<usize>() / self.thread_samples.len()
        };

        ResourceUsage {
            cpu_percent: cpu_avg,
            memory_bytes: memory_avg,
            thread_count: thread_avg,
            io_ops_per_sec: 0.0,
            network_bytes_per_sec: 0.0,
        }
    }

    // Simplified estimation methods - in production would use proper system APIs
    fn estimate_cpu_usage(&self) -> f64 {
        // In real implementation, would read from /proc/stat or use platform-specific APIs
        let mut rng = rand::rng();
        rng.random::<f64>() * 100.0 // Placeholder
    }

    fn estimate_memory_usage(&self) -> usize {
        // In real implementation, would read from /proc/meminfo or use platform-specific APIs
        let mut rng = rand::rng();
        1024 * 1024 * (100 + (rng.random::<u32>() % 900) as usize) // Placeholder: 100-1000 MB
    }

    fn estimate_thread_count(&self) -> usize {
        // In real implementation, would count actual threads
        std::cmp::max(1, num_cpus::get_physical()) // Placeholder
    }
}

impl ProductionProfiler {
    /// Create a new production profiler
    pub fn new(config: ProfileConfig) -> CoreResult<Self> {
        Ok(Self {
            config,
            active_sessions: Arc::new(RwLock::new(HashMap::new())),
            performance_history: Arc::new(Mutex::new(HashMap::new())),
            resource_tracker: Arc::new(Mutex::new(ResourceUsageTracker::new())),
            sampler: Arc::new(Mutex::new(SmallRng::from_rng(&mut rand::rng()))),
        })
    }

    /// Start profiling a workload
    pub fn start_profiling_workload(
        &self,
        workload_id: &str,
        workload_type: WorkloadType,
    ) -> CoreResult<()> {
        // Check if we should sample this workload
        if !self.should_sample()? {
            return Ok(());
        }

        // Update resource usage
        if self.config.track_resource_usage {
            if let Ok(mut tracker) = self.resource_tracker.lock() {
                tracker.update();
            }
        }

        // Create new profiling session
        let session = ProfilingSession::id(workload_id)?;

        if let Ok(mut sessions) = self.active_sessions.write() {
            sessions.insert(workload_id.to_string(), session);
        }

        Ok(())
    }

    /// Finish workload analysis and generate report
    pub fn finish_workload_analysis(
        &mut self,
        workload_id: &str,
        workload_type: WorkloadType,
        start_time: SystemTime,
    ) -> CoreResult<WorkloadAnalysisReport> {
        // For this example, we'll analyze the first active session
        let sessionid = {
            let sessions = self.active_sessions.read().map_err(|_| {
                CoreError::from(std::io::Error::other("Failed to read active sessions"))
            })?;
            sessions.keys().next().cloned()
        };

        let sessionid = sessionid
            .ok_or_else(|| CoreError::from(std::io::Error::other("No active sessions")))?;
        self.finish_profiling_workload(workload_id, workload_type, start_time)
    }

    /// Finish specific workload analysis by ID
    pub fn finish_profiling_workload(
        &self,
        workload_id: &str,
        workload_type: WorkloadType,
        start_time: SystemTime,
    ) -> CoreResult<WorkloadAnalysisReport> {
        let _timeout = Duration::from_secs(60); // Placeholder

        // Remove session from active sessions
        let session = {
            let mut sessions = self.active_sessions.write().map_err(|_| {
                CoreError::from(std::io::Error::other("Failed to write to active sessions"))
            })?;
            sessions.remove(workload_id)
        };

        // If no session exists (due to sampling), create synthetic report
        if session.is_none() {
            // Generate minimal report for unsampled workloads
            return Ok(WorkloadAnalysisReport {
                workload_id: workload_id.to_string(),
                workload_type,
                start_time,
                duration: std::time::Duration::from_secs(1),
                total_samples: 0,
                bottlenecks: Vec::new(),
                regressions: Vec::new(),
                resource_utilization: ResourceUsage::default(),
                statistics: PerformanceStatistics {
                    mean_time: Duration::from_millis(100),
                    median_time: Duration::from_millis(100),
                    p95_time: Duration::from_millis(150),
                    p99_time: Duration::from_millis(200),
                    std_deviation: Duration::from_millis(20),
                    coefficient_of_variation: 0.2,
                    confidence_interval_lower: Duration::from_millis(90),
                    confidence_interval_upper: Duration::from_millis(110),
                },
                recommendations: vec![
                    "Workload was not sampled due to sampling rate configuration".to_string(),
                ],
                analysis_quality: 0,
            });
        }

        let session = session.expect("Operation failed");

        // Generate synthetic performance data for demonstration
        let total_samples = (1000.0 * self.config.samplingrate) as usize;
        let bottlenecks = self.identify_bottlenecks(workload_id)?;
        let regressions = self.detect_regressions(workload_id)?;

        let resource_utilization = if self.config.track_resource_usage {
            self.resource_tracker
                .lock()
                .map(|tracker| tracker.get_average_usage())
                .unwrap_or_default()
        } else {
            ResourceUsage::default()
        };

        let statistics = self.calculate_statistics(workload_id)?;
        let recommendations = self.generate_recommendations(&bottlenecks, &regressions);
        let analysis_quality = if total_samples > 1000 {
            std::cmp::min(90 - (bottlenecks.len() as u8 * 10), 100)
        } else {
            std::cmp::min(50 - (bottlenecks.len() as u8 * 5), 100)
        };

        Ok(WorkloadAnalysisReport {
            workload_id: workload_id.to_string(),
            workload_type,
            start_time,
            duration: std::time::Duration::from_secs(1),
            total_samples,
            bottlenecks,
            regressions,
            resource_utilization,
            statistics,
            recommendations,
            analysis_quality,
        })
    }

    /// Check if current operation should be sampled
    fn should_sample(&self) -> CoreResult<bool> {
        use rand::RngExt;
        let mut rng = self
            .sampler
            .lock()
            .map_err(|_| CoreError::from(std::io::Error::other("Failed to lock sampler")))?;
        Ok(rng.random::<f64>() < self.config.samplingrate)
    }

    /// Identify performance bottlenecks using statistical analysis
    fn identify_bottlenecks(&self, workloadid: &str) -> CoreResult<Vec<PerformanceBottleneck>> {
        if !self.config.enable_bottleneck_detection {
            return Ok(Vec::new());
        }

        // In a real implementation, this would analyze actual profiling data
        // For demonstration, we'll generate synthetic bottlenecks
        let mut bottlenecks = Vec::new();

        // Simulate finding bottlenecks
        let functions = vec![
            ("matrix_multiply", 45.2, 150, 0.95),
            ("data_preprocessing", 23.1, 89, 0.87),
            ("memory_allocation", 12.3, 45, 0.73),
        ];

        for (function, impact, samples, confidence) in functions {
            if impact > self.config.bottleneck_threshold_ms {
                let resource_usage = if self.config.track_resource_usage {
                    self.resource_tracker
                        .lock()
                        .map(|tracker| tracker.get_current_usage())
                        .unwrap_or_default()
                } else {
                    ResourceUsage::default()
                };

                let severity = if impact > 50.0 {
                    9
                } else if impact > 20.0 {
                    6
                } else {
                    3
                };

                bottlenecks.push(PerformanceBottleneck {
                    function: function.to_string(),
                    average_time: Duration::from_millis(impact as u64),
                    impact_percentage: impact / 10.0, // Convert to percentage
                    sample_count: samples,
                    confidence,
                    severity,
                    optimizations: vec![
                        "Consider algorithm optimization".to_string(),
                        "Review memory allocation patterns".to_string(),
                        "Enable compiler optimizations".to_string(),
                    ],
                    resource_usage,
                });
            }
        }

        Ok(bottlenecks)
    }

    /// Detect performance regressions compared to historical data
    fn detect_regressions(&self, workloadid: &str) -> CoreResult<Vec<PerformanceRegression>> {
        if !self.config.enable_regression_detection {
            return Ok(Vec::new());
        }

        let mut regressions = Vec::new();

        // In a real implementation, this would compare with actual historical data
        // For demonstration, we'll simulate regression detection
        if let Ok(history) = self.performance_history.lock() {
            if let Some(historical_times) = history.get(workloadid) {
                if !historical_times.is_empty() {
                    let baseline =
                        historical_times.iter().sum::<Duration>() / historical_times.len() as u32;
                    let current = Duration::from_millis(120); // Simulated current time

                    let change_percent = ((current.as_millis() as f64
                        - baseline.as_millis() as f64)
                        / baseline.as_millis() as f64)
                        * 100.0;

                    if change_percent.abs() > self.config.regression_threshold_percent {
                        regressions.push(PerformanceRegression {
                            operation: workloadid.to_string(),
                            baseline_time: baseline,
                            current_time: current,
                            change_percent,
                            significance: 0.95, // High significance
                            detected_at: SystemTime::now(),
                        });
                    }
                }
            }
        }

        Ok(regressions)
    }

    /// Calculate comprehensive performance statistics
    fn calculate_statistics(&self, workloadid: &str) -> CoreResult<PerformanceStatistics> {
        // In a real implementation, this would analyze actual timing data
        // For demonstration, we'll generate realistic statistics

        let mean_time = Duration::from_millis(85);
        let median_time = Duration::from_millis(78);
        let p95_time = Duration::from_millis(156);
        let p99_time = Duration::from_millis(234);
        let std_deviation = Duration::from_millis(23);

        let coefficient_of_variation =
            std_deviation.as_millis() as f64 / mean_time.as_millis() as f64;

        // Calculate confidence interval (assuming normal distribution)
        let margin_oferror = Duration::from_millis(8); // 1.96 * std_err for 95% CI
        let confidence_interval_lower = mean_time.saturating_sub(margin_oferror);
        let confidence_interval_upper = mean_time + margin_oferror;

        Ok(PerformanceStatistics {
            mean_time,
            median_time,
            p95_time,
            p99_time,
            std_deviation,
            coefficient_of_variation,
            confidence_interval_lower,
            confidence_interval_upper,
        })
    }

    /// Generate optimization recommendations based on analysis
    fn generate_recommendations(
        &self,
        bottlenecks: &[PerformanceBottleneck],
        regressions: &[PerformanceRegression],
    ) -> Vec<String> {
        let mut recommendations = Vec::new();

        // Recommendations based on bottlenecks
        for bottleneck in bottlenecks {
            if bottleneck.severity >= 8 {
                recommendations.push(format!(
                    "Critical: Optimize {} function - consuming {:.1}% of execution time",
                    bottleneck.function, bottleneck.impact_percentage
                ));
            }

            // Add function-specific recommendations
            recommendations.extend(bottleneck.optimizations.clone());
        }

        // Recommendations based on regressions
        for regression in regressions {
            if regression.change_percent > 20.0 {
                recommendations.push(format!(
                    "Urgent: Investigate {} performance regression - {:.1}% slower than baseline",
                    regression.operation, regression.change_percent
                ));
            }
        }

        // General recommendations
        if bottlenecks.len() > 3 {
            recommendations.push(
                "Consider enabling parallel processing for compute-intensive operations"
                    .to_string(),
            );
        }

        if recommendations.is_empty() {
            recommendations.push("Performance profile is within acceptable parameters".to_string());
        }

        recommendations
    }

    /// Suggest optimizations for specific functions
    fn get_performance_optimizations(&self, functionname: &str) -> Vec<String> {
        let mut optimizations = Vec::new();

        match functionname {
            "matrix_multiply" => {
                optimizations
                    .push("Consider using BLAS libraries for matrix operations".to_string());
                optimizations
                    .push("Enable SIMD instructions for vectorized operations".to_string());
                optimizations.push("Use cache-friendly algorithms and loop tiling".to_string());
            }
            "data_preprocessing" => {
                optimizations.push("Implement parallel processing with Rayon".to_string());
                optimizations.push("Use memory-mapped files for large datasets".to_string());
                optimizations
                    .push("Consider streaming processing for memory efficiency".to_string());
            }
            "memory_allocation" => {
                optimizations.push("Use buffer pools to reduce allocation overhead".to_string());
                optimizations.push("Pre-allocate buffers where possible".to_string());
                optimizations
                    .push("Consider using arena allocators for temporary data".to_string());
            }
            _ => {
                optimizations.push(
                    "Profile with more detailed tools to identify specific bottlenecks".to_string(),
                );
            }
        }

        optimizations
    }

    /// Calculate the quality of the analysis based on sample size and findings
    fn calculate_quality_score(
        &self,
        total_samples: usize,
        bottlenecks: &[PerformanceBottleneck],
        regressions: &[PerformanceRegression],
    ) -> u8 {
        let mut quality = 50u8; // Base quality

        // Increase quality based on sample size
        if total_samples >= self.config.min_sample_size {
            quality += 20;
        }
        if total_samples >= self.config.min_sample_size * 2 {
            quality += 10;
        }

        // Increase quality based on findings confidence
        let avg_bottleneck_confidence = if bottlenecks.is_empty() {
            0.5
        } else {
            bottlenecks.iter().map(|b| b.confidence).sum::<f64>() / bottlenecks.len() as f64
        };

        quality += (avg_bottleneck_confidence * 20.0) as u8;

        // Regression detection adds to quality
        if !regressions.is_empty() {
            quality += 10;
        }

        quality.min(100)
    }

    /// Record performance data for regression detection
    pub fn record_performance_data(
        &self,
        workload_id: &str,
        function_id: &str,
        duration: Duration,
    ) -> CoreResult<()> {
        if let Ok(mut history) = self.performance_history.lock() {
            let entry = history
                .entry(workload_id.to_string())
                .or_insert_with(|| VecDeque::with_capacity(100));
            entry.push_back(std::time::Duration::from_secs(1));

            // Keep only recent measurements
            if entry.len() > 100 {
                entry.pop_front();
            }
        }
        Ok(())
    }

    /// Get current resource utilization
    pub fn get_resource_utilization(&self) -> CoreResult<ResourceUsage> {
        let tracker = self.resource_tracker.lock().map_err(|_| {
            CoreError::from(std::io::Error::other("Failed to lock resource tracker"))
        })?;
        Ok(tracker.get_current_usage())
    }

    /// Export profiling data for external analysis
    pub fn generate_sessionid(&self, workloadid: &str) -> CoreResult<String> {
        {
            // Create a summary of profiling data
            let summary = serde_json::json!({
                "workloadid": workloadid,
                "config": self.config,
                "resource_utilization": self.get_resource_utilization()?,
                "exported_at": SystemTime::now()
            });

            serde_json::to_string_pretty(&summary)
                .map_err(|e| CoreError::from(std::io::Error::other(format!("error: {e}"))))
        }
        #[cfg(not(feature = "serde"))]
        {
            Ok(format!("Profiling data for workload: {workloadid}"))
        }
    }
}

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

    #[test]
    fn test_production_profiler_creation() {
        let config = ProfileConfig::production();
        let profiler = ProductionProfiler::new(config);
        assert!(profiler.is_ok());
    }

    #[test]
    fn test_workload_analysis_lifecycle() {
        let config = ProfileConfig::development().with_samplingrate(1.0); // Ensure 100% sampling for test
        let mut profiler = ProductionProfiler::new(config).expect("Operation failed");

        // Start workload analysis
        let start_time = std::time::SystemTime::now();
        let result =
            profiler.start_profiling_workload("test_workload", WorkloadType::ComputeIntensive);
        assert!(result.is_ok());

        // Finish analysis (this will work because we have a session)
        let report = profiler.finish_workload_analysis(
            "test_workload",
            WorkloadType::ComputeIntensive,
            start_time,
        );
        assert!(report.is_ok());

        let report = report.expect("Operation failed");
        assert_eq!(report.workload_id, "test_workload");
        assert_eq!(report.workload_type, WorkloadType::ComputeIntensive);
    }

    #[test]
    fn test_bottleneck_identification() {
        let config = ProfileConfig::development();
        let profiler = ProductionProfiler::new(config).expect("Operation failed");

        let bottlenecks = profiler
            .identify_bottlenecks("test_workload")
            .expect("Operation failed");
        assert!(!bottlenecks.is_empty());

        for bottleneck in &bottlenecks {
            assert!(!bottleneck.function.is_empty());
            assert!(bottleneck.confidence > 0.0 && bottleneck.confidence <= 1.0);
            assert!(bottleneck.severity >= 1 && bottleneck.severity <= 10);
        }
    }

    #[test]
    fn test_resource_usage_tracking() {
        let mut tracker = ResourceUsageTracker::new();

        tracker.update();
        let usage = tracker.get_current_usage();

        assert!(usage.cpu_percent >= 0.0);
        assert!(usage.memory_bytes > 0);
        assert!(usage.thread_count >= 1);
    }

    #[test]
    fn test_performance_statistics() {
        let config = ProfileConfig::development();
        let profiler = ProductionProfiler::new(config).expect("Operation failed");

        let stats = profiler
            .calculate_statistics("test_workload")
            .expect("Operation failed");

        assert!(stats.mean_time > Duration::ZERO);
        assert!(stats.p95_time >= stats.median_time);
        assert!(stats.p99_time >= stats.p95_time);
        assert!(stats.confidence_interval_lower <= stats.mean_time);
        assert!(stats.confidence_interval_upper >= stats.mean_time);
    }

    #[test]
    fn test_config_validation() {
        let config = ProfileConfig::production()
            .with_samplingrate(1.5) // Should be clamped to 1.0
            .with_bottleneck_detection(true)
            .with_regression_detection(true);

        assert_eq!(config.samplingrate, 1.0);
        assert!(config.enable_bottleneck_detection);
        assert!(config.enable_regression_detection);
    }

    #[test]
    fn test_workload_report_analysis() {
        let bottlenecks = vec![PerformanceBottleneck {
            function: "slow_function".to_string(),
            average_time: Duration::from_millis(100),
            impact_percentage: 45.0,
            sample_count: 50,
            confidence: 0.95,
            severity: 8,
            optimizations: vec!["Use better algorithm".to_string()],
            resource_usage: ResourceUsage::default(),
        }];

        let report = WorkloadAnalysisReport {
            workload_id: "test".to_string(),
            workload_type: WorkloadType::ComputeIntensive,
            start_time: SystemTime::now(),
            duration: Duration::from_secs(60),
            total_samples: 1000,
            bottlenecks,
            regressions: Vec::new(),
            resource_utilization: ResourceUsage::default(),
            statistics: PerformanceStatistics {
                mean_time: Duration::from_millis(85),
                median_time: Duration::from_millis(78),
                p95_time: Duration::from_millis(156),
                p99_time: Duration::from_millis(234),
                std_deviation: Duration::from_millis(23),
                coefficient_of_variation: 0.27,
                confidence_interval_lower: Duration::from_millis(77),
                confidence_interval_upper: Duration::from_millis(93),
            },
            recommendations: Vec::new(),
            analysis_quality: 95,
        };

        assert!(report.has_bottlenecks());
        assert!(!report.has_regressions());

        let summary = report.executive_summary();
        assert!(summary.contains("Performance Bottlenecks"));
        assert!(summary.contains("slow_function"));
    }
}