Skip to content

Commit

Permalink
Add benchmarks for rinja (mitsuhiko#588)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez authored Oct 2, 2024
1 parent 1f119ec commit 670b67b
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 37 deletions.
74 changes: 74 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ These are related template engines for Rust:

* [Askama](https://crates.io/crates/askama): Jinja inspired, type-safe, requires template
precompilation. Has significant divergence from Jinja syntax in parts.
* [Rinja](https://crates.io/crates/rinja): Jinja inspired, type-safe, requires template
precompilation. Has significant divergence from Jinja syntax in parts.
* [Tera](https://crates.io/crates/tera): Jinja inspired, dynamic, has divergences from Jinja.
* [TinyTemplate](https://crates.io/crates/tinytemplate): minimal footprint template engine
with syntax that takes lose inspiration from Jinja and handlebars.
Expand Down
1 change: 1 addition & 0 deletions benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.93"
tera = "1.17.1"
askama = "0.12.1"
rinja = "0.3.4"

[dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports"] }
Expand Down
95 changes: 58 additions & 37 deletions benchmarks/benches/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,47 +34,61 @@ struct Context {
title: &'static str,
}

impl Default for Context {
fn default() -> Self {
Self {
items: vec![
"<First Item>".into(),
"<Second Item>".into(),
"<Third Item>".into(),
"<Fourth Item>".into(),
"<Fifth Item>".into(),
"<Sixth Item>".into(),
],
site: Site {
nav: vec![
NavItem {
url: "/",
title: "Index",
is_active: true,
},
NavItem {
url: "/download",
title: "Download",
is_active: false,
},
NavItem {
url: "/about",
title: "About",
is_active: false,
},
NavItem {
url: "/help",
title: "Help",
is_active: false,
#[derive(Serialize, Debug, rinja::Template)]
#[template(path = "comparison/askama.html")]
struct RinjaContext {
items: Vec<String>,
site: Site,
title: &'static str,
}

macro_rules! default_context {
($($ty:ident),+) => {
$(impl Default for $ty {
fn default() -> Self {
Self {
items: vec![
"<First Item>".into(),
"<Second Item>".into(),
"<Third Item>".into(),
"<Fourth Item>".into(),
"<Fifth Item>".into(),
"<Sixth Item>".into(),
],
site: Site {
nav: vec![
NavItem {
url: "/",
title: "Index",
is_active: true,
},
NavItem {
url: "/download",
title: "Download",
is_active: false,
},
NavItem {
url: "/about",
title: "About",
is_active: false,
},
NavItem {
url: "/help",
title: "Help",
is_active: false,
},
],
copyright: 2022,
},
],
copyright: 2022,
},
title: "My Benchmark Site",
}
title: "My Benchmark Site",
}
}
})+
}
}

default_context!(Context, RinjaContext);

pub fn bench_compare_compile(c: &mut Criterion) {
let mut g = c.benchmark_group("cmp_compile");

Expand Down Expand Up @@ -199,6 +213,13 @@ pub fn bench_compare_render(c: &mut Criterion) {
});
});

g.bench_function("rinja", |b| {
b.iter(|| {
let context = black_box(RinjaContext::default());
rinja::Template::render(&context).unwrap();
});
});

g.bench_function("askama", |b| {
b.iter(|| {
let context = black_box(Context::default());
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/rinja.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[general]
dirs = ["inputs"]

0 comments on commit 670b67b

Please sign in to comment.