mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-22 04:33:13 +00:00
perf: add buffer diff benchmark
This commit is contained in:
parent
d72968d86b
commit
99ef8651aa
1 changed files with 38 additions and 1 deletions
|
@ -1,11 +1,14 @@
|
|||
use std::iter::zip;
|
||||
|
||||
use criterion::{black_box, BenchmarkId, Criterion};
|
||||
use ratatui::{
|
||||
buffer::{Buffer, Cell},
|
||||
layout::Rect,
|
||||
text::Line,
|
||||
widgets::Widget,
|
||||
};
|
||||
|
||||
criterion::criterion_group!(benches, empty, filled, with_lines);
|
||||
criterion::criterion_group!(benches, empty, filled, with_lines, diff);
|
||||
|
||||
const fn rect(size: u16) -> Rect {
|
||||
Rect::new(0, 0, size, size)
|
||||
|
@ -58,3 +61,37 @@ fn with_lines(c: &mut Criterion) {
|
|||
}
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn diff(c: &mut Criterion) {
|
||||
const AREA: Rect = Rect {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 200,
|
||||
height: 50,
|
||||
};
|
||||
c.bench_function("buffer/diff", |b| {
|
||||
let buffer_1 = create_random_buffer(AREA);
|
||||
let buffer_2 = create_random_buffer(AREA);
|
||||
b.iter(|| {
|
||||
let _ = black_box(&buffer_1).diff(black_box(&buffer_2));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn create_random_buffer(area: Rect) -> Buffer {
|
||||
const PARAGRAPH_COUNT: i64 = 15;
|
||||
const SENTENCE_COUNT: i64 = 5;
|
||||
const WORD_COUNT: i64 = 20;
|
||||
const SEPARATOR: &str = "\n\n";
|
||||
let paragraphs = fakeit::words::paragraph(
|
||||
PARAGRAPH_COUNT,
|
||||
SENTENCE_COUNT,
|
||||
WORD_COUNT,
|
||||
SEPARATOR.to_string(),
|
||||
);
|
||||
let mut buffer = Buffer::empty(area);
|
||||
for (line, row) in zip(paragraphs.lines(), area.rows()) {
|
||||
Line::from(line).render(row, &mut buffer);
|
||||
}
|
||||
buffer
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue