sklears-utils 0.1.1

Shared utilities for sklears: validation, data generation, helpers
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
//! Performance measurement and profiling utilities
//!
//! This module provides utilities for measuring performance, memory usage,
//! and timing operations in machine learning workflows.

use crate::{UtilsError, UtilsResult};
use std::collections::HashMap;
use std::time::{Duration, Instant};

/// Timer for measuring execution time
#[derive(Debug, Clone)]
pub struct Timer {
    start: Option<Instant>,
    measurements: HashMap<String, Duration>,
}

impl Timer {
    /// Create a new timer
    pub fn new() -> Self {
        Self {
            start: None,
            measurements: HashMap::new(),
        }
    }

    /// Start timing
    pub fn start(&mut self) {
        self.start = Some(Instant::now());
    }

    /// Stop timing and return elapsed duration
    pub fn stop(&mut self) -> UtilsResult<Duration> {
        let start = self
            .start
            .take()
            .ok_or_else(|| UtilsError::InvalidParameter("Timer not started".to_string()))?;
        Ok(start.elapsed())
    }

    /// Stop timing and store result with a label
    pub fn stop_and_store(&mut self, label: String) -> UtilsResult<Duration> {
        let duration = self.stop()?;
        self.measurements.insert(label, duration);
        Ok(duration)
    }

    /// Time a closure and return the result and duration
    pub fn time<F, R>(&mut self, f: F) -> (R, Duration)
    where
        F: FnOnce() -> R,
    {
        let start = Instant::now();
        let result = f();
        let duration = start.elapsed();
        (result, duration)
    }

    /// Time a closure, store the result with a label, and return the result
    pub fn time_and_store<F, R>(&mut self, label: String, f: F) -> R
    where
        F: FnOnce() -> R,
    {
        let (result, duration) = self.time(f);
        self.measurements.insert(label, duration);
        result
    }

    /// Get all measurements
    pub fn measurements(&self) -> &HashMap<String, Duration> {
        &self.measurements
    }

    /// Get a specific measurement
    pub fn get_measurement(&self, label: &str) -> Option<Duration> {
        self.measurements.get(label).copied()
    }

    /// Clear all measurements
    pub fn clear(&mut self) {
        self.measurements.clear();
    }

    /// Get summary statistics
    pub fn summary(&self) -> TimerSummary {
        let mut total = Duration::from_secs(0);
        let mut min = Duration::from_secs(u64::MAX);
        let mut max = Duration::from_secs(0);

        for &duration in self.measurements.values() {
            total += duration;
            min = min.min(duration);
            max = max.max(duration);
        }

        let count = self.measurements.len();
        let average = if count > 0 {
            total / count as u32
        } else {
            Duration::from_secs(0)
        };

        TimerSummary {
            count,
            total,
            average,
            min: if min == Duration::from_secs(u64::MAX) {
                Duration::from_secs(0)
            } else {
                min
            },
            max,
        }
    }
}

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

/// Summary statistics for timer measurements
#[derive(Debug, Clone, PartialEq)]
pub struct TimerSummary {
    pub count: usize,
    pub total: Duration,
    pub average: Duration,
    pub min: Duration,
    pub max: Duration,
}

/// Memory usage tracker
#[derive(Debug, Clone)]
pub struct MemoryTracker {
    baseline: Option<usize>,
    measurements: HashMap<String, usize>,
}

impl MemoryTracker {
    /// Create a new memory tracker
    pub fn new() -> Self {
        Self {
            baseline: None,
            measurements: HashMap::new(),
        }
    }

    /// Set baseline memory usage
    pub fn set_baseline(&mut self) {
        if let Ok(usage) = get_memory_usage() {
            self.baseline = Some(usage);
        }
    }

    /// Record current memory usage with a label
    pub fn record(&mut self, label: String) -> UtilsResult<usize> {
        let usage = get_memory_usage()?;
        self.measurements.insert(label, usage);
        Ok(usage)
    }

    /// Get memory usage relative to baseline
    pub fn relative_usage(&self) -> UtilsResult<Option<isize>> {
        if let Some(baseline) = self.baseline {
            let current = get_memory_usage()?;
            Ok(Some(current as isize - baseline as isize))
        } else {
            Ok(None)
        }
    }

    /// Get all measurements
    pub fn measurements(&self) -> &HashMap<String, usize> {
        &self.measurements
    }

