use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use std::hint::black_box;
use std::process::Command;
use std::time::Duration;
#[path = "bench_utils.rs"]
mod bench_utils;
use bench_utils::crush_binary;
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);