use std::path::{Path, PathBuf};
use anyhow::Result;
use clap::Args;
use crate::builder::platforms::benches::BenchesBuilder;
use crate::builder::{BuildContext, BuildOptions, PlatformBuilder};
use crate::commands::build::{BuildTarget, LinkType, WindowsToolchain};
use crate::config::CcgoConfig;
use crate::testing::benchmark::{BenchmarkStore, ComparisonReport};
use crate::testing::ci::{CiFormat, CiReporter};
#[derive(Args, Debug)]
pub struct BenchCommand {
#[arg(long)]
pub filter: Option<String>,
#[arg(long)]
pub ide_project: bool,
#[arg(long)]
pub build_only: bool,
#[arg(long)]
pub run_only: bool,
#[arg(short, long)]
pub jobs: Option<usize>,
#[arg(long, default_value = "true")]
pub release: bool,
#[arg(long)]
pub profile: Option<String>,
#[arg(long)]
pub save: Option<String>,
#[arg(long)]
pub baseline: Option<String>,
#[arg(long)]
pub compare_latest: bool,
#[arg(long, default_value = "5.0")]
pub threshold: f64,
#[arg(long)]
pub fail_on_regression: bool,
#[arg(long)]
pub ci_format: Option<String>,
#[arg(long)]
pub list_runs: bool,
#[arg(long)]
pub export_markdown: Option<PathBuf>,
#[arg(long)]
pub export_json: Option<PathBuf>,
#[arg(long, conflicts_with = "tsan")]
pub asan: bool,
#[arg(long, conflicts_with = "asan")]
pub tsan: bool,
}
impl BenchCommand {
pub fn execute(self, verbose: bool) -> Result<()> {
if self.list_runs {
return self.list_saved_runs();
}
let config = CcgoConfig::load()?;
let project_root = std::env::current_dir()?;
self.validate_profile()?;
let options = self.build_options(verbose);
let ctx = BuildContext::new_with_platform_name(
project_root.clone(),
config,
options,
"benches".to_string(),
);
let builder = BenchesBuilder::new();
let build_dir = ctx.cmake_build_dir.clone();
if self.ide_project {
return builder.generate_ide_project(&ctx);
}
if !self.run_only {
if verbose {
eprintln!("Building benchmarks...");
}
builder.build(&ctx)?;
}
if !self.build_only {
if verbose {
eprintln!("Running benchmarks...");
}
builder.run_benchmarks(&ctx, self.filter.as_deref())?;
self.process_results(&project_root, &build_dir, verbose)?;
}
Ok(())
}
fn validate_profile(&self) -> Result<()> {
if let Some(ref profile) = self.profile {
if matches!(profile.as_str(), "debug" | "release") {
anyhow::bail!(
"'--profile {profile}' is reserved; use --debug or --release instead"
);
}
}
Ok(())
}
fn build_options(&self, verbose: bool) -> BuildOptions {
let sanitizer = if self.asan {
Some(crate::builder::sanitizer::SanitizerKind::Address)
} else if self.tsan {
Some(crate::builder::sanitizer::SanitizerKind::Thread)
} else {
None
};
BuildOptions {
target: BuildTarget::Linux,
architectures: vec![],
link_type: LinkType::All,
use_docker: false,
auto_docker: false,
jobs: self.jobs,
ide_project: self.ide_project,
release: self.release,
native_only: false,
toolchain: WindowsToolchain::All,
verbose,
dev: false,
features: vec![],
use_default_features: true,
all_features: false,
cache: Some("auto".to_string()),
analytics: false,
linkage_default: None,
linkage_overrides: std::collections::HashMap::new(),
sanitizer,
profile_name: self.profile.clone(),
..BuildOptions::default()
}
}
fn list_saved_runs(&self) -> Result<()> {
let store = BenchmarkStore::new(BenchmarkStore::default_path());
let runs = store.list()?;
if runs.is_empty() {
println!("No saved benchmark runs found.");
println!("Use --save <id> to save benchmark results.");
} else {
println!("Saved benchmark runs:");
for run_id in runs {
println!(" - {}", run_id);
}
}
Ok(())
}
fn process_results(&self, project_root: &Path, build_dir: &Path, verbose: bool) -> Result<()> {
let store = BenchmarkStore::new(project_root.join(".ccgo").join("benchmarks"));
let Some(json_path) = self.find_benchmark_json(build_dir)? else {
if verbose {
eprintln!(
"Warning: No benchmark JSON output found in {}",
build_dir.display()
);
}
return Ok(());
};
let mut current_run = store.parse_gbench_json(&json_path)?;
self.assign_run_id(&mut current_run);
if self.save.is_some() {
let saved_path = store.save(¤t_run)?;
eprintln!("📁 Benchmark results saved: {}", saved_path.display());
}
if let Some(report) = self.comparison(&store, ¤t_run)? {
self.report_comparison(&report, verbose)?;
}
Ok(())
}
fn assign_run_id(&self, run: &mut crate::testing::benchmark::BenchmarkRun) {
run.run_id = self
.save
.clone()
.unwrap_or_else(|| chrono::Local::now().format("%Y%m%d_%H%M%S").to_string());
}
fn comparison(
&self,
store: &BenchmarkStore,
current: &crate::testing::benchmark::BenchmarkRun,
) -> Result<Option<ComparisonReport>> {
if let Some(ref baseline_id) = self.baseline {
return Ok(Some(self.compare_with_baseline(store, baseline_id, current)?));
}
if !self.compare_latest {
return Ok(None);
}
let Ok(baseline) = store.load_latest() else {
return Ok(None);
};
if baseline.run_id == current.run_id {
return Ok(None);
}
Ok(Some(ComparisonReport::new(
&baseline,
current,
self.threshold,
)))
}
fn find_benchmark_json(&self, build_dir: &Path) -> Result<Option<PathBuf>> {
if !build_dir.exists() {
return Ok(None);
}
let mut latest: Option<(PathBuf, std::time::SystemTime)> = None;
for entry in std::fs::read_dir(build_dir)? {
let entry = entry?;
let path = entry.path();
if path.is_file() {
let name = path.file_name().unwrap().to_string_lossy();
if name.contains("_result") && name.ends_with(".json") {
if let Ok(metadata) = path.metadata() {
if let Ok(modified) = metadata.modified() {
match &latest {
None => latest = Some((path, modified)),
Some((_, latest_time)) if modified > *latest_time => {
latest = Some((path, modified))
}
_ => {}
}
}
}
}
}
}
Ok(latest.map(|(p, _)| p))
}
fn compare_with_baseline(
&self,
store: &BenchmarkStore,
baseline_id: &str,
current: &crate::testing::benchmark::BenchmarkRun,
) -> Result<ComparisonReport> {
let baseline = store.load(baseline_id)?;
Ok(ComparisonReport::new(&baseline, current, self.threshold))
}
fn report_comparison(&self, report: &ComparisonReport, _verbose: bool) -> Result<()> {
report.print_report();
if let Some(ref md_path) = self.export_markdown {
std::fs::write(md_path, report.to_markdown())?;
eprintln!("📄 Markdown report: {}", md_path.display());
}
if let Some(ref json_path) = self.export_json {
std::fs::write(json_path, report.to_json()?)?;
eprintln!("📄 JSON report: {}", json_path.display());
}
if self.ci_format.is_some() {
let format = if let Some(ref fmt) = self.ci_format {
fmt.parse::<CiFormat>()?
} else {
CiFormat::detect()
};
let reporter = CiReporter::new(format);
reporter.report_benchmarks(report);
}
if self.fail_on_regression && report.has_regressions() {
anyhow::bail!(
"{} benchmark regression(s) detected (threshold: {:.1}%)",
report.regressions,
self.threshold
);
}
Ok(())
}
}