    /// Get peak memory usage
    pub fn peak_usage(&self) -> Option<usize> {
        self.measurements.values().max().copied()
    }

    /// Clear all measurements
    pub fn clear(&mut self) {
        self.measurements.clear();
        self.baseline = None;
    }
}

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

/// Performance profiler combining timing and memory tracking
#[derive(Debug, Clone)]
pub struct Profiler {
    timer: Timer,
    memory: MemoryTracker,
    active_sessions: HashMap<String, (Instant, usize)>,
}

impl Profiler {
    /// Create a new profiler
    pub fn new() -> Self {
        Self {
            timer: Timer::new(),
            memory: MemoryTracker::new(),
            active_sessions: HashMap::new(),
        }
    }

    /// Start a profiling session
    pub fn start_session(&mut self, name: String) -> UtilsResult<()> {
        let start_time = Instant::now();
        let start_memory = get_memory_usage()?;
        self.active_sessions
            .insert(name, (start_time, start_memory));
        Ok(())
    }

    /// End a profiling session and record results
    pub fn end_session(&mut self, name: &str) -> UtilsResult<ProfileResult> {
        let (start_time, start_memory) = self
            .active_sessions
            .remove(name)
            .ok_or_else(|| UtilsError::InvalidParameter(format!("Session '{name}' not found")))?;

        let duration = start_time.elapsed();
        let end_memory = get_memory_usage()?;
        let memory_delta = end_memory as isize - start_memory as isize;

        self.timer.measurements.insert(name.to_string(), duration);
        self.memory
            .measurements
            .insert(name.to_string(), end_memory);

        Ok(ProfileResult {
            name: name.to_string(),
            duration,
            memory_delta,
            start_memory,
            end_memory,
        })
    }

    /// Profile a closure
    pub fn profile<F, R>(&mut self, name: String, f: F) -> UtilsResult<(R, ProfileResult)>
    where
        F: FnOnce() -> R,
    {
        self.start_session(name.clone())?;
        let result = f();
        let profile_result = self.end_session(&name)?;
        Ok((result, profile_result))
    }

    /// Get timer reference
    pub fn timer(&self) -> &Timer {
        &self.timer
    }

    /// Get memory tracker reference
    pub fn memory(&self) -> &MemoryTracker {
        &self.memory
    }

    /// Clear all data
    pub fn clear(&mut self) {
        self.timer.clear();
        self.memory.clear();
        self.active_sessions.clear();
    }

    /// Generate a performance report
    pub fn report(&self) -> ProfileReport {
        ProfileReport {
            timer_summary: self.timer.summary(),
            peak_memory: self.memory.peak_usage(),
            total_sessions: self.timer.measurements.len(),
            active_sessions: self.active_sessions.len(),
        }
    }
}

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

/// Result from a profiling session
#[derive(Debug, Clone)]
pub struct ProfileResult {
    pub name: String,
    pub duration: Duration,
    pub memory_delta: isize,
    pub start_memory: usize,
    pub end_memory: usize,
}

/// Overall performance report
#[derive(Debug, Clone)]
pub struct ProfileReport {
    pub timer_summary: TimerSummary,
    pub peak_memory: Option<usize>,
    pub total_sessions: usize,
    pub active_sessions: usize,
}

/// Benchmark runner for repeated measurements
#[derive(Debug)]
pub struct Benchmark {
    name: String,
    iterations: usize,
    warmup_iterations: usize,
    measurements: Vec<Duration>,
}

impl Benchmark {
    /// Create a new benchmark
    pub fn new(name: String, iterations: usize) -> Self {
        Self {
            name,
            iterations,
            warmup_iterations: 10,
            measurements: Vec::with_capacity(iterations),
        }
    }

    /// Set number of warmup iterations
    pub fn with_warmup(mut self, warmup_iterations: usize) -> Self {
        self.warmup_iterations = warmup_iterations;
        self
    }

    /// Run the benchmark
    pub fn run<F>(&mut self, mut f: F) -> BenchmarkResult
    where
        F: FnMut(),
    {
        // Warmup phase
        for _ in 0..self.warmup_iterations {
            f();
        }

        // Measurement phase
        self.measurements.clear();
        for _ in 0..self.iterations {
            let start = Instant::now();
            f();
            let duration = start.elapsed();
            self.measurements.push(duration);
        }

        self.analyze()
    }

