torsh-profiler 0.1.2

Performance profiling and monitoring for ToRSh
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
//! CI/CD integration for performance profiling
//!
//! This module provides integration with continuous integration and deployment
//! systems for automated performance monitoring and regression detection.

use crate::{
    regression::{RegressionDetector, RegressionResult, RegressionSeverity},
    ProfileEvent,
};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::time::{Duration, SystemTime};

/// CI/CD platform types
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum CiCdPlatform {
    GitHub,
    GitLab,
    Jenkins,
    CircleCI,
    TravisCI,
    AppVeyor,
    TeamCity,
    Bamboo,
    Azure,
    Custom(String),
}

impl std::fmt::Display for CiCdPlatform {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CiCdPlatform::GitHub => write!(f, "GitHub Actions"),
            CiCdPlatform::GitLab => write!(f, "GitLab CI"),
            CiCdPlatform::Jenkins => write!(f, "Jenkins"),
            CiCdPlatform::CircleCI => write!(f, "CircleCI"),
            CiCdPlatform::TravisCI => write!(f, "Travis CI"),
            CiCdPlatform::AppVeyor => write!(f, "AppVeyor"),
            CiCdPlatform::TeamCity => write!(f, "TeamCity"),
            CiCdPlatform::Bamboo => write!(f, "Bamboo"),
            CiCdPlatform::Azure => write!(f, "Azure DevOps"),
            CiCdPlatform::Custom(name) => write!(f, "{name}"),
        }
    }
}

/// CI/CD build information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BuildInfo {
    pub build_id: String,
    pub commit_hash: String,
    pub branch: String,
    pub pr_number: Option<u32>,
    pub author: String,
    pub timestamp: SystemTime,
    pub platform: CiCdPlatform,
    pub build_url: Option<String>,
    pub artifacts_url: Option<String>,
}

/// Performance benchmark result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkResult {
    pub name: String,
    pub duration_ns: u64,
    pub throughput: Option<f64>,
    pub memory_usage: Option<u64>,
    pub cpu_usage: Option<f64>,
    pub custom_metrics: HashMap<String, f64>,
}

/// CI/CD performance report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceReport {
    pub build_info: BuildInfo,
    pub benchmarks: Vec<BenchmarkResult>,
    pub regression_analysis: Option<RegressionAnalysis>,
    pub summary: ReportSummary,
    pub recommendations: Vec<String>,
}

/// Regression analysis results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegressionAnalysis {
    pub regressions_found: Vec<RegressionResult>,
    pub improvements_found: Vec<RegressionResult>,
    pub overall_status: RegressionStatus,
}

/// Overall regression status
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum RegressionStatus {
    Pass,
    Warning,
    Fail,
}

/// Report summary
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReportSummary {
    pub total_benchmarks: usize,
    pub passed_benchmarks: usize,
    pub failed_benchmarks: usize,
    pub regression_count: usize,
    pub improvement_count: usize,
    pub overall_status: RegressionStatus,
}

/// CI/CD configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CiCdConfig {
    pub platform: CiCdPlatform,
    pub baseline_path: String,
    pub report_path: String,
    pub fail_on_regression: bool,
    pub regression_threshold: f64,
    pub improvement_threshold: f64,
    pub enable_comments: bool,
    pub comment_template: Option<String>,
    pub artifact_retention_days: u32,
}

impl Default for CiCdConfig {
    fn default() -> Self {
        Self {
            platform: CiCdPlatform::GitHub,
            baseline_path: "performance_baseline.json".to_string(),
            report_path: "performance_report.json".to_string(),
            fail_on_regression: true,
            regression_threshold: 0.05,  // 5%
            improvement_threshold: 0.05, // 5%
            enable_comments: true,
            comment_template: None,
            artifact_retention_days: 30,
        }
    }
}

/// CI/CD integration manager
pub struct CiCdIntegration {
    config: CiCdConfig,
    regression_detector: RegressionDetector,
}

