ratatui
ratatui copied to clipboard
Test: Add benchmarking for widgets
Problem
There might be performance issues/concerns about tui widgets and adding benchmarks is important to uncover them.
Solution
Add benches for widgets. See #22 for an example.
Alternatives
None.
Additional context
#22
I've looked at the linked issue, and tried to add a bench (for Block
, just to try).
#[bench]
fn bench_title_rounded_corners(b: &mut test::Bencher) {
b.iter(|| {
Block::default()
.borders(Borders::ALL)
.title("Main block with round corners")
.title_alignment(Alignment::Center)
.border_type(BorderType::Rounded)
})
}
Does this look okay? I just took one of the example usages of Block
in the repo, and I put it in a bench
. Am I missing something? Is this the right direction for writing benches fi the widgets?
It's a good start. Most of the work for widgets is done in rendering, so I think it would be a good idea to set up a buffer outside of the measured func, and render to that buffer in the measured code.
When benching the rendering of widgets, should I use the TestBackend
?
Or should I create different rendering benches for crossterm
and termion
?
I don't know if you even need a backend tbh - doing something like
let area = Rect{...}; // put in size info
let buf = Buffer::empty(area);
b.iter(|| {
let w = Block... // set up test
w.render(area, buf);
});
Should capture the time it takes to render, and doesn't induce any backend related overhead.
(you may need to twiddle some of above, buf may need to be cloned for example, but that's the approach I'd start with).
Got it. Thanks!
Are you working on this @Sufilevy (or anyone else) ? Otherwise I'm interested in submitting a PR.
You're welcome to!
Most of the work for widgets is done in rendering, so I think it would be a good idea to set up a buffer outside of the measured func, and render to that buffer in the measured code.
Most widgets are created on every render, so I think they should also be included in the benchmarks. Either they are fast and no problem or an issue shows up.