|
| 1 | +use std::fs; |
| 2 | +use std::path::PathBuf; |
| 3 | + |
| 4 | +use crate::types::estimates::Estimates; |
| 5 | + |
| 6 | +/// Result of a benchmark comparison. |
| 7 | +#[derive(Debug)] |
| 8 | +pub struct BenchmarkComparison { |
| 9 | + pub name: String, |
| 10 | + pub change_percentage: f64, |
| 11 | + pub exceeds_limit: bool, |
| 12 | +} |
| 13 | + |
| 14 | +/// Loads change estimates from criterion's change directory for a given benchmark. |
| 15 | +/// Panics if the change file doesn't exist. |
| 16 | +fn load_change_estimates(bench_name: &str) -> Estimates { |
| 17 | + let change_path = |
| 18 | + PathBuf::from("target/criterion").join(bench_name).join("change/estimates.json"); |
| 19 | + |
| 20 | + if !change_path.exists() { |
| 21 | + panic!( |
| 22 | + "Change file not found for benchmark '{}': {}\nThis likely means no baseline exists. \ |
| 23 | + Run the benchmark at least once before using run-and-compare.", |
| 24 | + bench_name, |
| 25 | + change_path.display() |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + let data = fs::read_to_string(&change_path) |
| 30 | + .unwrap_or_else(|e| panic!("Failed to read {}: {}", change_path.display(), e)); |
| 31 | + |
| 32 | + serde_json::from_str(&data).unwrap_or_else(|e| { |
| 33 | + panic!("Failed to deserialize {}: {}\nContent: {}", change_path.display(), e, data) |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +/// Converts change estimates to percentage. |
| 38 | +/// The mean.point_estimate in change/estimates.json represents fractional change |
| 39 | +/// (e.g., 0.0706 = 7.06% change). |
| 40 | +fn get_regression_percentage(change_estimates: &Estimates) -> f64 { |
| 41 | + change_estimates.mean.point_estimate * 100.0 |
| 42 | +} |
| 43 | + |
| 44 | +/// Checks all benchmarks for regressions against a specified limit. |
| 45 | +/// Returns a vector of comparison results for all benchmarks. |
| 46 | +/// If any benchmark exceeds the regression limit, returns an error with detailed results. |
| 47 | +/// Panics if change file is not found for any benchmark. |
| 48 | +pub fn check_regressions( |
| 49 | + bench_names: &[&str], |
| 50 | + regression_limit: f64, |
| 51 | +) -> Result<Vec<BenchmarkComparison>, (String, Vec<BenchmarkComparison>)> { |
| 52 | + let mut results = Vec::new(); |
| 53 | + let mut exceeded_count = 0; |
| 54 | + |
| 55 | + for bench_name in bench_names { |
| 56 | + let change_estimates = load_change_estimates(bench_name); |
| 57 | + let change_percentage = get_regression_percentage(&change_estimates); |
| 58 | + let exceeds_limit = change_percentage > regression_limit; |
| 59 | + |
| 60 | + if exceeds_limit { |
| 61 | + exceeded_count += 1; |
| 62 | + } |
| 63 | + |
| 64 | + results.push(BenchmarkComparison { |
| 65 | + name: bench_name.to_string(), |
| 66 | + change_percentage, |
| 67 | + exceeds_limit, |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + if exceeded_count > 0 { |
| 72 | + let error_msg = format!("{} benchmark(s) exceeded regression threshold!", exceeded_count); |
| 73 | + Err((error_msg, results)) |
| 74 | + } else { |
| 75 | + Ok(results) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod tests { |
| 81 | + use super::*; |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_get_regression_percentage() { |
| 85 | + let estimates = Estimates { |
| 86 | + mean: crate::types::estimates::Stat { |
| 87 | + point_estimate: 0.0706, |
| 88 | + standard_error: 0.01, |
| 89 | + confidence_interval: crate::types::estimates::ConfidenceInterval { |
| 90 | + confidence_level: 0.95, |
| 91 | + lower_bound: 0.05, |
| 92 | + upper_bound: 0.09, |
| 93 | + }, |
| 94 | + }, |
| 95 | + median: crate::types::estimates::Stat { |
| 96 | + point_estimate: 0.03, |
| 97 | + standard_error: 0.01, |
| 98 | + confidence_interval: crate::types::estimates::ConfidenceInterval { |
| 99 | + confidence_level: 0.95, |
| 100 | + lower_bound: 0.01, |
| 101 | + upper_bound: 0.05, |
| 102 | + }, |
| 103 | + }, |
| 104 | + std_dev: None, |
| 105 | + median_abs_dev: None, |
| 106 | + slope: None, |
| 107 | + }; |
| 108 | + |
| 109 | + let percentage = get_regression_percentage(&estimates); |
| 110 | + assert!((percentage - 7.06).abs() < 0.01); |
| 111 | + } |
| 112 | +} |
0 commit comments