impl CiCdIntegration {
    pub fn new(config: CiCdConfig) -> Self {
        let regression_config = crate::regression::RegressionConfig {
            min_baseline_samples: 10,
            max_baseline_age_days: 1,
            regression_threshold_percent: config.regression_threshold * 100.0,
            improvement_threshold_percent: config.improvement_threshold * 100.0,
            significance_level: 0.05,
            adaptive_thresholds: true,
            rolling_window_size: 100,
            outlier_detection: true,
        };
        let regression_detector = RegressionDetector::new(regression_config);

        Self {
            config,
            regression_detector,
        }
    }

    /// Detect build environment and create BuildInfo
    pub fn detect_build_info(&self) -> Result<BuildInfo> {
        let platform = self.config.platform.clone();

        match platform {
            CiCdPlatform::GitHub => self.detect_github_info(),
            CiCdPlatform::GitLab => self.detect_gitlab_info(),
            CiCdPlatform::Jenkins => self.detect_jenkins_info(),
            CiCdPlatform::CircleCI => self.detect_circleci_info(),
            CiCdPlatform::TravisCI => self.detect_travis_info(),
            CiCdPlatform::Azure => self.detect_azure_info(),
            _ => self.detect_generic_info(),
        }
    }

    fn detect_github_info(&self) -> Result<BuildInfo> {
        Ok(BuildInfo {
            build_id: std::env::var("GITHUB_RUN_ID").unwrap_or_else(|_| "unknown".to_string()),
            commit_hash: std::env::var("GITHUB_SHA").unwrap_or_else(|_| "unknown".to_string()),
            branch: std::env::var("GITHUB_REF_NAME").unwrap_or_else(|_| "unknown".to_string()),
            pr_number: std::env::var("GITHUB_EVENT_NUMBER")
                .ok()
                .and_then(|n| n.parse().ok()),
            author: std::env::var("GITHUB_ACTOR").unwrap_or_else(|_| "unknown".to_string()),
            timestamp: SystemTime::now(),
            platform: CiCdPlatform::GitHub,
            build_url: Some(format!(
                "https://github.com/{}/actions/runs/{}",
                std::env::var("GITHUB_REPOSITORY").unwrap_or_else(|_| "unknown".to_string()),
                std::env::var("GITHUB_RUN_ID").unwrap_or_else(|_| "unknown".to_string())
            )),
            artifacts_url: None,
        })
    }

    fn detect_gitlab_info(&self) -> Result<BuildInfo> {
        Ok(BuildInfo {
            build_id: std::env::var("CI_JOB_ID").unwrap_or_else(|_| "unknown".to_string()),
            commit_hash: std::env::var("CI_COMMIT_SHA").unwrap_or_else(|_| "unknown".to_string()),
            branch: std::env::var("CI_COMMIT_REF_NAME").unwrap_or_else(|_| "unknown".to_string()),
            pr_number: std::env::var("CI_MERGE_REQUEST_IID")
                .ok()
                .and_then(|n| n.parse().ok()),
            author: std::env::var("CI_COMMIT_AUTHOR").unwrap_or_else(|_| "unknown".to_string()),
            timestamp: SystemTime::now(),
            platform: CiCdPlatform::GitLab,
            build_url: std::env::var("CI_JOB_URL").ok(),
            artifacts_url: None,
        })
    }

    fn detect_jenkins_info(&self) -> Result<BuildInfo> {
        Ok(BuildInfo {
            build_id: std::env::var("BUILD_NUMBER").unwrap_or_else(|_| "unknown".to_string()),
            commit_hash: std::env::var("GIT_COMMIT").unwrap_or_else(|_| "unknown".to_string()),
            branch: std::env::var("GIT_BRANCH").unwrap_or_else(|_| "unknown".to_string()),
            pr_number: std::env::var("CHANGE_ID").ok().and_then(|n| n.parse().ok()),
            author: std::env::var("BUILD_USER").unwrap_or_else(|_| "unknown".to_string()),
            timestamp: SystemTime::now(),
            platform: CiCdPlatform::Jenkins,
            build_url: std::env::var("BUILD_URL").ok(),
            artifacts_url: None,
        })
    }

