From b4f809f1ea8a469d0bdee225f47d7f09bc22aa61 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 25 Nov 2020 19:28:04 +0100 Subject: =?UTF-8?q?Move=20benchmarks=20into=20separate=20crate=20=E2=99=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit So that CI does not have to build criterion each time. --- .gitignore | 1 + Cargo.toml | 12 +++--------- bench/Cargo.toml | 16 ++++++++++++++++ bench/src/bench.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ benches/benchmarks.rs | 43 ------------------------------------------- 5 files changed, 68 insertions(+), 52 deletions(-) create mode 100644 bench/Cargo.toml create mode 100644 bench/src/bench.rs delete mode 100644 benches/benchmarks.rs diff --git a/.gitignore b/.gitignore index 064a0a81..e4e7a1be 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ /target **/*.rs.bk Cargo.lock +bench/target tests/png tests/pdf _things diff --git a/Cargo.toml b/Cargo.toml index f92114d8..b7c3055f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,9 @@ version = "0.1.0" authors = ["The Typst Project Developers"] edition = "2018" +[workspace] +members = ["bench"] + [features] default = ["fs", "anyhow"] fs = ["fontdock/fs"] @@ -23,7 +26,6 @@ serde = { version = "1", features = ["derive"], optional = true } anyhow = { version = "1", optional = true } [dev-dependencies] -criterion = "0.3" memmap = "0.7" tiny-skia = "0.2" @@ -33,9 +35,6 @@ opt-level = 2 [profile.release] lto = true -[lib] -bench = false - [[bin]] name = "typst" required-features = ["fs", "anyhow"] @@ -44,8 +43,3 @@ required-features = ["fs", "anyhow"] name = "typeset" required-features = ["fs"] harness = false - -[[bench]] -name = "benchmarks" -required-features = ["fs"] -harness = false diff --git a/bench/Cargo.toml b/bench/Cargo.toml new file mode 100644 index 00000000..25a112a1 --- /dev/null +++ b/bench/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "typst-bench" +version = "0.0.1" +authors = ["The Typst Project Developers"] +edition = "2018" +publish = false + +[dependencies] +fontdock = { path = "../../fontdock" } +typst = { path = ".." } +criterion = "0.3" + +[[bench]] +name = "typst" +path = "src/bench.rs" +harness = false diff --git a/bench/src/bench.rs b/bench/src/bench.rs new file mode 100644 index 00000000..e004c5ce --- /dev/null +++ b/bench/src/bench.rs @@ -0,0 +1,48 @@ +use std::cell::RefCell; +use std::rc::Rc; + +use criterion::{criterion_group, criterion_main, Criterion}; +use fontdock::fs::{FsIndex, FsSource}; + +use typst::eval::{eval, State}; +use typst::export::pdf; +use typst::font::FontLoader; +use typst::layout::layout; +use typst::parse::parse; +use typst::typeset; + +const FONT_DIR: &str = "fonts"; +const COMA: &str = include_str!("../../tests/typ/coma.typ"); + +fn benchmarks(c: &mut Criterion) { + macro_rules! bench { + ($name:literal: $($tts:tt)*) => { + c.bench_function($name, |b| b.iter(|| $($tts)*)); + }; + } + + let mut index = FsIndex::new(); + index.search_dir(FONT_DIR); + + let (files, descriptors) = index.into_vecs(); + let loader = Rc::new(RefCell::new(FontLoader::new( + Box::new(FsSource::new(files)), + descriptors, + ))); + + // Prepare intermediate results and run warm. + let state = State::default(); + let tree = parse(COMA).output; + let document = eval(&tree, state.clone()).output; + let layouts = layout(&document, Rc::clone(&loader)); + + // Bench! + bench!("parse-coma": parse(COMA)); + bench!("eval-coma": eval(&tree, state.clone())); + bench!("layout-coma": layout(&document, Rc::clone(&loader))); + bench!("typeset-coma": typeset(COMA, state.clone(), Rc::clone(&loader))); + bench!("export-pdf-coma": pdf::export(&layouts, &loader.borrow())); +} + +criterion_group!(benches, benchmarks); +criterion_main!(benches); diff --git a/benches/benchmarks.rs b/benches/benchmarks.rs deleted file mode 100644 index ea37ff0f..00000000 --- a/benches/benchmarks.rs +++ /dev/null @@ -1,43 +0,0 @@ -use std::cell::RefCell; -use std::rc::Rc; - -use criterion::{criterion_group, criterion_main, Criterion}; -use fontdock::fs::{FsIndex, FsSource}; - -use typst::eval::{eval, State}; -use typst::font::FontLoader; -use typst::layout::layout; -use typst::parse::parse; -use typst::typeset; - -const FONT_DIR: &str = "fonts"; -const COMA: &str = include_str!("../tests/typ/coma.typ"); - -fn benchmarks(c: &mut Criterion) { - let state = State::default(); - - let mut index = FsIndex::new(); - index.search_dir(FONT_DIR); - - let (files, descriptors) = index.into_vecs(); - let loader = Rc::new(RefCell::new(FontLoader::new( - Box::new(FsSource::new(files)), - descriptors, - ))); - - let tree = parse(COMA).output; - let document = eval(&tree, state.clone()).output; - let _ = layout(&document, Rc::clone(&loader)); - - c.bench_function("parse-coma", |b| b.iter(|| parse(COMA))); - c.bench_function("eval-coma", |b| b.iter(|| eval(&tree, state.clone()))); - c.bench_function("layout-coma", |b| { - b.iter(|| layout(&document, Rc::clone(&loader))) - }); - c.bench_function("typeset-coma", |b| { - b.iter(|| typeset(COMA, state.clone(), Rc::clone(&loader))) - }); -} - -criterion_group!(benches, benchmarks); -criterion_main!(benches); -- cgit v1.2.3