summaryrefslogtreecommitdiff
path: root/bench/src/bench.rs
blob: d4e297bf3fe9a69a65cffec3fee7f21248eb091c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::path::Path;
use std::rc::Rc;

use criterion::{criterion_group, criterion_main, Criterion};

use typst::eval::eval;
use typst::exec::exec;
use typst::export::pdf;
use typst::layout::layout;
use typst::loading::FsLoader;
use typst::parse::parse;
use typst::typeset;

const FONT_DIR: &str = "../fonts";
const TYP_DIR: &str = "../tests/typ";
const CASES: &[&str] = &["full/coma.typ", "text/basic.typ"];

fn benchmarks(c: &mut Criterion) {
    let mut loader = FsLoader::new();
    loader.search_path(FONT_DIR);

    let mut cache = typst::cache::Cache::new(&loader);
    let scope = typst::library::new();
    let state = typst::exec::State::default();

    for case in CASES {
        let path = Path::new(TYP_DIR).join(case);
        let name = path.file_stem().unwrap().to_string_lossy();

        macro_rules! bench {
            ($step:literal: $code:expr) => {
                c.bench_function(&format!("{}-{}", $step, name), |b| {
                    b.iter(|| {
                        cache.layout.clear();
                        $code
                    });
                });
            };
        }

        // Prepare intermediate results, run warm and fill caches.
        let src = std::fs::read_to_string(&path).unwrap();
        let tree = Rc::new(parse(&src).output);
        let evaluated = eval(&mut loader, &mut cache, Some(&path), tree.clone(), &scope);
        let executed = exec(&evaluated.output.template, state.clone());
        let layouted = layout(&mut loader, &mut cache, &executed.output);

        // Bench!
        bench!("parse": parse(&src));
        bench!("eval": eval(&mut loader, &mut cache, Some(&path), tree.clone(), &scope));
        bench!("exec": exec(&evaluated.output.template, state.clone()));
        bench!("layout": layout(&mut loader, &mut cache, &executed.output));
        bench!("typeset": typeset(&mut loader, &mut cache, Some(&path), &src, &scope, state.clone()));
        bench!("pdf": pdf(&cache, &layouted));
    }
}

criterion_group!(benches, benchmarks);
criterion_main!(benches);