    fn detect_circleci_info(&self) -> Result<BuildInfo> {
        Ok(BuildInfo {
            build_id: std::env::var("CIRCLE_BUILD_NUM").unwrap_or_else(|_| "unknown".to_string()),
            commit_hash: std::env::var("CIRCLE_SHA1").unwrap_or_else(|_| "unknown".to_string()),
            branch: std::env::var("CIRCLE_BRANCH").unwrap_or_else(|_| "unknown".to_string()),
            pr_number: std::env::var("CIRCLE_PR_NUMBER")
                .ok()
                .and_then(|n| n.parse().ok()),
            author: std::env::var("CIRCLE_USERNAME").unwrap_or_else(|_| "unknown".to_string()),
            timestamp: SystemTime::now(),
            platform: CiCdPlatform::CircleCI,
            build_url: std::env::var("CIRCLE_BUILD_URL").ok(),
            artifacts_url: None,
        })
    }

    fn detect_travis_info(&self) -> Result<BuildInfo> {
        Ok(BuildInfo {
            build_id: std::env::var("TRAVIS_BUILD_NUMBER")
                .unwrap_or_else(|_| "unknown".to_string()),
            commit_hash: std::env::var("TRAVIS_COMMIT").unwrap_or_else(|_| "unknown".to_string()),
            branch: std::env::var("TRAVIS_BRANCH").unwrap_or_else(|_| "unknown".to_string()),
            pr_number: std::env::var("TRAVIS_PULL_REQUEST")
                .ok()
                .and_then(|n| n.parse().ok()),
            author: std::env::var("TRAVIS_COMMIT_AUTHOR").unwrap_or_else(|_| "unknown".to_string()),
            timestamp: SystemTime::now(),
            platform: CiCdPlatform::TravisCI,
            build_url: std::env::var("TRAVIS_BUILD_WEB_URL").ok(),
            artifacts_url: None,
        })
    }

    fn detect_azure_info(&self) -> Result<BuildInfo> {
        Ok(BuildInfo {
            build_id: std::env::var("BUILD_BUILDID").unwrap_or_else(|_| "unknown".to_string()),
            commit_hash: std::env::var("BUILD_SOURCEVERSION")
                .unwrap_or_else(|_| "unknown".to_string()),
            branch: std::env::var("BUILD_SOURCEBRANCH").unwrap_or_else(|_| "unknown".to_string()),
            pr_number: std::env::var("SYSTEM_PULLREQUEST_PULLREQUESTNUMBER")
                .ok()
                .and_then(|n| n.parse().ok()),
            author: std::env::var("BUILD_REQUESTEDFOR").unwrap_or_else(|_| "unknown".to_string()),
            timestamp: SystemTime::now(),
            platform: CiCdPlatform::Azure,
            build_url: std::env::var("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI").ok(),
            artifacts_url: None,
        })
    }

    fn detect_generic_info(&self) -> Result<BuildInfo> {
        Ok(BuildInfo {
            build_id: std::env::var("BUILD_ID").unwrap_or_else(|_| "unknown".to_string()),
            commit_hash: std::env::var("COMMIT_HASH").unwrap_or_else(|_| "unknown".to_string()),
            branch: std::env::var("BRANCH").unwrap_or_else(|_| "unknown".to_string()),
            pr_number: std::env::var("PR_NUMBER").ok().and_then(|n| n.parse().ok()),
            author: std::env::var("AUTHOR").unwrap_or_else(|_| "unknown".to_string()),
            timestamp: SystemTime::now(),
            platform: self.config.platform.clone(),
            build_url: std::env::var("BUILD_URL").ok(),
            artifacts_url: None,
        })
    }

