mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-21 20:23:11 +00:00
chore: add benchmark for Table
(#1408)
This commit is contained in:
parent
1153a9ebaf
commit
c32baa7cd8
2 changed files with 72 additions and 1 deletions
|
@ -7,6 +7,7 @@ pub mod main {
|
|||
pub mod paragraph;
|
||||
pub mod rect;
|
||||
pub mod sparkline;
|
||||
pub mod table;
|
||||
}
|
||||
pub use main::*;
|
||||
|
||||
|
@ -18,5 +19,6 @@ criterion::criterion_main!(
|
|||
list::benches,
|
||||
paragraph::benches,
|
||||
rect::benches,
|
||||
sparkline::benches
|
||||
sparkline::benches,
|
||||
table::benches,
|
||||
);
|
||||
|
|
69
benches/main/table.rs
Normal file
69
benches/main/table.rs
Normal file
|
@ -0,0 +1,69 @@
|
|||
use criterion::{criterion_group, BatchSize, Bencher, BenchmarkId, Criterion};
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
layout::{Constraint, Rect},
|
||||
widgets::{Row, StatefulWidget, Table, TableState, Widget},
|
||||
};
|
||||
|
||||
/// Benchmark for rendering a table.
|
||||
/// It only benchmarks the render with a different number of rows, and columns.
|
||||
fn table(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("table");
|
||||
|
||||
for row_count in [64, 2048, 16384] {
|
||||
for col_count in [2, 4, 8] {
|
||||
let bench_sizes = format!("{row_count}x{col_count}");
|
||||
let rows: Vec<Row> = (0..row_count)
|
||||
.map(|_| Row::new((0..col_count).map(|_| fakeit::words::quote())))
|
||||
.collect();
|
||||
|
||||
// Render default table
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("render", &bench_sizes),
|
||||
&Table::new(rows.clone(), [] as [Constraint; 0]),
|
||||
render,
|
||||
);
|
||||
|
||||
// Render with an offset to the middle of the table and a selected row
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("render_scroll_half", &bench_sizes),
|
||||
&Table::new(rows, [] as [Constraint; 0]).highlight_symbol(">>"),
|
||||
|b, table| {
|
||||
render_stateful(
|
||||
b,
|
||||
table,
|
||||
TableState::default()
|
||||
.with_offset(row_count / 2)
|
||||
.with_selected(Some(row_count / 2)),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn render(bencher: &mut Bencher, table: &Table) {
|
||||
let mut buffer = Buffer::empty(Rect::new(0, 0, 200, 50));
|
||||
bencher.iter_batched(
|
||||
|| table.to_owned(),
|
||||
|bench_table| {
|
||||
Widget::render(bench_table, buffer.area, &mut buffer);
|
||||
},
|
||||
BatchSize::LargeInput,
|
||||
);
|
||||
}
|
||||
|
||||
fn render_stateful(bencher: &mut Bencher, table: &Table, mut state: TableState) {
|
||||
let mut buffer = Buffer::empty(Rect::new(0, 0, 200, 50));
|
||||
bencher.iter_batched(
|
||||
|| table.to_owned(),
|
||||
|bench_table| {
|
||||
StatefulWidget::render(bench_table, buffer.area, &mut buffer, &mut state);
|
||||
},
|
||||
BatchSize::LargeInput,
|
||||
);
|
||||
}
|
||||
|
||||
criterion_group!(benches, table);
|
Loading…
Reference in a new issue