use anyhow::Result;
use awesome_sails_benchmarks::{BenchStorage, ComparisonConfig, ReportBuilder};
use clap::Parser;
use std::{fs, path::PathBuf};
#[derive(Parser)]
#[command(
name = "bench-analyzer",
about = "A CLI tool to compare two benchmark JSON files and detect performance regressions."
)]
struct Cli {
#[arg(long)]
current: PathBuf,
#[arg(long)]
other: PathBuf,
#[arg(long)]
output: Option<PathBuf>,
#[arg(long)]
threshold: Option<f64>,
#[arg(long)]
fail_on_regression: bool,
#[arg(long, requires = "threshold")]
strict: bool,
}
fn main() -> Result<()> {
let cli = Cli::parse();
let storage_cur = BenchStorage::at_path(&cli.current);
let storage_oth = BenchStorage::at_path(&cli.other);
let mut config = ComparisonConfig::default();
if let Some(t) = cli.threshold {
config.regressed_strong = t;
}
let diffs = storage_cur.compare(&storage_oth, &config)?;
let report = ReportBuilder::new(diffs).with_config(config).build();
println!("{}", report.as_ascii());
if let Some(out_path) = cli.output {
fs::write(out_path, report.as_markdown().to_string())?;
}
if cli.fail_on_regression || cli.strict {
let failed = if cli.strict {
report.has_any_deviation(cli.threshold.unwrap())
} else {
report.has_significant_regression()
};
if failed {
eprintln!("\nError: Benchmark performance regression detected!");
std::process::exit(1);
}
}
Ok(())
}