    /// Generate performance report from profile events
    pub fn generate_report(&mut self, events: &[ProfileEvent]) -> Result<PerformanceReport> {
        let build_info = self.detect_build_info()?;
        let benchmarks = self.extract_benchmarks(events)?;
        let regression_analysis = self.analyze_regressions(&benchmarks)?;
        let summary = self.generate_summary(&benchmarks, &regression_analysis);
        let recommendations = self.generate_recommendations(&regression_analysis);

        Ok(PerformanceReport {
            build_info,
            benchmarks,
            regression_analysis: Some(regression_analysis),
            summary,
            recommendations,
        })
    }

    fn extract_benchmarks(&self, events: &[ProfileEvent]) -> Result<Vec<BenchmarkResult>> {
        let mut benchmarks = Vec::new();
        let mut benchmark_map: HashMap<String, Vec<&ProfileEvent>> = HashMap::new();

        // Group events by name
        for event in events {
            benchmark_map
                .entry(event.name.clone())
                .or_default()
                .push(event);
        }

        // Convert to benchmark results
        for (name, events) in benchmark_map {
            let total_duration: u64 = events.iter().map(|e| e.duration_us * 1000).sum(); // Convert to ns
            let avg_duration = total_duration / events.len() as u64;

            let throughput = events
                .iter()
                .map(|e| e.operation_count)
                .sum::<Option<u64>>()
                .map(|total_ops| total_ops as f64 / (total_duration as f64 / 1_000_000_000.0));

            let memory_usage = events.iter().map(|e| e.bytes_transferred).sum();

            benchmarks.push(BenchmarkResult {
                name,
                duration_ns: avg_duration,
                throughput,
                memory_usage,
                cpu_usage: None,
                custom_metrics: HashMap::new(),
            });
        }

        Ok(benchmarks)
    }

    fn analyze_regressions(
        &mut self,
        benchmarks: &[BenchmarkResult],
    ) -> Result<RegressionAnalysis> {
        let mut regressions = Vec::new();
        let mut improvements = Vec::new();

        // Load baseline if exists
        if Path::new(&self.config.baseline_path).exists() {
            let baseline_data = fs::read_to_string(&self.config.baseline_path)?;
            let baseline_benchmarks: Vec<BenchmarkResult> = serde_json::from_str(&baseline_data)?;

            for current in benchmarks {
                if let Some(baseline) = baseline_benchmarks.iter().find(|b| b.name == current.name)
                {
                    // Update baseline for the regression detector
                    self.regression_detector.update_baseline(
                        &current.name,
                        "benchmark",
                        vec![baseline.duration_ns as f64],
                    )?;

                    // Create a dummy event for regression detection
                    let dummy_event = crate::ProfileEvent {
                        name: current.name.clone(),
                        category: "benchmark".to_string(),
                        start_us: 0,
                        duration_us: (current.duration_ns / 1000),
                        thread_id: 0,
                        operation_count: None,
                        flops: None,
                        bytes_transferred: None,
                        stack_trace: None,
                    };

                    // Detect regressions using the available method
                    let results = self
                        .regression_detector
                        .detect_regressions(&[dummy_event])?;
                    for result in results {
                        if result.is_regression {
                            regressions.push(result);
                        } else if result.is_improvement {
                            improvements.push(result);
                        }
                    }
                }
            }
        }

        let overall_status = if regressions
            .iter()
            .any(|r| matches!(r.severity, RegressionSeverity::Critical))
        {
            RegressionStatus::Fail
        } else if !regressions.is_empty() {
            RegressionStatus::Warning
        } else {
            RegressionStatus::Pass
        };

        Ok(RegressionAnalysis {
            regressions_found: regressions,
            improvements_found: improvements,
            overall_status,
        })
    }