    fn analyze(&self) -> BenchmarkResult {
        let mut measurements = self.measurements.clone();
        measurements.sort();

        let len = measurements.len();
        let total: Duration = measurements.iter().sum();
        let average = total / len as u32;

        let median = if len.is_multiple_of(2) {
            (measurements[len / 2 - 1] + measurements[len / 2]) / 2
        } else {
            measurements[len / 2]
        };

        let min = measurements[0];
        let max = measurements[len - 1];

        // Calculate percentiles
        let p95_index = ((len as f64) * 0.95) as usize;
        let p99_index = ((len as f64) * 0.99) as usize;
        let p95 = measurements[p95_index.min(len - 1)];
        let p99 = measurements[p99_index.min(len - 1)];

        // Calculate standard deviation
        let variance_sum: f64 = measurements
            .iter()
            .map(|&d| {
                let diff = d.as_secs_f64() - average.as_secs_f64();
                diff * diff
            })
            .sum();
        let variance = variance_sum / len as f64;
        let std_dev = Duration::from_secs_f64(variance.sqrt());

        BenchmarkResult {
            name: self.name.clone(),
            iterations: len,
            total,
            average,
            median,
            min,
            max,
            std_dev,
            p95,
            p99,
        }
    }
}

/// Benchmark analysis results
#[derive(Debug, Clone)]
pub struct BenchmarkResult {
    pub name: String,
    pub iterations: usize,
    pub total: Duration,
    pub average: Duration,
    pub median: Duration,
    pub min: Duration,
    pub max: Duration,
    pub std_dev: Duration,
    pub p95: Duration,
    pub p99: Duration,
}

impl BenchmarkResult {
    /// Format results as a human-readable string
    pub fn format(&self) -> String {
        format!(
            "Benchmark: {}\n\
             Iterations: {}\n\
             Average: {:?}\n\
             Median: {:?}\n\
             Min: {:?}\n\
             Max: {:?}\n\
             Std Dev: {:?}\n\
             95th percentile: {:?}\n\
             99th percentile: {:?}",
            self.name,
            self.iterations,
            self.average,
            self.median,
            self.min,
            self.max,
            self.std_dev,
            self.p95,
            self.p99
        )
    }
}

/// Get current memory usage in bytes
fn get_memory_usage() -> UtilsResult<usize> {
    #[cfg(target_os = "linux")]
    {
        use std::fs;
        let status = fs::read_to_string("/proc/self/status").map_err(|e| {
            UtilsError::InvalidParameter(format!("Failed to read memory info: {e}"))
        })?;

        for line in status.lines() {
            if line.starts_with("VmRSS:") {
                let parts: Vec<&str> = line.split_whitespace().collect();
                if parts.len() >= 2 {
                    let kb: usize = parts[1].parse().map_err(|e| {
                        UtilsError::InvalidParameter(format!("Failed to parse memory: {e}"))
                    })?;
                    return Ok(kb * 1024); // Convert KB to bytes
                }
            }
        }
        Err(UtilsError::InvalidParameter(
            "Memory info not found".to_string(),
        ))
    }

    #[cfg(target_os = "macos")]
    {
        // On macOS, we'll use a simpler approach with task_info
        // For now, return a placeholder value
        use std::process::Command;
        let output = Command::new("ps")
            .args(["-o", "rss=", "-p"])
            .arg(std::process::id().to_string())
            .output()
            .map_err(|e| {
                UtilsError::InvalidParameter(format!("Failed to get memory usage: {e}"))
            })?;

        let output_str = String::from_utf8_lossy(&output.stdout);
        let kb: usize = output_str
            .trim()
            .parse()
            .map_err(|e| UtilsError::InvalidParameter(format!("Failed to parse memory: {e}")))?;
        Ok(kb * 1024) // Convert KB to bytes
    }

    #[cfg(not(any(target_os = "linux", target_os = "macos")))]
    {
        // For other platforms, return a default value
        Ok(0)
    }
}

/// Performance regression detection system
#[derive(Debug, Clone)]
pub struct RegressionDetector {
    baselines: HashMap<String, BaselineMetrics>,
    threshold_factor: f64,
    min_samples: usize,
}

/// Baseline performance metrics for regression detection
#[derive(Debug, Clone)]
pub struct BaselineMetrics {
    pub average_duration: Duration,
    pub std_dev: Duration,
    pub sample_count: usize,
    pub last_updated: std::time::SystemTime,
    pub historical_durations: Vec<Duration>,
}

