2021-05-08 14:05:02 +00:00
|
|
|
use crate::app;
|
|
|
|
use crate::ui;
|
|
|
|
use criterion::{criterion_group, criterion_main, Criterion};
|
|
|
|
use crossterm::execute;
|
|
|
|
use crossterm::terminal as term;
|
|
|
|
use std::fs;
|
|
|
|
use tui::backend::CrosstermBackend;
|
|
|
|
use tui::Terminal;
|
2021-07-29 17:25:44 +00:00
|
|
|
use xplr::runner::get_tty;
|
2021-05-08 14:05:02 +00:00
|
|
|
use xplr::*;
|
|
|
|
|
|
|
|
const PWD: &str = "/tmp/xplr_bench";
|
|
|
|
|
|
|
|
fn navigation_benchmark(c: &mut Criterion) {
|
|
|
|
fs::create_dir_all(PWD).unwrap();
|
|
|
|
(1..10000).for_each(|i| {
|
2022-06-02 16:40:58 +00:00
|
|
|
fs::File::create(std::path::Path::new(PWD).join(i.to_string())).unwrap();
|
2021-05-08 14:05:02 +00:00
|
|
|
});
|
|
|
|
|
2021-05-21 07:17:28 +00:00
|
|
|
let lua = mlua::Lua::new();
|
2023-02-13 14:31:41 +00:00
|
|
|
let mut app =
|
|
|
|
app::App::create("xplr".into(), None, PWD.into(), &lua, None, [].into())
|
|
|
|
.expect("failed to create app");
|
2021-05-08 14:05:02 +00:00
|
|
|
|
|
|
|
app = app
|
|
|
|
.clone()
|
|
|
|
.handle_task(app::Task::new(
|
|
|
|
app::MsgIn::External(app::ExternalMsg::ChangeDirectory(PWD.into())),
|
|
|
|
None,
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
c.bench_function("focus next item", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
app.clone()
|
|
|
|
.handle_task(app::Task::new(
|
|
|
|
app::MsgIn::External(app::ExternalMsg::FocusNext),
|
|
|
|
None,
|
|
|
|
))
|
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
c.bench_function("focus previous item", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
app.clone()
|
|
|
|
.handle_task(app::Task::new(
|
|
|
|
app::MsgIn::External(app::ExternalMsg::FocusPrevious),
|
|
|
|
None,
|
|
|
|
))
|
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
c.bench_function("focus first item", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
app.clone()
|
|
|
|
.handle_task(app::Task::new(
|
|
|
|
app::MsgIn::External(app::ExternalMsg::FocusFirst),
|
|
|
|
None,
|
|
|
|
))
|
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
c.bench_function("focus last item", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
app.clone()
|
|
|
|
.handle_task(app::Task::new(
|
|
|
|
app::MsgIn::External(app::ExternalMsg::FocusLast),
|
|
|
|
None,
|
|
|
|
))
|
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
c.bench_function("leave and enter directory", |b| {
|
|
|
|
b.iter(|| {
|
|
|
|
app.clone()
|
|
|
|
.handle_task(app::Task::new(
|
|
|
|
app::MsgIn::External(app::ExternalMsg::Back),
|
|
|
|
None,
|
|
|
|
))
|
|
|
|
.unwrap()
|
|
|
|
.handle_task(app::Task::new(
|
|
|
|
app::MsgIn::External(app::ExternalMsg::Enter),
|
|
|
|
None,
|
|
|
|
))
|
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw_benchmark(c: &mut Criterion) {
|
|
|
|
fs::create_dir_all(PWD).unwrap();
|
|
|
|
(1..10000).for_each(|i| {
|
2022-06-02 16:40:58 +00:00
|
|
|
fs::File::create(std::path::Path::new(PWD).join(i.to_string())).unwrap();
|
2021-05-08 14:05:02 +00:00
|
|
|
});
|
|
|
|
|
2021-05-21 07:17:28 +00:00
|
|
|
let lua = mlua::Lua::new();
|
2023-02-13 14:31:41 +00:00
|
|
|
let mut app =
|
|
|
|
app::App::create("xplr".into(), None, PWD.into(), &lua, None, [].into())
|
|
|
|
.expect("failed to create app");
|
2021-05-08 14:05:02 +00:00
|
|
|
|
|
|
|
app = app
|
|
|
|
.clone()
|
|
|
|
.handle_task(app::Task::new(
|
|
|
|
app::MsgIn::External(app::ExternalMsg::ChangeDirectory(PWD.into())),
|
|
|
|
None,
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
term::enable_raw_mode().unwrap();
|
|
|
|
let mut stdout = get_tty().unwrap();
|
|
|
|
// let mut stdout = stdout.lock();
|
|
|
|
execute!(stdout, term::EnterAlternateScreen).unwrap();
|
|
|
|
// let stdout = MouseTerminal::from(stdout);
|
|
|
|
let backend = CrosstermBackend::new(stdout);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
terminal.hide_cursor().unwrap();
|
|
|
|
|
|
|
|
c.bench_function("draw on terminal", |b| {
|
|
|
|
b.iter(|| {
|
2021-05-21 07:17:28 +00:00
|
|
|
terminal.draw(|f| ui::draw(f, &app, &lua)).unwrap();
|
2021-05-08 14:05:02 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
terminal.clear().unwrap();
|
|
|
|
terminal.set_cursor(0, 0).unwrap();
|
|
|
|
execute!(terminal.backend_mut(), term::LeaveAlternateScreen).unwrap();
|
|
|
|
term::disable_raw_mode().unwrap();
|
|
|
|
terminal.show_cursor().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, navigation_benchmark, draw_benchmark);
|
|
|
|
criterion_main!(benches);
|