    fn generate_summary(
        &self,
        benchmarks: &[BenchmarkResult],
        regression_analysis: &RegressionAnalysis,
    ) -> ReportSummary {
        let total_benchmarks = benchmarks.len();
        let regression_count = regression_analysis.regressions_found.len();
        let improvement_count = regression_analysis.improvements_found.len();
        let failed_benchmarks = regression_analysis
            .regressions_found
            .iter()
            .filter(|r| matches!(r.severity, RegressionSeverity::Critical))
            .count();
        let passed_benchmarks = total_benchmarks - failed_benchmarks;

        ReportSummary {
            total_benchmarks,
            passed_benchmarks,
            failed_benchmarks,
            regression_count,
            improvement_count,
            overall_status: regression_analysis.overall_status.clone(),
        }
    }

    fn generate_recommendations(&self, regression_analysis: &RegressionAnalysis) -> Vec<String> {
        let mut recommendations = Vec::new();

        if !regression_analysis.regressions_found.is_empty() {
            recommendations.push(
                "Performance regressions detected. Consider optimizing the affected code paths."
                    .to_string(),
            );

            for regression in &regression_analysis.regressions_found {
                if regression.change_percent > 20.0 {
                    // Consider >20% as critical
                    recommendations.push(format!(
                        "CRITICAL: {} has significant performance regression",
                        regression.metric_name
                    ));
                }
            }
        }

        if !regression_analysis.improvements_found.is_empty() {
            recommendations.push("Performance improvements detected. Good work!".to_string());
        }

        if regression_analysis.overall_status == RegressionStatus::Pass {
            recommendations
                .push("All performance benchmarks are within acceptable thresholds.".to_string());
        }

        recommendations
    }

    /// Save performance report to file
    pub fn save_report(&self, report: &PerformanceReport) -> Result<()> {
        let json = serde_json::to_string_pretty(report)?;
        fs::write(&self.config.report_path, json)?;
        Ok(())
    }

    /// Update performance baseline
    pub fn update_baseline(&self, benchmarks: &[BenchmarkResult]) -> Result<()> {
        let json = serde_json::to_string_pretty(benchmarks)?;
        fs::write(&self.config.baseline_path, json)?;
        Ok(())
    }

    /// Generate comment for pull request
    pub fn generate_pr_comment(&self, report: &PerformanceReport) -> String {
        if let Some(template) = &self.config.comment_template {
            // Use custom template (simplified template engine)
            template
                .replace("{status}", &format!("{:?}", report.summary.overall_status))
                .replace("{total}", &report.summary.total_benchmarks.to_string())
                .replace(
                    "{regressions}",
                    &report.summary.regression_count.to_string(),
                )
                .replace(
                    "{improvements}",
                    &report.summary.improvement_count.to_string(),
                )
        } else {
            // Default comment template
            let mut comment = String::new();
            comment.push_str("## Performance Report\n\n");
            comment.push_str(&format!(
                "**Status**: {:?}\n",
                report.summary.overall_status
            ));
            comment.push_str(&format!(
                "**Total Benchmarks**: {}\n",
                report.summary.total_benchmarks
            ));
            comment.push_str(&format!(
                "**Regressions**: {}\n",
                report.summary.regression_count
            ));
            comment.push_str(&format!(
                "**Improvements**: {}\n",
                report.summary.improvement_count
            ));

            if let Some(analysis) = &report.regression_analysis {
                if !analysis.regressions_found.is_empty() {
                    comment.push_str("\n### Regressions Found\n");
                    for regression in &analysis.regressions_found {
                        comment.push_str(&format!(
                            "- **{}**: Regression ({:.2}% change)\n",
                            regression.metric_name, regression.change_percent
                        ));
                    }
                }

                if !analysis.improvements_found.is_empty() {
                    comment.push_str("\n### Improvements Found\n");
                    for improvement in &analysis.improvements_found {
                        comment.push_str(&format!(
                            "- **{}**: {:.2}% improvement\n",
                            improvement.metric_name,
                            improvement.change_percent.abs()
                        ));
                    }
                }
            }

            if !report.recommendations.is_empty() {
                comment.push_str("\n### Recommendations\n");
                for recommendation in &report.recommendations {
                    comment.push_str(&format!("- {recommendation}\n"));
                }
            }

            comment
        }
    }

