I wrote a benchmark to test the speed of generating simple web pages using the Sailfish template engine, pulldown_cmark, and TOML for frontmatter data.
Somehow I can't believe that the code is so fast, so I'm wondering, if I made a mistake somewhere.
The benchmark code is:
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use plugin_system::{control, MARKDOWN};
pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("control 100.000", |b| {
b.iter(|| control::run(black_box(MARKDOWN), black_box(100_000)))
})
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
And the benchmarked code is as follows:
use extract_frontmatter::Extractor;
use pulldown_cmark::{html, Parser};
use sailfish::TemplateOnce;
use toml::Value;
#[derive(TemplateOnce)]
#[template(path = "benchmark.stpl")]
struct Template<'a> {
title: &'a str,
author: &'a str,
description: &'a str,
body: String,
}
pub fn run(markdown: &'static str, times: i32) {
for _ in 0..times {
generate_page(markdown)
}
}
fn generate_page(markdown: &'static str) {
let mut fm = Extractor::new(markdown);
fm.select_by_terminator("---");
let (front_matter, document): (Vec<&str>, &str) = fm.split();
let fm_str = front_matter.join("\n");
let mut fm_val: Value = fm_str.parse().unwrap();
let fm_tbl = fm_val.as_table_mut().unwrap();
let title = fm_tbl.remove("title").unwrap();
let author = fm_tbl.remove("author").unwrap();
let description = fm_tbl.remove("description").unwrap();
let parser = Parser::new(document);
let mut html = String::new();
html::push_html(&mut html, parser);
let ctx = Template {
title: title.as_str().unwrap(),
author: author.as_str().unwrap(),
description: description.as_str().unwrap(),
body: html,
};
ctx.render_once().unwrap();
}
MARKDOWN
is a small Markdown document. I use a &'static str
instead of loading files from the file system to exclude IO time.
The benchmark result is:
Benchmarking control 100.000: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 84.6s, or reduce sample count to 10.
control 100.000 time: [841.15 ms 841.96 ms 842.99 ms]
Found 6 outliers among 100 measurements (6.00%)
3 (3.00%) high mild
3 (3.00%) high severe
If I understand the output correctly, 'control 100.000' generates 100.000 HTML documents in 841.15 ms.
Could this be correct?