/// Regression detection result
#[derive(Debug, Clone)]
pub struct RegressionResult {
    pub test_name: String,
    pub current_duration: Duration,
    pub baseline_average: Duration,
    pub deviation_factor: f64,
    pub is_regression: bool,
    pub confidence_level: f64,
}

impl RegressionDetector {
    /// Create a new regression detector
    pub fn new() -> Self {
        Self {
            baselines: HashMap::new(),
            threshold_factor: 1.5, // 50% slower is considered a regression
            min_samples: 5,
        }
    }

    /// Set the regression threshold factor (e.g., 1.5 = 50% slower)
    pub fn with_threshold(mut self, threshold_factor: f64) -> Self {
        self.threshold_factor = threshold_factor;
        self
    }

    /// Set minimum samples required for regression detection
    pub fn with_min_samples(mut self, min_samples: usize) -> Self {
        self.min_samples = min_samples;
        self
    }

    /// Record a baseline measurement
    pub fn record_baseline(&mut self, test_name: String, duration: Duration) {
        let test_name_clone = test_name.clone();
        let entry = self
            .baselines
            .entry(test_name)
            .or_insert_with(|| BaselineMetrics {
                average_duration: Duration::from_secs(0),
                std_dev: Duration::from_secs(0),
                sample_count: 0,
                last_updated: std::time::SystemTime::now(),
                historical_durations: Vec::new(),
            });

        entry.historical_durations.push(duration);
        entry.sample_count += 1;
        entry.last_updated = std::time::SystemTime::now();

        // Keep only the last 100 measurements to avoid unbounded growth
        if entry.historical_durations.len() > 100 {
            entry.historical_durations.remove(0);
        }

        // Recalculate statistics - work with a cloned entry to avoid borrowing issues
        let mut entry_clone = entry.clone();
        let _ = entry; // Release the mutable reference to self.baselines
        self.update_statistics(&mut entry_clone, &test_name_clone);

        // Update the entry back in the map
        if let Some(stored_entry) = self.baselines.get_mut(&test_name_clone) {
            *stored_entry = entry_clone;
        }
    }

    /// Check for performance regression
    pub fn check_regression(
        &self,
        test_name: &str,
        current_duration: Duration,
    ) -> Option<RegressionResult> {
        let baseline = self.baselines.get(test_name)?;

        if baseline.sample_count < self.min_samples {
            return None;
        }

        let current_secs = current_duration.as_secs_f64();
        let baseline_secs = baseline.average_duration.as_secs_f64();
        let deviation_factor = current_secs / baseline_secs;

        let is_regression = deviation_factor > self.threshold_factor;

        // Calculate confidence level using t-test-like approach
        let std_dev_secs = baseline.std_dev.as_secs_f64();
        let z_score =
            (current_secs - baseline_secs) / (std_dev_secs / (baseline.sample_count as f64).sqrt());
        let confidence_level = if z_score > 0.0 {
            1.0 - (-z_score * z_score / 2.0).exp() // Approximation of normal CDF
        } else {
            0.0
        };

        Some(RegressionResult {
            test_name: test_name.to_string(),
            current_duration,
            baseline_average: baseline.average_duration,
            deviation_factor,
            is_regression,
            confidence_level,
        })
    }

    /// Get baseline metrics for a test
    pub fn get_baseline(&self, test_name: &str) -> Option<&BaselineMetrics> {
        self.baselines.get(test_name)
    }

    /// List all tracked tests
    pub fn tracked_tests(&self) -> Vec<&String> {
        self.baselines.keys().collect()
    }

    /// Clear all baselines
    pub fn clear_baselines(&mut self) {
        self.baselines.clear();
    }

    /// Update statistics for a baseline
    fn update_statistics(&mut self, baseline: &mut BaselineMetrics, test_name: &str) {
        if baseline.historical_durations.is_empty() {
            return;
        }

        // Calculate average
        let total: Duration = baseline.historical_durations.iter().sum();
        baseline.average_duration = total / baseline.historical_durations.len() as u32;

        // Calculate standard deviation
        let mean_secs = baseline.average_duration.as_secs_f64();
        let variance_sum: f64 = baseline
            .historical_durations
            .iter()
            .map(|d| {
                let diff = d.as_secs_f64() - mean_secs;
                diff * diff
            })
            .sum();

        let variance = variance_sum / baseline.historical_durations.len() as f64;
        baseline.std_dev = Duration::from_secs_f64(variance.sqrt());

        // Update the entry in the map
        self.baselines
            .insert(test_name.to_string(), baseline.clone());
    }
}

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

