use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use dotmax::BrailleGrid;
use std::hint::black_box;
fn bench_grid_creation(c: &mut Criterion) {
let mut group = c.benchmark_group("grid_creation");
let sizes = [
(40, 12, "40x12_small"),
(80, 24, "80x24_standard"),
(160, 48, "160x48_large"),
(200, 50, "200x50_max"),
];
for (width, height, label) in sizes {
group.throughput(Throughput::Elements(1));
group.bench_with_input(BenchmarkId::new("new", label), &(width, height), |b, &(w, h)| {
b.iter(|| black_box(BrailleGrid::new(w, h).unwrap()));
});
}
group.finish();
}
fn bench_grid_clear(c: &mut Criterion) {
let mut group = c.benchmark_group("grid_clear");
let sizes = [
(40, 12, "40x12_small"),
(80, 24, "80x24_standard"),
(160, 48, "160x48_large"),
(200, 50, "200x50_max"),
];
for (width, height, label) in sizes {
let mut grid = BrailleGrid::new(width, height).unwrap();
for y in 0..(height * 4).min(100) {
for x in 0..(width * 2).min(100) {
let _ = grid.set_dot(x, y);
}
}
group.bench_with_input(BenchmarkId::new("clear", label), &(), |b, _| {
b.iter(|| {
grid.clear();
black_box(&grid);
});
});
}
group.finish();
}
fn bench_dot_operations(c: &mut Criterion) {
let mut group = c.benchmark_group("dot_operations");
group.throughput(Throughput::Elements(1000));
group.bench_function("set_dot_1000_ops", |b| {
let mut grid = BrailleGrid::new(80, 24).unwrap();
b.iter(|| {
for i in 0..1000 {
let x = (i * 7) % 160; let y = (i * 13) % 96; let _ = grid.set_dot(x, y);
}
black_box(&grid);
});
});
group.throughput(Throughput::Elements(10000));
group.bench_function("set_dot_10000_ops", |b| {
let mut grid = BrailleGrid::new(200, 50).unwrap(); b.iter(|| {
for i in 0..10000 {
let x = (i * 7) % 400; let y = (i * 13) % 200; let _ = grid.set_dot(x, y);
}
black_box(&grid);
});
});
group.throughput(Throughput::Elements(1000));
group.bench_function("mixed_set_get_1000_ops", |b| {
let mut grid = BrailleGrid::new(80, 24).unwrap();
b.iter(|| {
for i in 0..500 {
let x = (i * 7) % 160;
let y = (i * 13) % 96;
let _ = grid.set_dot(x, y);
}
for i in 0..500 {
let cell_x = (i * 7) % 80;
let cell_y = (i * 13) % 24;
black_box(grid.cell_to_braille_char(cell_x, cell_y).unwrap());
}
});
});
group.finish();
}
fn bench_unicode_conversion(c: &mut Criterion) {
let mut group = c.benchmark_group("unicode_conversion");
group.bench_function("cell_to_braille_char_single", |b| {
let mut grid = BrailleGrid::new(80, 24).unwrap();
let _ = grid.set_dot(0, 0);
let _ = grid.set_dot(1, 1);
let _ = grid.set_dot(0, 2);
b.iter(|| black_box(grid.cell_to_braille_char(0, 0).unwrap()));
});
group.throughput(Throughput::Elements(1920)); group.bench_function("to_char_80x24_full_grid", |b| {
let mut grid = BrailleGrid::new(80, 24).unwrap();
for y in 0..24 {
for x in 0..80 {
let _ = grid.set_dot(x * 2, y * 4);
}
}
b.iter(|| {
for y in 0..24 {
for x in 0..80 {
black_box(grid.cell_to_braille_char(x, y).unwrap());
}
}
});
});
group.bench_function("to_unicode_grid_80x24", |b| {
let mut grid = BrailleGrid::new(80, 24).unwrap();
for y in 0..24 {
for x in 0..80 {
let _ = grid.set_dot(x * 2, y * 4);
}
}
b.iter(|| black_box(grid.to_unicode_grid()));
});
group.throughput(Throughput::Elements(10000)); group.bench_function("to_unicode_grid_200x50", |b| {
let mut grid = BrailleGrid::new(200, 50).unwrap();
for y in 0..50 {
for x in 0..200 {
let _ = grid.set_dot(x * 2, y * 4);
}
}
b.iter(|| black_box(grid.to_unicode_grid()));
});
group.finish();
}
fn bench_combined_operations(c: &mut Criterion) {
let mut group = c.benchmark_group("combined_operations");
group.bench_function("create_draw_convert_80x24", |b| {
b.iter(|| {
let mut grid = BrailleGrid::new(80, 24).unwrap();
for dy in 0..8 {
for dx in 0..4 {
let _ = grid.set_dot(80 + dx, 48 + dy);
}
}
let chars = grid.to_unicode_grid();
black_box(chars);
});
});
group.bench_function("clear_redraw_convert_80x24", |b| {
let mut grid = BrailleGrid::new(80, 24).unwrap();
b.iter(|| {
grid.clear();
for dy in 0..8 {
for dx in 0..4 {
let _ = grid.set_dot(82 + dx, 50 + dy); }
}
let chars = grid.to_unicode_grid();
black_box(chars);
});
});
group.finish();
}
criterion_group!(
benches,
bench_grid_creation,
bench_grid_clear,
bench_dot_operations,
bench_unicode_conversion,
bench_combined_operations
);
criterion_main!(benches);