    /// Check if build should fail based on regression analysis
    pub fn should_fail_build(&self, report: &PerformanceReport) -> bool {
        if !self.config.fail_on_regression {
            return false;
        }

        matches!(report.summary.overall_status, RegressionStatus::Fail)
    }
}

/// Convenience functions for CI/CD integration
/// Create CI/CD integration with default configuration
pub fn create_ci_cd_integration() -> CiCdIntegration {
    CiCdIntegration::new(CiCdConfig::default())
}

/// Create CI/CD integration with custom configuration
pub fn create_ci_cd_integration_with_config(config: CiCdConfig) -> CiCdIntegration {
    CiCdIntegration::new(config)
}

/// Generate and save performance report
pub fn generate_performance_report(
    events: &[ProfileEvent],
    config: Option<CiCdConfig>,
) -> Result<PerformanceReport> {
    let mut integration = if let Some(config) = config {
        CiCdIntegration::new(config)
    } else {
        create_ci_cd_integration()
    };

    let report = integration.generate_report(events)?;
    integration.save_report(&report)?;

    Ok(report)
}

/// Update performance baseline from current benchmarks
pub fn update_performance_baseline(
    events: &[ProfileEvent],
    config: Option<CiCdConfig>,
) -> Result<()> {
    let integration = if let Some(config) = config {
        CiCdIntegration::new(config)
    } else {
        create_ci_cd_integration()
    };

    let benchmarks = integration.extract_benchmarks(events)?;
    integration.update_baseline(&benchmarks)?;

    Ok(())
}

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

    #[test]
    fn test_build_info_detection() {
        let config = CiCdConfig::default();
        let integration = CiCdIntegration::new(config);

        let build_info = integration.detect_build_info().unwrap();
        assert!(!build_info.build_id.is_empty());
        assert!(!build_info.commit_hash.is_empty());
        assert!(!build_info.branch.is_empty());
        assert!(!build_info.author.is_empty());
    }

    #[test]
    fn test_benchmark_extraction() {
        let config = CiCdConfig::default();
        let integration = CiCdIntegration::new(config);

        let events = vec![
            ProfileEvent {
                name: "test_operation".to_string(),
                category: "test".to_string(),
                start_us: 0,
                duration_us: 1,
                thread_id: 0,
                operation_count: Some(10),
                flops: Some(100),
                bytes_transferred: Some(1024),
                stack_trace: Some("test trace".to_string()),
            },
            ProfileEvent {
                name: "test_operation".to_string(),
                category: "test".to_string(),
                start_us: 0,
                duration_us: 2,
                thread_id: 0,
                operation_count: Some(20),
                flops: Some(200),
                bytes_transferred: Some(2048),
                stack_trace: Some("test trace".to_string()),
            },
        ];

        let benchmarks = integration.extract_benchmarks(&events).unwrap();
        assert_eq!(benchmarks.len(), 1);
        assert_eq!(benchmarks[0].name, "test_operation");
        assert_eq!(benchmarks[0].duration_ns, 1500); // average
    }

    #[test]
    fn test_report_generation() {
        let mut integration = CiCdIntegration::new(CiCdConfig::default());

        let events = vec![ProfileEvent {
            name: "benchmark_1".to_string(),
            category: "test".to_string(),
            start_us: 0,
            duration_us: 1,
            thread_id: 0,
            operation_count: Some(1),
            flops: Some(10),
            bytes_transferred: Some(100),
            stack_trace: Some("test trace".to_string()),
        }];

        let report = integration.generate_report(&events).unwrap();
        assert_eq!(report.benchmarks.len(), 1);
        assert_eq!(report.summary.total_benchmarks, 1);
        assert!(!report.recommendations.is_empty());
    }

    #[test]
    fn test_pr_comment_generation() {
        let integration = CiCdIntegration::new(CiCdConfig::default());

        let report = PerformanceReport {
            build_info: BuildInfo {
                build_id: "123".to_string(),
                commit_hash: "abc123".to_string(),
                branch: "main".to_string(),
                pr_number: Some(456),
                author: "test_author".to_string(),
                timestamp: SystemTime::now(),
                platform: CiCdPlatform::GitHub,
                build_url: None,
                artifacts_url: None,
            },
            benchmarks: vec![],
            regression_analysis: None,
            summary: ReportSummary {
                total_benchmarks: 5,
                passed_benchmarks: 4,
                failed_benchmarks: 1,
                regression_count: 1,
                improvement_count: 2,
                overall_status: RegressionStatus::Warning,
            },
            recommendations: vec!["Test recommendation".to_string()],
        };

        let comment = integration.generate_pr_comment(&report);
        assert!(comment.contains("Performance Report"));
        assert!(comment.contains("Warning"));
        assert!(comment.contains("**Total Benchmarks**: 5"));
        assert!(comment.contains("Test recommendation"));
    }

    #[test]
    fn test_platform_display() {
        assert_eq!(CiCdPlatform::GitHub.to_string(), "GitHub Actions");
        assert_eq!(CiCdPlatform::GitLab.to_string(), "GitLab CI");
        assert_eq!(
            CiCdPlatform::Custom("Custom CI".to_string()).to_string(),
            "Custom CI"
        );
    }

    #[test]
    fn test_config_serialization() {
        let config = CiCdConfig {
            platform: CiCdPlatform::GitHub,
            baseline_path: "test_baseline.json".to_string(),
            report_path: "test_report.json".to_string(),
            fail_on_regression: true,
            regression_threshold: 0.1,
            improvement_threshold: 0.05,
            enable_comments: true,
            comment_template: Some("Custom template".to_string()),
            artifact_retention_days: 7,
        };

        let json = serde_json::to_string(&config).unwrap();
        let deserialized: CiCdConfig = serde_json::from_str(&json).unwrap();

        assert_eq!(config.platform, deserialized.platform);
        assert_eq!(config.baseline_path, deserialized.baseline_path);
        assert_eq!(
            config.regression_threshold,
            deserialized.regression_threshold
        );
    }

    #[test]
    fn test_build_failure_conditions() {
        let config = CiCdConfig {
            fail_on_regression: true,
            ..CiCdConfig::default()
        };
        let integration = CiCdIntegration::new(config);

        let fail_report = PerformanceReport {
            build_info: BuildInfo {
                build_id: "123".to_string(),
                commit_hash: "abc123".to_string(),
                branch: "main".to_string(),
                pr_number: None,
                author: "test".to_string(),
                timestamp: SystemTime::now(),
                platform: CiCdPlatform::GitHub,
                build_url: None,
                artifacts_url: None,
            },
            benchmarks: vec![],
            regression_analysis: None,
            summary: ReportSummary {
                total_benchmarks: 1,
                passed_benchmarks: 0,
                failed_benchmarks: 1,
                regression_count: 1,
                improvement_count: 0,
                overall_status: RegressionStatus::Fail,
            },
            recommendations: vec![],
        };

        assert!(integration.should_fail_build(&fail_report));

        let pass_report = PerformanceReport {
            build_info: fail_report.build_info.clone(),
            benchmarks: vec![],
            regression_analysis: None,
            summary: ReportSummary {
                total_benchmarks: 1,
                passed_benchmarks: 1,
                failed_benchmarks: 0,
                regression_count: 0,
                improvement_count: 0,
                overall_status: RegressionStatus::Pass,
            },
            recommendations: vec![],
        };

        assert!(!integration.should_fail_build(&pass_report));
    }
}