mirror of
https://github.com/ratatui/ratatui.git
synced 2025-09-26 20:40:44 +00:00
test(bench): Added a benchmark for constraints (#2043)
I've added a new benchmark for constraints, which only takes into account the time it takes to generate a full layout using a single type of constraints only. Avoided rendering here as it resulted in more inaccurate benchmarks, and i believe it should be separated nonetheless.
This commit is contained in:
parent
75b78be09f
commit
a21501f7f4
@ -2,6 +2,7 @@ pub mod main {
|
|||||||
pub mod barchart;
|
pub mod barchart;
|
||||||
pub mod block;
|
pub mod block;
|
||||||
pub mod buffer;
|
pub mod buffer;
|
||||||
|
pub mod constraints;
|
||||||
pub mod line;
|
pub mod line;
|
||||||
pub mod list;
|
pub mod list;
|
||||||
pub mod paragraph;
|
pub mod paragraph;
|
||||||
@ -21,4 +22,5 @@ criterion::criterion_main!(
|
|||||||
rect::benches,
|
rect::benches,
|
||||||
sparkline::benches,
|
sparkline::benches,
|
||||||
table::benches,
|
table::benches,
|
||||||
|
constraints::benches,
|
||||||
);
|
);
|
||||||
|
60
ratatui/benches/main/constraints.rs
Normal file
60
ratatui/benches/main/constraints.rs
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
use std::hint::black_box;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use criterion::{Criterion, criterion_group};
|
||||||
|
use ratatui::layout::Constraint::{Fill, Length, Max, Min, Percentage, Ratio};
|
||||||
|
use ratatui::layout::{Layout, Rect};
|
||||||
|
|
||||||
|
const SPLIT_BY: u16 = 10;
|
||||||
|
|
||||||
|
fn layout_split(criterion: &mut Criterion) {
|
||||||
|
for size in [16, 64, 256] {
|
||||||
|
let mut group = criterion.benchmark_group(format!("constraints {size}x{size}"));
|
||||||
|
let area = Rect::new(0, 0, size, size);
|
||||||
|
group.bench_function("Fill", |bencher| {
|
||||||
|
bencher.iter(|| layout_fill(black_box(area)));
|
||||||
|
});
|
||||||
|
group.bench_function("Length", |bencher| {
|
||||||
|
bencher.iter(|| layout_length(black_box(area)));
|
||||||
|
});
|
||||||
|
group.bench_function("Max", |bencher| {
|
||||||
|
bencher.iter(|| layout_max(black_box(area)));
|
||||||
|
});
|
||||||
|
group.bench_function("Min", |bencher| {
|
||||||
|
bencher.iter(|| layout_min(black_box(area)));
|
||||||
|
});
|
||||||
|
group.bench_function("Percentage", |bencher| {
|
||||||
|
bencher.iter(|| layout_percentage(black_box(area)));
|
||||||
|
});
|
||||||
|
group.bench_function("Ratio", |bencher| {
|
||||||
|
bencher.iter(|| layout_ratio(black_box(area)));
|
||||||
|
});
|
||||||
|
group.finish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layout_fill(area: Rect) -> Rc<[Rect]> {
|
||||||
|
Layout::vertical([Fill(1); SPLIT_BY as usize]).split(area)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layout_length(area: Rect) -> Rc<[Rect]> {
|
||||||
|
Layout::vertical([Length(area.width / SPLIT_BY); SPLIT_BY as usize]).split(area)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layout_max(area: Rect) -> Rc<[Rect]> {
|
||||||
|
Layout::vertical([Max(area.width / SPLIT_BY); SPLIT_BY as usize]).split(area)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layout_min(area: Rect) -> Rc<[Rect]> {
|
||||||
|
Layout::vertical([Min(area.width / SPLIT_BY); SPLIT_BY as usize]).split(area)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layout_percentage(area: Rect) -> Rc<[Rect]> {
|
||||||
|
Layout::vertical([Percentage(100 / SPLIT_BY); SPLIT_BY as usize]).split(area)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layout_ratio(area: Rect) -> Rc<[Rect]> {
|
||||||
|
Layout::vertical([Ratio(1, SPLIT_BY.into()); SPLIT_BY as usize]).split(area)
|
||||||
|
}
|
||||||
|
|
||||||
|
criterion_group!(benches, layout_split);
|
Loading…
x
Reference in New Issue
Block a user