test(benches): refactor benches into a simpler and more scalable structure (#467)

This commit is contained in:
Alexandre Pasmantier 2025-04-14 17:41:16 +00:00 committed by GitHub
parent ec8a7dbfc3
commit 315a9f71fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 66 additions and 67 deletions

View File

@ -1,7 +1,9 @@
use criterion::criterion_main;
pub mod main {
pub mod draw;
pub mod draw_results_list;
pub mod ui;
}
pub use main::*;
criterion::criterion_main!(draw_results_list::benches, draw::benches,);
criterion_main!(ui::benches,);

View File

@ -1,62 +0,0 @@
use criterion::{black_box, Criterion};
use ratatui::backend::TestBackend;
use ratatui::layout::Rect;
use ratatui::Terminal;
use std::path::PathBuf;
use television::action::Action;
use television::channels::OnAir;
use television::channels::{files::Channel, TelevisionChannel};
use television::config::{Config, ConfigEnv};
use television::television::Television;
use tokio::runtime::Runtime;
fn draw(c: &mut Criterion) {
let width = 250;
let height = 80;
let rt = Runtime::new().unwrap();
c.bench_function("draw", |b| {
b.to_async(&rt).iter_batched(
// FIXME: this is kind of hacky
|| {
let config = Config::new(&ConfigEnv::init().unwrap()).unwrap();
let backend = TestBackend::new(width, height);
let terminal = Terminal::new(backend).unwrap();
let (tx, _) = tokio::sync::mpsc::unbounded_channel();
let mut channel =
TelevisionChannel::Files(Channel::new(vec![
PathBuf::from("."),
]));
channel.find("television");
// Wait for the channel to finish loading
let mut tv =
Television::new(tx, channel, config, None, false, false);
for _ in 0..5 {
// tick the matcher
let _ = tv.channel.results(10, 0);
std::thread::sleep(std::time::Duration::from_millis(10));
}
tv.select_next_entry(10);
let _ = tv.update_preview_state(
&tv.get_selected_entry(None).unwrap(),
);
let _ = tv.update(&Action::Tick);
(tv, terminal)
},
// Measurement
|(tv, mut terminal)| async move {
television::draw::draw(
black_box(&tv.dump_context()),
black_box(&mut terminal.get_frame()),
black_box(Rect::new(0, 0, width, height)),
)
.unwrap();
},
criterion::BatchSize::SmallInput,
);
});
}
criterion::criterion_group!(benches, draw);
criterion::criterion_main!(benches);

View File

@ -1,13 +1,24 @@
use criterion::{criterion_group, Criterion};
use criterion::criterion_group;
use criterion::{black_box, Criterion};
use devicons::FileIcon;
use ratatui::backend::TestBackend;
use ratatui::layout::Alignment;
use ratatui::layout::Rect;
use ratatui::prelude::{Line, Style};
use ratatui::style::Color;
use ratatui::widgets::{Block, BorderType, Borders, ListDirection, Padding};
use ratatui::Terminal;
use std::path::PathBuf;
use television::action::Action;
use television::channels::entry::into_ranges;
use television::channels::entry::{Entry, PreviewType};
use television::channels::OnAir;
use television::channels::{files::Channel, TelevisionChannel};
use television::config::{Config, ConfigEnv};
use television::screen::colors::ResultsColorscheme;
use television::screen::results::build_results_list;
use television::television::Television;
use tokio::runtime::Runtime;
pub fn draw_results_list(c: &mut Criterion) {
// FIXME: there's probably a way to have this as a benchmark asset
@ -481,4 +492,52 @@ pub fn draw_results_list(c: &mut Criterion) {
});
}
criterion_group!(benches, draw_results_list);
pub fn draw(c: &mut Criterion) {
let width = 250;
let height = 80;
let rt = Runtime::new().unwrap();
c.bench_function("draw", |b| {
b.to_async(&rt).iter_batched(
// FIXME: this is kind of hacky
|| {
let config = Config::new(&ConfigEnv::init().unwrap()).unwrap();
let backend = TestBackend::new(width, height);
let terminal = Terminal::new(backend).unwrap();
let (tx, _) = tokio::sync::mpsc::unbounded_channel();
let mut channel =
TelevisionChannel::Files(Channel::new(vec![
PathBuf::from("."),
]));
channel.find("television");
// Wait for the channel to finish loading
let mut tv =
Television::new(tx, channel, config, None, false, false);
for _ in 0..5 {
// tick the matcher
let _ = tv.channel.results(10, 0);
std::thread::sleep(std::time::Duration::from_millis(10));
}
tv.select_next_entry(10);
let _ = tv.update_preview_state(
&tv.get_selected_entry(None).unwrap(),
);
let _ = tv.update(&Action::Tick);
(tv, terminal)
},
// Measurement
|(tv, mut terminal)| async move {
television::draw::draw(
black_box(&tv.dump_context()),
black_box(&mut terminal.get_frame()),
black_box(Rect::new(0, 0, width, height)),
)
.unwrap();
},
criterion::BatchSize::SmallInput,
);
});
}
criterion_group!(benches, draw_results_list, draw);