Skip to content

Commit

Permalink
test(sparkline): added benchmark (#384)
Browse files Browse the repository at this point in the history
Added benchmark for the `sparkline` widget testing a basic render with different amout of data
  • Loading branch information
Valentin271 committed Aug 11, 2023
1 parent 149d489 commit 3293c6b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ fakeit = "1.1"
itertools = "0.10"
rand = "0.8"

[[bench]]
name = "block"
harness = false

[[bench]]
name = "paragraph"
harness = false

[[bench]]
name = "block"
name = "sparkline"
harness = false

[[bench]]
Expand Down
45 changes: 45 additions & 0 deletions benches/sparkline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use criterion::{criterion_group, criterion_main, Bencher, BenchmarkId, Criterion};
use rand::Rng;
use ratatui::{
buffer::Buffer,
layout::Rect,
widgets::{Sparkline, Widget},
};

/// Benchmark for rendering a sparkline.
pub fn sparkline(c: &mut Criterion) {
let mut group = c.benchmark_group("sparkline");
let mut rng = rand::thread_rng();

for data_count in [64, 256, 2048] {
let data: Vec<u64> = (0..data_count)
.map(|_| rng.gen_range(0..data_count))
.collect();

// Render a basic sparkline
group.bench_with_input(
BenchmarkId::new("render", data_count),
&Sparkline::default().data(&data),
render,
);
}

group.finish();
}

/// 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.
// See https://github.com/ratatui-org/ratatui/pull/377.
bencher.iter_batched(
|| sparkline.clone(),
|bench_sparkline| {
bench_sparkline.render(buffer.area, &mut buffer);
},
criterion::BatchSize::LargeInput,
)
}

criterion_group!(benches, sparkline);
criterion_main!(benches);

0 comments on commit 3293c6b

Please sign in to comment.