test(gauge): add benchmarks for gauge (#2221)
This commit is contained in:
@@ -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,
|
||||
);
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
58
ratatui/benches/main/gauge.rs
Normal file
58
ratatui/benches/main/gauge.rs
Normal 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);
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user