impl RegressionResult {
    /// Format the regression result as a human-readable string
    pub fn format(&self) -> String {
        let status = if self.is_regression {
            "REGRESSION DETECTED"
        } else {
            "OK"
        };
        format!(
            "Test: {} [{}]\n\
             Current: {:?}\n\
             Baseline: {:?}\n\
             Deviation: {:.2}x\n\
             Confidence: {:.1}%",
            self.test_name,
            status,
            self.current_duration,
            self.baseline_average,
            self.deviation_factor,
            self.confidence_level * 100.0
        )
    }
}

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;

    #[test]
    fn test_timer_basic() {
        let mut timer = Timer::new();

        timer.start();
        thread::sleep(Duration::from_millis(10));
        let duration = timer.stop().expect("operation should succeed");

        assert!(duration >= Duration::from_millis(10));
        assert!(duration < Duration::from_millis(50)); // Allow some tolerance
    }

    #[test]
    fn test_timer_closure() {
        let mut timer = Timer::new();

        let (result, duration) = timer.time(|| {
            thread::sleep(Duration::from_millis(10));
            42
        });

        assert_eq!(result, 42);
        assert!(duration >= Duration::from_millis(10));
    }

    #[test]
    fn test_timer_store() {
        let mut timer = Timer::new();

        timer.start();
        thread::sleep(Duration::from_millis(10));
        timer
            .stop_and_store("test".to_string())
            .expect("operation should succeed");

        assert!(timer.get_measurement("test").is_some());
        assert!(
            timer
                .get_measurement("test")
                .expect("operation should succeed")
                >= Duration::from_millis(10)
        );
    }

    #[test]
    fn test_timer_summary() {
        let mut timer = Timer::new();

        timer
            .measurements
            .insert("test1".to_string(), Duration::from_millis(100));
        timer
            .measurements
            .insert("test2".to_string(), Duration::from_millis(200));
        timer
            .measurements
            .insert("test3".to_string(), Duration::from_millis(300));

        let summary = timer.summary();
        assert_eq!(summary.count, 3);
        assert_eq!(summary.min, Duration::from_millis(100));
        assert_eq!(summary.max, Duration::from_millis(300));
        assert_eq!(summary.average, Duration::from_millis(200));
    }

    #[test]
    fn test_memory_tracker() {
        let mut tracker = MemoryTracker::new();

        tracker.set_baseline();
        tracker
            .record("test".to_string())
            .expect("operation should succeed");

        assert!(tracker.measurements().contains_key("test"));
        assert!(tracker.peak_usage().is_some());
    }

    #[test]
    fn test_profiler() {
        let mut profiler = Profiler::new();

        let (result, profile_result) = profiler
            .profile("test".to_string(), || {
                thread::sleep(Duration::from_millis(10));
                42
            })
            .expect("operation should succeed");

        assert_eq!(result, 42);
        assert_eq!(profile_result.name, "test");
        assert!(profile_result.duration >= Duration::from_millis(10));
    }

    #[test]
    fn test_benchmark() {
        let mut benchmark = Benchmark::new("test_benchmark".to_string(), 10).with_warmup(2);

        let result = benchmark.run(|| {
            thread::sleep(Duration::from_millis(1));
        });

        assert_eq!(result.name, "test_benchmark");
        assert_eq!(result.iterations, 10);
        assert!(result.average >= Duration::from_millis(1));
        assert!(result.min <= result.median);
        assert!(result.median <= result.max);
    }

    #[test]
    fn test_benchmark_result_format() {
        let result = BenchmarkResult {
            name: "test".to_string(),
            iterations: 100,
            total: Duration::from_millis(1000),
            average: Duration::from_millis(10),
            median: Duration::from_millis(9),
            min: Duration::from_millis(5),
            max: Duration::from_millis(20),
            std_dev: Duration::from_millis(2),
            p95: Duration::from_millis(15),
            p99: Duration::from_millis(18),
        };

        let formatted = result.format();
        assert!(formatted.contains("test"));
        assert!(formatted.contains("100"));
        assert!(formatted.contains("10ms"));
    }

    #[test]
    fn test_profiler_sessions() {
        let mut profiler = Profiler::new();

        profiler
            .start_session("session1".to_string())
            .expect("operation should succeed");
        thread::sleep(Duration::from_millis(10));
        let result = profiler
            .end_session("session1")
            .expect("operation should succeed");

        assert_eq!(result.name, "session1");
        assert!(result.duration >= Duration::from_millis(10));
    }

    #[test]
    fn test_profiler_report() {
        let mut profiler = Profiler::new();

        profiler
            .profile("test1".to_string(), || {})
            .expect("operation should succeed");
        profiler
            .profile("test2".to_string(), || {})
            .expect("operation should succeed");

        let report = profiler.report();
        assert_eq!(report.total_sessions, 2);
        assert_eq!(report.active_sessions, 0);
    }

    #[test]
    fn test_regression_detector_baseline() {
        let mut detector = RegressionDetector::new();

        // Record several baseline measurements
        for i in 0..10 {
            detector.record_baseline("test_func".to_string(), Duration::from_millis(100 + i));
        }

        let baseline = detector
            .get_baseline("test_func")
            .expect("operation should succeed");
        assert_eq!(baseline.sample_count, 10);
        assert!(baseline.average_duration >= Duration::from_millis(100));
        assert!(baseline.average_duration <= Duration::from_millis(110));
    }

    #[test]
    fn test_regression_detector_no_regression() {
        let mut detector = RegressionDetector::new().with_min_samples(3);

        // Record baseline measurements around 100ms
        for _ in 0..5 {
            detector.record_baseline("test_func".to_string(), Duration::from_millis(100));
        }

        // Test with similar performance (no regression)
        let result = detector.check_regression("test_func", Duration::from_millis(105));
        assert!(result.is_some());
        let result = result.expect("operation should succeed");
        assert!(!result.is_regression);
        assert_eq!(result.test_name, "test_func");
    }

    #[test]
    fn test_regression_detector_with_regression() {
        let mut detector = RegressionDetector::new()
            .with_min_samples(3)
            .with_threshold(1.5);

        // Record baseline measurements around 100ms
        for _ in 0..5 {
            detector.record_baseline("test_func".to_string(), Duration::from_millis(100));
        }

        // Test with 2x slower performance (regression)
        let result = detector.check_regression("test_func", Duration::from_millis(200));
        assert!(result.is_some());
        let result = result.expect("operation should succeed");
        assert!(result.is_regression);
        assert!(result.deviation_factor > 1.5);
    }

    #[test]
    fn test_regression_detector_insufficient_samples() {
        let mut detector = RegressionDetector::new().with_min_samples(5);

        // Record only 2 baseline measurements (less than min_samples)
        for _ in 0..2 {
            detector.record_baseline("test_func".to_string(), Duration::from_millis(100));
        }

        // Should return None due to insufficient samples
        let result = detector.check_regression("test_func", Duration::from_millis(200));
        assert!(result.is_none());
    }

    #[test]
    fn test_regression_detector_tracked_tests() {
        let mut detector = RegressionDetector::new();

        detector.record_baseline("test1".to_string(), Duration::from_millis(100));
        detector.record_baseline("test2".to_string(), Duration::from_millis(200));

        let tracked = detector.tracked_tests();
        assert_eq!(tracked.len(), 2);
        assert!(tracked.contains(&&"test1".to_string()));
        assert!(tracked.contains(&&"test2".to_string()));
    }

    #[test]
    fn test_regression_result_format() {
        let result = RegressionResult {
            test_name: "test_function".to_string(),
            current_duration: Duration::from_millis(200),
            baseline_average: Duration::from_millis(100),
            deviation_factor: 2.0,
            is_regression: true,
            confidence_level: 0.95,
        };

        let formatted = result.format();
        assert!(formatted.contains("test_function"));
        assert!(formatted.contains("REGRESSION DETECTED"));
        assert!(formatted.contains("2.00x"));
        assert!(formatted.contains("95.0%"));
    }

    #[test]
    fn test_regression_detector_clear() {
        let mut detector = RegressionDetector::new();

        detector.record_baseline("test1".to_string(), Duration::from_millis(100));
        detector.record_baseline("test2".to_string(), Duration::from_millis(200));

        assert_eq!(detector.tracked_tests().len(), 2);

        detector.clear_baselines();
        assert_eq!(detector.tracked_tests().len(), 0);
    }
}