summaryrefslogtreecommitdiff
path: root/benches
diff options
context:
space:
mode:
authorMartin <mhaug@live.de>2021-08-18 18:12:26 +0200
committerGitHub <noreply@github.com>2021-08-18 18:12:26 +0200
commitc44ecbfbd2706bf8e09728f2c85135aa2299d542 (patch)
tree1a7fe1b7f1627ed63817030468476ef4a5dc2b9e /benches
parent011865ab5c8943abcb64c7b545e265d1a65db32a (diff)
Move to exclusively oneshot benchmarks with Iai fork (#41)
Diffstat (limited to 'benches')
-rw-r--r--benches/bench.typ45
-rw-r--r--benches/oneshot.rs82
2 files changed, 127 insertions, 0 deletions
diff --git a/benches/bench.typ b/benches/bench.typ
new file mode 100644
index 00000000..f290844b
--- /dev/null
+++ b/benches/bench.typ
@@ -0,0 +1,45 @@
+// Configuration with `page` and `font` functions.
+#page(width: 450pt, margins: 1cm)
+
+// There are variables and they can take normal values like strings, ...
+#let city = "Berlin"
+
+// ... but also "template" values. While these contain markup,
+// they are also values and can be summed, stored in arrays etc.
+// There are also more standard control flow structures, like #if and #for.
+#let university = [*Technische Universität {city}*]
+#let faculty = [*Fakultät II, Institut for Mathematik*]
+
+// The `box` function just places content into a rectangular container. When
+// the only argument to a function is a template, the parentheses can be omitted
+// (i.e. `f[a]` is the same as `f([a])`).
+#box[
+ // Backslash adds a forced line break.
+ #university \
+ #faculty \
+ Sekretariat MA \
+ Dr. Max Mustermann \
+ Ola Nordmann, John Doe
+]
+#align(right, box[*WiSe 2019/2020* \ Woche 3])
+
+// Adds vertical spacing.
+#v(6mm)
+
+// If the last argument to a function is a template, we can also place it behind
+// the parentheses.
+#align(center)[
+ // Markdown-like syntax for headings.
+ ==== 3. Übungsblatt Computerorientierte Mathematik II #v(4mm)
+ *Abgabe: 03.05.2019* (bis 10:10 Uhr in MA 001) #v(4mm)
+ *Alle Antworten sind zu beweisen.*
+]
+
+*1. Aufgabe* #align(right)[(1 + 1 + 2 Punkte)]
+
+Ein _Binärbaum_ ist ein Wurzelbaum, in dem jeder Knoten ≤ 2 Kinder hat.
+Die Tiefe eines Knotens _v_ ist die Länge des eindeutigen Weges von der Wurzel
+zu _v_, und die Höhe von _v_ ist die Länge eines längsten (absteigenden) Weges
+von _v_ zu einem Blatt. Die Höhe des Baumes ist die Höhe der Wurzel.
+
+#v(6mm)
diff --git a/benches/oneshot.rs b/benches/oneshot.rs
new file mode 100644
index 00000000..5f11589b
--- /dev/null
+++ b/benches/oneshot.rs
@@ -0,0 +1,82 @@
+use std::path::Path;
+
+use iai::{black_box, main, Iai};
+
+use typst::eval::eval;
+use typst::layout::layout;
+use typst::loading::{MemLoader};
+use typst::parse::{parse, Scanner, TokenMode, Tokens};
+use typst::source::{SourceFile, SourceId};
+use typst::Context;
+
+const SRC: &str = include_str!("bench.typ");
+
+fn context() -> (Context, SourceId) {
+ let font = include_bytes!("../fonts/EBGaramond-Regular.ttf");
+ let loader = MemLoader::new()
+ .with(Path::new("EBGaramond-Regular.ttf"), &font[..])
+ .wrap();
+ let mut ctx = Context::new(loader);
+ let id = ctx.sources.provide(Path::new(""), SRC.to_string());
+ (ctx, id)
+}
+
+fn bench_decode(iai: &mut Iai) {
+ iai.run(|| {
+ // We don't use chars().count() because that has a special
+ // superfast implementation.
+ let mut count = 0;
+ let mut chars = black_box(SRC).chars();
+ while let Some(_) = chars.next() {
+ count += 1;
+ }
+ count
+ })
+}
+
+fn bench_scan(iai: &mut Iai) {
+ iai.run(|| {
+ let mut count = 0;
+ let mut scanner = Scanner::new(black_box(SRC));
+ while let Some(_) = scanner.eat() {
+ count += 1;
+ }
+ count
+ })
+}
+
+fn bench_tokenize(iai: &mut Iai) {
+ iai.run(|| Tokens::new(black_box(SRC), black_box(TokenMode::Markup)).count());
+}
+
+fn bench_parse(iai: &mut Iai) {
+ iai.run(|| parse(&SourceFile::detached(SRC)));
+}
+
+fn bench_eval(iai: &mut Iai) {
+ let (mut ctx, id) = context();
+ let ast = ctx.parse(id).unwrap();
+ iai.run(|| eval(&mut ctx, id, &ast).unwrap());
+}
+
+fn bench_to_tree(iai: &mut Iai) {
+ let (mut ctx, id) = context();
+ let module = ctx.evaluate(id).unwrap();
+ iai.run(|| module.template.to_tree(ctx.state()));
+}
+
+fn bench_layout(iai: &mut Iai) {
+ let (mut ctx, id) = context();
+ let tree = ctx.execute(id).unwrap();
+ iai.run(|| layout(&mut ctx, &tree));
+}
+
+main!(
+ bench_decode,
+ bench_scan,
+ bench_tokenize,
+ bench_parse,
+ bench_eval,
+ bench_to_tree,
+ bench_layout
+);