forked from ratatui/ratatui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added benchmarks to the block widget to uncover eventual performance issues ratatui#137
- Loading branch information
1 parent
e82521e
commit fb808a1
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use criterion::{black_box, criterion_group, criterion_main, Bencher, BenchmarkId, Criterion}; | ||
use ratatui::{ | ||
buffer::Buffer, | ||
layout::Rect, | ||
widgets::{Block, Borders, Padding, Widget}, | ||
}; | ||
|
||
/// Benchmark for rendering a block. | ||
pub fn block(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("block"); | ||
|
||
// Benchmark that measures the overhead of creating a block separately from rendering | ||
group.bench_function("new", |b| b.iter(|| black_box(Block::new()))); | ||
|
||
for buffer_size in &[ | ||
Rect::new(0, 0, 20, 10), // square-ish | ||
Rect::new(0, 0, 100, 50), // vertically split screen | ||
Rect::new(0, 0, 200, 50), // 1080p fullscreen with medium font | ||
] { | ||
let buffer_size_displayable = &format!("{}x{}", buffer_size.width, buffer_size.height); | ||
|
||
// Render an empty block | ||
group.bench_with_input( | ||
BenchmarkId::new("render_empty", buffer_size_displayable), | ||
&Block::new(), | ||
|b, block| render(b, block, buffer_size), | ||
); | ||
|
||
// Render with default borders | ||
group.bench_with_input( | ||
BenchmarkId::new("render_default_borders", buffer_size_displayable), | ||
&Block::new().borders(Borders::ALL), | ||
|b, block| render(b, block, buffer_size), | ||
); | ||
|
||
// Render with one default title | ||
group.bench_with_input( | ||
BenchmarkId::new("render_default_title", buffer_size_displayable), | ||
&Block::new().title("default title"), | ||
|b, block| render(b, block, buffer_size), | ||
); | ||
|
||
// Render with padding | ||
group.bench_with_input( | ||
BenchmarkId::new("render_padding", buffer_size_displayable), | ||
&Block::new().padding(Padding::new(5, 5, 2, 2)), | ||
|b, block| render(b, block, buffer_size), | ||
); | ||
|
||
// Calculate inner area | ||
group.bench_with_input( | ||
BenchmarkId::new("compute_inner_area", buffer_size_displayable), | ||
&Block::new(), | ||
|b, block| b.iter(|| block.inner(*buffer_size)), | ||
); | ||
} | ||
|
||
group.finish(); | ||
} | ||
|
||
/// render the block into a buffer of the given `size` | ||
fn render(bencher: &mut Bencher, block: &Block, size: &Rect) { | ||
let mut buffer = Buffer::empty(*size); | ||
bencher.iter(|| { | ||
block.clone().render(buffer.area, &mut buffer); | ||
}) | ||
} | ||
|
||
criterion_group!(benches, block); | ||
criterion_main!(benches); |