test(gauge): add benchmarks for gauge (#2221)

This commit is contained in:
WaterWhisperer
2025-11-24 03:37:55 +08:00
committed by GitHub
parent 500f6c5a52
commit 94ba82e9ca
8 changed files with 66 additions and 6 deletions

View File

@@ -3,6 +3,7 @@ pub mod main {
pub mod block;
pub mod buffer;
pub mod constraints;
pub mod gauge;
pub mod line;
pub mod list;
pub mod paragraph;
@@ -25,4 +26,5 @@ criterion::criterion_main!(
table::benches,
text::benches,
constraints::benches,
gauge::benches,
);

View File

@@ -52,7 +52,7 @@ fn barchart(c: &mut Criterion) {
group.finish();
}
/// Render the widget in a classical size buffer
/// Render the widget in a classical size buffer.
fn render(bencher: &mut Bencher, barchart: &BarChart) {
let mut buffer = Buffer::empty(Rect::new(0, 0, 200, 50));
// We use `iter_batched` to clone the value in the setup function.

View File

@@ -36,7 +36,7 @@ fn block(c: &mut Criterion) {
group.finish();
}
/// render the block into a buffer of the given `size`
/// 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);
// We use `iter_batched` to clone the value in the setup function.

View File

@@ -0,0 +1,58 @@
use criterion::{BatchSize, Criterion, criterion_group};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::Style;
use ratatui::widgets::{Block, Gauge, Widget};
/// Benchmark for rendering a gauge.
fn gauge(c: &mut Criterion) {
let mut group = c.benchmark_group("gauge");
let (width, height) = (200, 50); // 1080p fullscreen with medium font
let buffer_size = Rect::new(0, 0, width, height);
// Render an empty gauge
group.bench_with_input(
format!("render_empty/{width}x{height}"),
&Gauge::default(),
|b, gauge| {
let mut buffer = Buffer::empty(buffer_size);
// We use `iter_batched` to clone the value in the setup function because
// `Widget::render` consumes the widget.
b.iter_batched(
|| gauge.to_owned(),
|bench_gauge| {
bench_gauge.render(buffer.area, &mut buffer);
},
BatchSize::SmallInput,
);
},
);
// Render with all features
group.bench_with_input(
format!("render_all_feature/{width}x{height}"),
&Gauge::default()
.block(Block::bordered().title("Progress"))
.gauge_style(Style::new().white().on_black().italic())
.percent(20)
.label("20%")
.use_unicode(true),
|b, gauge| {
let mut buffer = Buffer::empty(buffer_size);
// We use `iter_batched` to clone the value in the setup function because
// `Widget::render` consumes the widget.
b.iter_batched(
|| gauge.to_owned(),
|bench_gauge| {
bench_gauge.render(buffer.area, &mut buffer);
},
BatchSize::SmallInput,
);
},
);
group.finish();
}
criterion_group!(benches, gauge);

View File

@@ -39,7 +39,7 @@ fn list(c: &mut Criterion) {
group.finish();
}
/// render the list into a common size buffer
/// Render the list into a common size buffer.
fn render(bencher: &mut Bencher, list: &List) {
let mut buffer = Buffer::empty(Rect::new(0, 0, 200, 50));
// We use `iter_batched` to clone the value in the setup function.

View File

@@ -67,7 +67,7 @@ fn paragraph(c: &mut Criterion) {
group.finish();
}
/// render the paragraph into a buffer with the given width
/// Render the paragraph into a buffer with the given width.
fn render(bencher: &mut Bencher, paragraph: &Paragraph, width: u16) {
let mut buffer = Buffer::empty(Rect::new(0, 0, width, PARAGRAPH_DEFAULT_HEIGHT));
// We use `iter_batched` to clone the value in the setup function.

View File

@@ -25,7 +25,7 @@ fn sparkline(c: &mut Criterion) {
group.finish();
}
/// render the block into a buffer of the given `size`
/// Render the block into a buffer of the given `size`.
fn render(bencher: &mut Bencher, sparkline: &Sparkline) {
let mut buffer = Buffer::empty(Rect::new(0, 0, 200, 50));
// We use `iter_batched` to clone the value in the setup function.

View File

@@ -46,7 +46,7 @@ fn text(c: &mut Criterion) {
group.finish();
}
/// Renders the text into a buffer of the given `size`
/// Render the text into a buffer of the given `size`.
fn render(bencher: &mut Bencher, text: &Text, size: Rect) {
let mut buffer = Buffer::empty(size);
// We use `iter_batched` to clone the value in the setup function.