2024-04-21 17:29:37 +00:00
|
|
|
use criterion::{criterion_group, criterion_main, BatchSize, Bencher, Criterion};
|
2023-08-05 14:47:06 +00:00
|
|
|
use ratatui::{
|
|
|
|
buffer::Buffer,
|
2024-04-21 17:29:37 +00:00
|
|
|
layout::{Alignment, Rect},
|
2023-08-05 14:47:06 +00:00
|
|
|
widgets::{
|
|
|
|
block::{Position, Title},
|
|
|
|
Block, Borders, Padding, Widget,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Benchmark for rendering a block.
|
2024-03-02 09:06:53 +00:00
|
|
|
fn block(c: &mut Criterion) {
|
2023-08-05 14:47:06 +00:00
|
|
|
let mut group = c.benchmark_group("block");
|
|
|
|
|
2024-04-21 17:29:37 +00:00
|
|
|
for (width, height) in [
|
|
|
|
(100, 50), // vertically split screen
|
|
|
|
(200, 50), // 1080p fullscreen with medium font
|
|
|
|
(256, 256), // Max sized area
|
2023-08-05 14:47:06 +00:00
|
|
|
] {
|
2024-04-21 17:29:37 +00:00
|
|
|
let buffer_size = Rect::new(0, 0, width, height);
|
2023-08-05 14:47:06 +00:00
|
|
|
|
|
|
|
// Render an empty block
|
|
|
|
group.bench_with_input(
|
2024-04-21 17:29:37 +00:00
|
|
|
format!("render_empty/{width}x{height}"),
|
2023-08-05 14:47:06 +00:00
|
|
|
&Block::new(),
|
|
|
|
|b, block| render(b, block, buffer_size),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Render with all features
|
|
|
|
group.bench_with_input(
|
2024-04-21 17:29:37 +00:00
|
|
|
format!("render_all_feature/{width}x{height}"),
|
2023-08-05 14:47:06 +00:00
|
|
|
&Block::new()
|
|
|
|
.borders(Borders::ALL)
|
|
|
|
.title("test title")
|
|
|
|
.title(
|
|
|
|
Title::from("bottom left title")
|
|
|
|
.alignment(Alignment::Right)
|
|
|
|
.position(Position::Bottom),
|
|
|
|
)
|
|
|
|
.padding(Padding::new(5, 5, 2, 2)),
|
|
|
|
|b, block| render(b, block, buffer_size),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
group.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// render the block into a buffer of the given `size`
|
2024-03-02 09:06:53 +00:00
|
|
|
fn render(bencher: &mut Bencher, block: &Block, size: Rect) {
|
|
|
|
let mut buffer = Buffer::empty(size);
|
2023-08-11 02:17:14 +00:00
|
|
|
// We use `iter_batched` to clone the value in the setup function.
|
|
|
|
// See https://github.com/ratatui-org/ratatui/pull/377.
|
|
|
|
bencher.iter_batched(
|
|
|
|
|| block.to_owned(),
|
|
|
|
|bench_block| {
|
|
|
|
bench_block.render(buffer.area, &mut buffer);
|
|
|
|
},
|
|
|
|
BatchSize::SmallInput,
|
2024-03-02 09:06:53 +00:00
|
|
|
);
|
2023-08-05 14:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, block);
|
|
|
|
criterion_main!(benches);
|