ratatui icon indicating copy to clipboard operation
ratatui copied to clipboard

Test: Add benchmarking for widgets

Open orhun opened this issue 1 year ago • 8 comments

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

orhun avatar Apr 15 '23 15:04 orhun

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?

Sufilevy avatar Apr 30 '23 17:04 Sufilevy

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.

sophacles avatar May 02 '23 12:05 sophacles

When benching the rendering of widgets, should I use the TestBackend? Or should I create different rendering benches for crossterm and termion?

Sufilevy avatar May 02 '23 15:05 Sufilevy

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).

sophacles avatar May 02 '23 15:05 sophacles

Got it. Thanks!

Sufilevy avatar May 02 '23 16:05 Sufilevy

Are you working on this @Sufilevy (or anyone else) ? Otherwise I'm interested in submitting a PR.

Valentin271 avatar Aug 04 '23 13:08 Valentin271

You're welcome to!

Sufilevy avatar Aug 04 '23 13:08 Sufilevy

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.

EdJoPaTo avatar Mar 04 '24 16:03 EdJoPaTo