use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use std::hint::black_box;
use std::process::Command;
use std::time::Duration;
fn crush_binary() -> std::path::PathBuf {
let output = Command::new("cargo")
.args(["build", "--release", "--bin", "crush"])
.output()
.expect("Failed to build crush binary");
if !output.status.success() {
panic!(
"Failed to build crush: {}",
String::from_utf8_lossy(&output.stderr)
);
}
let mut path = std::env::current_dir().expect("Failed to get current dir");
if path.ends_with("crush-cli") {
path.pop();
}
path.push("target");
path.push("release");
path.push("crush");
#[cfg(windows)]
path.set_extension("exe");
if !path.exists() {
panic!("Crush binary not found at: {}", path.display());
}
path
}
fn bench_root_help(c: &mut Criterion) {
let binary = crush_binary();
let mut group = c.benchmark_group("help_command");
group.measurement_time(Duration::from_secs(10));
group.sample_size(100);
group.bench_function("root_help", |b| {
b.iter(|| {
let output = Command::new(&binary)
.arg("--help")
.output()
.expect("Failed to run crush --help");
assert!(!output.stdout.is_empty(), "Help output should not be empty");
black_box(output)
})
});
group.finish();
}
fn bench_subcommand_help(c: &mut Criterion) {
let binary = crush_binary();
let mut group = c.benchmark_group("help_command");
group.measurement_time(Duration::from_secs(10));
group.sample_size(100);
let subcommands = ["compress", "decompress", "inspect", "config", "plugins"];
for subcommand in subcommands.iter() {
group.bench_with_input(
BenchmarkId::new("subcommand", subcommand),
subcommand,
|b, &cmd| {
b.iter(|| {
let output = Command::new(&binary)
.arg(cmd)
.arg("--help")
.output()
.unwrap_or_else(|_| panic!("Failed to run crush {} --help", cmd));
assert!(!output.stdout.is_empty(), "Help output should not be empty");
black_box(output)
})
},
);
}
group.finish();
}
fn bench_help_subcommand(c: &mut Criterion) {
let binary = crush_binary();
let mut group = c.benchmark_group("help_command");
group.measurement_time(Duration::from_secs(10));
group.sample_size(100);
let commands = ["compress", "decompress", "inspect"];
for command in commands.iter() {
group.bench_with_input(BenchmarkId::new("help_cmd", command), command, |b, &cmd| {
b.iter(|| {
let output = Command::new(&binary)
.arg("help")
.arg(cmd)
.output()
.unwrap_or_else(|_| panic!("Failed to run crush help {}", cmd));
black_box(output)
})
});
}
group.finish();
}
fn bench_short_vs_long_help(c: &mut Criterion) {
let binary = crush_binary();
let mut group = c.benchmark_group("help_command");
group.measurement_time(Duration::from_secs(10));
group.sample_size(100);
group.bench_function("short_flag", |b| {
b.iter(|| {
let output = Command::new(&binary)
.arg("-h")
.output()
.expect("Failed to run crush -h");
black_box(output)
})
});
group.bench_function("long_flag", |b| {
b.iter(|| {
let output = Command::new(&binary)
.arg("--help")
.output()
.expect("Failed to run crush --help");
black_box(output)
})
});
group.finish();
}
criterion_group!(
benches,
bench_root_help,
bench_subcommand_help,
bench_help_subcommand,
bench_short_vs_long_help
);
criterion_main!(benches);