|
| 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 | +type RegressionError = (String, Vec<BenchmarkComparison>); |
| 15 | +type BenchmarkComparisonsResult = Result<Vec<BenchmarkComparison>, RegressionError>; |
| 16 | + |
| 17 | +/// Loads change estimates from criterion's change directory for a given benchmark. |
| 18 | +/// Panics if the change file doesn't exist. |
| 19 | +fn load_change_estimates(bench_name: &str) -> Estimates { |
| 20 | + let change_path = |
| 21 | + PathBuf::from("target/criterion").join(bench_name).join("change/estimates.json"); |
| 22 | + |
| 23 | + if !change_path.exists() { |
| 24 | + panic!( |
| 25 | + "Change file not found for benchmark '{}': {}\nThis likely means no baseline exists. \ |
| 26 | + Run the benchmark at least once before using run-and-compare.", |
| 27 | + bench_name, |
| 28 | + change_path.display() |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + let data = fs::read_to_string(&change_path) |
| 33 | + .unwrap_or_else(|e| panic!("Failed to read {}: {}", change_path.display(), e)); |
| 34 | + |
| 35 | + serde_json::from_str(&data).unwrap_or_else(|e| { |
| 36 | + panic!("Failed to deserialize {}: {}\nContent: {}", change_path.display(), e, data) |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +/// Converts change estimates to percentage. |
| 41 | +/// The mean.point_estimate in change/estimates.json represents fractional change |
| 42 | +/// (e.g., 0.0706 = 7.06% change). |
| 43 | +pub(crate) fn get_regression_percentage(change_estimates: &Estimates) -> f64 { |
| 44 | + change_estimates.mean.point_estimate * 100.0 |
| 45 | +} |
| 46 | + |
| 47 | +/// Checks all benchmarks for regressions against a specified limit. |
| 48 | +/// Returns a vector of comparison results for all benchmarks. |
| 49 | +/// If any benchmark exceeds the regression limit, returns an error with detailed results. |
| 50 | +/// Panics if change file is not found for any benchmark. |
| 51 | +pub fn check_regressions( |
| 52 | + bench_names: &[&str], |
| 53 | + regression_limit: f64, |
| 54 | +) -> BenchmarkComparisonsResult { |
| 55 | + let mut results = Vec::new(); |
| 56 | + let mut exceeded_count = 0; |
| 57 | + |
| 58 | + for bench_name in bench_names { |
| 59 | + let change_estimates = load_change_estimates(bench_name); |
| 60 | + let change_percentage = get_regression_percentage(&change_estimates); |
| 61 | + let exceeds_limit = change_percentage > regression_limit; |
| 62 | + |
| 63 | + if exceeds_limit { |
| 64 | + exceeded_count += 1; |
| 65 | + } |
| 66 | + |
| 67 | + results.push(BenchmarkComparison { |
| 68 | + name: bench_name.to_string(), |
| 69 | + change_percentage, |
| 70 | + exceeds_limit, |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + if exceeded_count > 0 { |
| 75 | + let error_msg = format!("{} benchmark(s) exceeded regression threshold!", exceeded_count); |
| 76 | + Err((error_msg, results)) |
| 77 | + } else { |
| 78 | + Ok(results) |
| 79 | + } |
| 80 | +} |
0 commit comments