use std::hint::black_box;
use criterion::{Criterion, criterion_group, criterion_main};
use inkferro_core::dom::{Arena, apply};
use inkferro_core::input::Parser;
use inkferro_core::render::render_to_string;
use inkferro_core::text::wrap_ansi::wrap_ansi;
#[path = "fixture.rs"]
mod fixture;
fn fixture_path(name: &str) -> std::path::PathBuf {
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../bench/fixtures")
.join(name)
}
fn read_fixture_string(name: &str) -> String {
std::fs::read_to_string(fixture_path(name))
.unwrap_or_else(|e| panic!("read fixture {name}: {e}"))
}
fn read_fixture_bytes(name: &str) -> Vec<u8> {
std::fs::read(fixture_path(name)).unwrap_or_else(|e| panic!("read fixture {name}: {e}"))
}
fn bench_pipeline(c: &mut Criterion) {
let ops = fixture::build_ops();
let golden = read_fixture_string("pipeline_golden.txt");
{
let mut arena = Arena::new();
apply(&mut arena, &ops);
let frame = render_to_string(&arena, fixture::FIXTURE_ROOT, fixture::FIXTURE_WIDTH);
assert_eq!(frame, golden, "pipeline fixture drifted from golden frame");
}
c.bench_function("pipeline", |b| {
b.iter(|| {
let mut arena = Arena::new();
apply(&mut arena, black_box(&ops));
let frame = render_to_string(&arena, fixture::FIXTURE_ROOT, fixture::FIXTURE_WIDTH);
black_box(frame)
});
});
}
fn bench_wrap_ansi(c: &mut Criterion) {
let input = read_fixture_string("wrap_input.txt");
let columns = 30usize;
c.bench_function("wrap_ansi", |b| {
b.iter(|| black_box(wrap_ansi(black_box(&input), black_box(columns))));
});
}
fn bench_input_parse(c: &mut Criterion) {
let script = read_fixture_bytes("input_script.bin");
c.bench_function("input_parse", |b| {
b.iter(|| {
let mut parser = Parser::new();
black_box(parser.feed(black_box(&script)))
});
});
}
criterion_group!(benches, bench_pipeline, bench_wrap_ansi, bench_input_parse);
criterion_main!(benches);