mirror of
https://github.com/itsjunetime/tdf.git
synced 2026-06-01 23:51:46 -04:00
Add initial support for doing benchmarking and some starting info on how to build the benchmark stuff with poppler debug info
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,10 @@
|
||||
mod utils;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let file = std::env::args()
|
||||
.nth(1)
|
||||
.expect("Please enter a file to profile");
|
||||
|
||||
utils::render_doc(file).await;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
mod utils;
|
||||
|
||||
use utils::render_doc;
|
||||
|
||||
use criterion::{criterion_group, criterion_main, Criterion};
|
||||
|
||||
fn render_dict(c: &mut Criterion) {
|
||||
c.bench_function(
|
||||
"example dictionary",
|
||||
|b| b.iter(||
|
||||
tokio::runtime::Runtime::new()
|
||||
.unwrap()
|
||||
.block_on(render_doc("./benches/example_dictionary.pdf"))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
fn render_example(c: &mut Criterion) {
|
||||
c.bench_function(
|
||||
"adobe-provided sample",
|
||||
|b| b.iter(||
|
||||
tokio::runtime::Runtime::new()
|
||||
.unwrap()
|
||||
.block_on(render_doc("./benches/adobe_example.pdf"))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
criterion_group!(benches, render_dict, render_example);
|
||||
criterion_main!(benches);
|
||||
@@ -0,0 +1,94 @@
|
||||
use std::{hint::black_box, path::Path};
|
||||
|
||||
use crossterm::terminal::WindowSize;
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui_image::picker::{Picker, ProtocolType};
|
||||
use tdf::{converter::{run_conversion_loop, ConvertedPage, ConverterMsg}, renderer::{fill_default, start_rendering, RenderError, RenderInfo, RenderNotif}};
|
||||
use tokio::sync::mpsc::{unbounded_channel, UnboundedSender};
|
||||
|
||||
pub async fn render_doc(path: impl AsRef<Path>) {
|
||||
let pathbuf = path.as_ref().canonicalize().unwrap();
|
||||
let str_path = format!("file://{}", pathbuf.into_os_string().to_string_lossy());
|
||||
|
||||
let (to_render_tx, from_main_rx) = unbounded_channel();
|
||||
let (to_main_tx, mut from_render_rx) = unbounded_channel();
|
||||
|
||||
let font_size = (8, 14);
|
||||
let (columns, rows) = (60, 180);
|
||||
|
||||
let size = WindowSize {
|
||||
columns,
|
||||
rows,
|
||||
height: rows * font_size.1,
|
||||
width: columns * font_size.0
|
||||
};
|
||||
|
||||
std::thread::spawn(move || {
|
||||
start_rendering(str_path, to_main_tx, from_main_rx, size)
|
||||
});
|
||||
|
||||
let (mut to_converter_tx, from_main_rx) = unbounded_channel();
|
||||
let (to_main_tx, mut from_converter_rx) = unbounded_channel();
|
||||
|
||||
let mut picker = Picker::new(font_size);
|
||||
picker.protocol_type = ProtocolType::Kitty;
|
||||
|
||||
tokio::spawn(run_conversion_loop(to_main_tx, from_main_rx, picker));
|
||||
|
||||
let mut pages: Vec<Option<ConvertedPage>> = Vec::new();
|
||||
|
||||
fn handle_renderer_msg(
|
||||
msg: Result<RenderInfo, RenderError>,
|
||||
pages: &mut Vec<Option<ConvertedPage>>,
|
||||
to_converter_tx: &mut UnboundedSender<tdf::converter::ConverterMsg>,
|
||||
) {
|
||||
match msg {
|
||||
Ok(RenderInfo::NumPages(num)) => {
|
||||
fill_default(pages, num);
|
||||
to_converter_tx.send(ConverterMsg::NumPages(num)).unwrap();
|
||||
},
|
||||
Ok(RenderInfo::Page(info)) => to_converter_tx.send(ConverterMsg::AddImg(info)).unwrap(),
|
||||
Err(e) => panic!("Got error from renderer: {e:?}")
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_converter_msg(
|
||||
msg: Result<ConvertedPage, RenderError>,
|
||||
pages: &mut [Option<ConvertedPage>],
|
||||
to_converter_tx: &mut UnboundedSender<ConverterMsg>
|
||||
) {
|
||||
let page = msg.expect("Got error from converter");
|
||||
let num = page.num;
|
||||
|
||||
pages[num] = Some(page);
|
||||
|
||||
let num_got = pages.iter()
|
||||
.filter(|p| p.is_some())
|
||||
.count();
|
||||
|
||||
// we have to tell it to jump to a certain page so that it will actually render it (since
|
||||
// it only renders fanning out from the page that we currently have selected)
|
||||
to_converter_tx.send(ConverterMsg::GoToPage(num_got)).unwrap();
|
||||
}
|
||||
|
||||
let main_area = Rect {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: columns - 2,
|
||||
height: rows - 6
|
||||
};
|
||||
to_render_tx.send(RenderNotif::Area(main_area)).unwrap();
|
||||
|
||||
while pages.is_empty() || pages.iter().any(|p| p.is_none()) {
|
||||
tokio::select! {
|
||||
Some(renderer_msg) = from_render_rx.recv() => {
|
||||
handle_renderer_msg(renderer_msg, &mut pages, &mut to_converter_tx);
|
||||
},
|
||||
Some(converter_msg) = from_converter_rx.recv() => {
|
||||
handle_converter_msg(converter_msg, &mut pages, &mut to_converter_tx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
black_box(pages);
|
||||
}
|
||||
Reference in New Issue
Block a user