use std::hint::black_box;
use std::io::Write;
use cli_forge::{define_tag, style, tag};
use criterion::{Criterion, criterion_group, criterion_main};
fn force_truecolor() {
unsafe {
std::env::set_var("CLICOLOR_FORCE", "1");
std::env::set_var("COLORTERM", "truecolor");
}
}
fn output_benches(c: &mut Criterion) {
force_truecolor();
define_tag("bench-error", style("").red().bold());
let mut buffer: Vec<u8> = Vec::with_capacity(256);
c.bench_function("plain_write", |b| {
b.iter(|| {
buffer.clear();
let _ = writeln!(buffer, "{}", black_box("deploying release artifacts"));
black_box(&buffer);
});
});
c.bench_function("builder_render_unstyled", |b| {
b.iter(|| black_box(style(black_box("deploying release artifacts")).render()));
});
c.bench_function("builder_render_styled", |b| {
b.iter(|| {
black_box(
style(black_box("deploying release artifacts"))
.red()
.bold()
.render(),
)
});
});
c.bench_function("builder_render_rgb", |b| {
b.iter(|| black_box(style(black_box("status: ok")).rgb(0, 200, 120).render()));
});
c.bench_function("registry_render", |b| {
b.iter(|| black_box(tag(black_box("bench-error")).render_with(black_box("build failed"))));
});
}
fn parse_benches(c: &mut Criterion) {
use cli_forge::{App, Arg, Command};
let mut app = App::new("bench").version("1.0.0");
app.register(
Command::new("build")
.arg(Arg::flag("release").short('r'))
.arg(Arg::count("verbose").short('v'))
.arg(Arg::option("jobs").short('j').default("1"))
.arg(Arg::option("define").short('D').multiple(true))
.arg(Arg::positional("targets").multiple(true))
.run(|_| {}),
);
c.bench_function("parse_simple", |b| {
b.iter(|| black_box(app.try_parse_from(black_box(["build", "-r"]))));
});
c.bench_function("parse_rich", |b| {
b.iter(|| {
black_box(app.try_parse_from(black_box([
"build",
"-vvv",
"--release",
"-D",
"A",
"-D",
"B",
"-j",
"8",
"a.rs",
"b.rs",
])))
});
});
}
criterion_group!(benches, output_benches, parse_benches);
criterion_main!(benches);