|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// |
| 3 | +// check-bench-regression — compare a criterion bencher run against |
| 4 | +// .machine_readable/benchmarks/baselines.json and fail if any benchmark |
| 5 | +// regressed by more than the configured threshold. A faithful Rust port of |
| 6 | +// the former scripts/check-bench-regression.py (org policy bans Python |
| 7 | +// outside SaltStack). Pairs with update-bench-baselines. |
| 8 | +// |
| 9 | +// Usage: |
| 10 | +// check-bench-regression <bencher-output> <baselines.json> |
| 11 | +// |
| 12 | +// Exit status: 0 = no regressions over threshold (or no baselines yet), |
| 13 | +// 1 = at least one regression, 2 = usage / file error. |
| 14 | +// |
| 15 | +// Markdown summary -> stdout (for $GITHUB_STEP_SUMMARY); `::error::` |
| 16 | +// annotations -> stderr. |
| 17 | + |
| 18 | +use bench_tools::{fmt_ns, parse_bencher_output, parse_json, Json}; |
| 19 | +use std::process::exit; |
| 20 | + |
| 21 | +fn main() { |
| 22 | + let argv: Vec<String> = std::env::args().collect(); |
| 23 | + if argv.len() != 3 { |
| 24 | + eprintln!("usage: check-bench-regression <bencher-output> <baselines.json>"); |
| 25 | + exit(2); |
| 26 | + } |
| 27 | + let current_path = &argv[1]; |
| 28 | + let baselines_path = &argv[2]; |
| 29 | + |
| 30 | + let current_text = match std::fs::read_to_string(current_path) { |
| 31 | + Ok(t) => t, |
| 32 | + Err(_) => { |
| 33 | + eprintln!("error: {current_path} missing"); |
| 34 | + exit(2); |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + let mut current = parse_bencher_output(¤t_text); |
| 39 | + current.sort_by(|a, b| a.0.cmp(&b.0)); // Python iterates `sorted(current.items())` |
| 40 | + |
| 41 | + if current.is_empty() { |
| 42 | + println!( |
| 43 | + "::warning::no bench lines parsed from current run \u{2014} \ |
| 44 | + did criterion use --output-format bencher?" |
| 45 | + ); |
| 46 | + exit(0); |
| 47 | + } |
| 48 | + |
| 49 | + let baseline_doc: Json = match std::fs::read_to_string(baselines_path) { |
| 50 | + Ok(t) => match parse_json(&t) { |
| 51 | + Ok(v) => v, |
| 52 | + Err(_) => { |
| 53 | + println!( |
| 54 | + "::warning::{baselines_path} is not valid JSON; \ |
| 55 | + treating as empty baseline" |
| 56 | + ); |
| 57 | + Json::Obj(vec![]) |
| 58 | + } |
| 59 | + }, |
| 60 | + Err(_) => Json::Obj(vec![]), |
| 61 | + }; |
| 62 | + |
| 63 | + let baselines: Vec<(String, f64)> = match baseline_doc.get("baselines") { |
| 64 | + Some(Json::Obj(p)) => p |
| 65 | + .iter() |
| 66 | + .filter_map(|(k, v)| v.as_f64().map(|n| (k.clone(), n))) |
| 67 | + .collect(), |
| 68 | + _ => vec![], |
| 69 | + }; |
| 70 | + let lookup = |name: &str| baselines.iter().find(|(k, _)| k == name).map(|(_, v)| *v); |
| 71 | + |
| 72 | + let threshold_pct = baseline_doc |
| 73 | + .get("_regression_threshold_pct") |
| 74 | + .and_then(|v| v.as_f64()) |
| 75 | + .unwrap_or(50.0); |
| 76 | + |
| 77 | + if baselines.is_empty() { |
| 78 | + println!("## Benchmark run (advisory mode \u{2014} no baselines yet)"); |
| 79 | + println!(); |
| 80 | + println!("| Benchmark | Current |"); |
| 81 | + println!("|-----------|---------|"); |
| 82 | + for (name, ns) in ¤t { |
| 83 | + println!("| `{name}` | {} |", fmt_ns(*ns)); |
| 84 | + } |
| 85 | + println!(); |
| 86 | + println!( |
| 87 | + "_No entries in `baselines.json` yet \u{2014} see \ |
| 88 | + `.machine_readable/benchmarks/README.md` for how to seed them._" |
| 89 | + ); |
| 90 | + exit(0); |
| 91 | + } |
| 92 | + |
| 93 | + let mut regressions: Vec<(String, i64, i64, f64)> = vec![]; |
| 94 | + let mut rows: Vec<(String, String, String, String, String)> = vec![]; |
| 95 | + |
| 96 | + for (name, ns_now) in ¤t { |
| 97 | + let ns_now = *ns_now; |
| 98 | + match lookup(name) { |
| 99 | + None => rows.push(( |
| 100 | + name.clone(), |
| 101 | + fmt_ns(ns_now), |
| 102 | + "\u{2014}".into(), |
| 103 | + "new".into(), |
| 104 | + "\u{2728}".into(), |
| 105 | + )), |
| 106 | + Some(ns_base) => { |
| 107 | + let pct = if ns_base != 0.0 { |
| 108 | + (ns_now as f64 - ns_base) / ns_base * 100.0 |
| 109 | + } else { |
| 110 | + 0.0 |
| 111 | + }; |
| 112 | + let mut verdict = "\u{2705}"; |
| 113 | + if pct > threshold_pct { |
| 114 | + verdict = "\u{274c}"; |
| 115 | + regressions.push((name.clone(), ns_base as i64, ns_now, pct)); |
| 116 | + } else if pct > threshold_pct / 2.0 { |
| 117 | + verdict = "\u{26a0}\u{fe0f}"; |
| 118 | + } else if pct < -10.0 { |
| 119 | + verdict = "\u{1f680}"; |
| 120 | + } |
| 121 | + rows.push(( |
| 122 | + name.clone(), |
| 123 | + fmt_ns(ns_now), |
| 124 | + fmt_ns(ns_base as i64), |
| 125 | + format!("{pct:+.1}%"), |
| 126 | + verdict.into(), |
| 127 | + )); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + println!("## Benchmark comparison"); |
| 133 | + println!(); |
| 134 | + println!("Threshold: regression > **{threshold_pct:.0}%** fails CI."); |
| 135 | + println!(); |
| 136 | + println!("| Benchmark | Current | Baseline | \u{0394} | |"); |
| 137 | + println!("|-----------|---------|----------|---|---|"); |
| 138 | + for (a, b, c, d, e) in &rows { |
| 139 | + println!("| `{a}` | {b} | {c} | {d} | {e} |"); |
| 140 | + } |
| 141 | + println!(); |
| 142 | + |
| 143 | + if !regressions.is_empty() { |
| 144 | + println!("### Regressions exceeding threshold"); |
| 145 | + println!(); |
| 146 | + for (name, ns_base, ns_now, pct) in ®ressions { |
| 147 | + let msg = format!( |
| 148 | + "{name}: {} \u{2192} {} ({pct:+.1}%, threshold {threshold_pct:.0}%)", |
| 149 | + fmt_ns(*ns_base), |
| 150 | + fmt_ns(*ns_now), |
| 151 | + ); |
| 152 | + println!("- {msg}"); |
| 153 | + eprintln!("::error::benchmark regression: {msg}"); |
| 154 | + } |
| 155 | + exit(1); |
| 156 | + } |
| 157 | +} |
0 commit comments