Merge branch 'benchmark'

This commit is contained in:
Kogia-sima 2020-06-06 18:29:23 +09:00
commit 27817e7f6a
22 changed files with 2324 additions and 1 deletions

View File

@ -14,7 +14,7 @@ Simple, small, and extremely fast template engine for Rust
- Simple and intuitive syntax inspired by [EJS](https://ejs.co/)
- Relatively small number of dependencies (<15 crates in total)
- Extremely fast
- Extremely fast (See [benchmarks](./benches/README.md))
- Better error message
- Template rendering is always type-safe because templates are statically compiled.
- Syntax highlighting support ([vscode](./syntax/vscode), [vim](./syntax/vim))

1650
benches/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

20
benches/Cargo.toml Normal file
View File

@ -0,0 +1,20 @@
[package]
name = "benches"
version = "0.0.1"
authors = ["Ryohei Machida <orcinus4627@gmail.com>"]
edition = "2018"
publish = false
[dependencies]
horrorshow = "0.8.3"
markup = "0.4.1"
askama = "0.9.0"
criterion = "0.3.2"
yarte = "0.8.3"
sailfish = { path = "../sailfish" }
sailfish-macros = { path = "../sailfish-macros" }
fomat-macros = "0.3.1"
[[bench]]
name = "all"
harness = false

35
benches/README.md Normal file
View File

@ -0,0 +1,35 @@
# Rust template engine benchmarks
Performance comparison of template engines for Rust based on [criterion](https://github.com/bheisler/criterion.rs) crate
## crates
- [askama](https://github.com/djc/askama): Type-safe, compiled Jinja-like templates for Rust
- [fomat](https://github.com/krdln/fomat-macros): Alternative syntax for printing macros in Rust
- [horrorshow](https://github.com/Stebalien/horrorshow-rs): A macro-based html builder for rust
- [markup](https://github.com/utkarshkukreti/markup.rs): A blazing fast, type-safe template engine for Rust.
- [std::write!](https://doc.rust-lang.org/std/macro.write.html): the std library `write!` macro
- [yarte](https://github.com/botika/yarte): Yet Another Rust Template Engine, is the fastest template engine
- [sailfish](https://github.com/Kogia-sima/sailfish): Simple, small, and extremely fast template engine for Rust
## Running the benchmarks
```console
$ cargo bench
```
## Environment
- OS: Ubuntu 20.04 LTS
- CPU Model Name: Intel(R) Core(TM) i5-8265U CPU @ 1.60GHz
- BogoMIPS: 3600.00
## Results
- Big table
![Big table](./bigtable.png)
- Teams
![Teams](./teams.png)

31
benches/benches/all.rs Normal file
View File

@ -0,0 +1,31 @@
use criterion::{criterion_group, criterion_main, Criterion};
use benches::{
askama_bench, fomat, horrorshow_bench, markup_bench, sailfish, std_write, yarte_bench
};
fn big_table(c: &mut Criterion) {
let mut g = c.benchmark_group("Big table");
g.bench_function("Askama", |b| askama_bench::big_table(b, &100));
g.bench_function("fomat", |b| fomat::big_table(b, &100));
g.bench_function("Horrorshow", |b| horrorshow_bench::big_table(b, &100));
g.bench_function("Markup", |b| markup_bench::big_table(b, &100));
g.bench_function("Yarte", |b| yarte_bench::big_table(b, &100));
g.bench_function("write", |b| std_write::big_table(b, &100));
g.bench_function("sailfish", |b| sailfish::big_table(b, &100));
g.finish();
}
fn teams(c: &mut Criterion) {
let mut g = c.benchmark_group("Teams");
g.bench_function("Askama", |b| askama_bench::teams(b, &0));
g.bench_function("fomat", |b| fomat::teams(b, &0));
g.bench_function("Horrorshow", |b| horrorshow_bench::teams(b, &0));
g.bench_function("Markup", |b| markup_bench::teams(b, &0));
g.bench_function("Yarte", |b| yarte_bench::teams(b));
g.bench_function("write", |b| std_write::teams(b, &0));
g.bench_function("sailfish", |b| sailfish::teams(b));
g.finish();
}
criterion_group!(benches, big_table, teams);
criterion_main!(benches);

BIN
benches/bigtable.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View File

@ -0,0 +1,58 @@
use askama::Template;
use criterion;
pub fn big_table(b: &mut criterion::Bencher<'_>, size: &usize) {
let mut table = Vec::with_capacity(*size);
for _ in 0..*size {
let mut inner = Vec::with_capacity(*size);
for i in 0..*size {
inner.push(i);
}
table.push(inner);
}
let ctx = BigTable { table };
b.iter(|| ctx.render().unwrap());
}
#[derive(Template)]
#[template(path = "big-table.html")]
struct BigTable {
table: Vec<Vec<usize>>,
}
pub fn teams(b: &mut criterion::Bencher<'_>, _: &usize) {
let teams = Teams {
year: 2015,
teams: vec![
Team {
name: "Jiangsu".into(),
score: 43,
},
Team {
name: "Beijing".into(),
score: 27,
},
Team {
name: "Guangzhou".into(),
score: 22,
},
Team {
name: "Shandong".into(),
score: 12,
},
],
};
b.iter(|| teams.render().unwrap());
}
#[derive(Template)]
#[template(path = "teams.html")]
struct Teams {
year: u16,
teams: Vec<Team>,
}
struct Team {
name: String,
score: u8,
}

11
benches/src/build.rs Normal file
View File

@ -0,0 +1,11 @@
use ructe::Ructe;
use std::env;
use std::path::PathBuf;
fn main() {
let in_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("templates_ructe");
Ructe::from_env()
.unwrap()
.compile_templates(&in_dir)
.expect("compile templates");
}

76
benches/src/fomat.rs Normal file
View File

@ -0,0 +1,76 @@
use criterion;
use fomat_macros::fomat;
pub fn big_table(b: &mut criterion::Bencher<'_>, size: &usize) {
let mut table = Vec::with_capacity(*size);
for _ in 0..*size {
let mut inner = Vec::with_capacity(*size);
for i in 0..*size {
inner.push(i);
}
table.push(inner);
}
b.iter(|| {
fomat!(
"<table>\n"
for r1 in &table {
"<tr>\n"
for r2 in r1 { "<td>"(r2)"</td>" }
"\n</tr>\n"
}
"</table>"
)
});
}
pub fn teams(b: &mut criterion::Bencher<'_>, _: &usize) {
let teams = Teams {
year: 2015,
teams: vec![
Team {
name: "Jiangsu".into(),
score: 43,
},
Team {
name: "Beijing".into(),
score: 27,
},
Team {
name: "Guangzhou".into(),
score: 22,
},
Team {
name: "Shandong".into(),
score: 12,
},
],
};
b.iter(|| {
fomat!(
"<html>
<head>
<title>"(teams.year)"</title>
</head>
<body>
<h1>CSL "(teams.year)"</h1>
<ul>"
for (i,team) in (&teams).teams.iter().enumerate() {
" <li class=\"" if i == 0 { "champion" } "\">\n"
" <b>"(team.name)"</b>: "(team.score)"\n"
" </li>\n"
}
"\n </ul>
</body>
</html>")
});
}
struct Teams {
year: u16,
teams: Vec<Team>,
}
struct Team {
name: String,
score: u8,
}

View File

@ -0,0 +1,92 @@
use criterion;
use horrorshow::html;
use horrorshow::prelude::*;
use horrorshow::Error;
pub fn big_table(b: &mut criterion::Bencher<'_>, size: &usize) {
let mut table = Vec::with_capacity(*size);
for _ in 0..*size {
let mut inner = Vec::with_capacity(*size);
for i in 0..*size {
inner.push(i);
}
table.push(inner);
}
b.iter(|| big_table_render(&table).unwrap());
}
fn big_table_render(table: &Vec<Vec<usize>>) -> Result<String, Error> {
let page = html! {
table {
@ for row in table {
tr {
@ for col in row {
td { : col }
}
}
}
}
};
page.into_string()
}
pub fn teams(b: &mut criterion::Bencher<'_>, _: &usize) {
let teams = Teams {
year: 2015,
teams: vec![
Team {
name: "Jiangsu".into(),
score: 43,
},
Team {
name: "Beijing".into(),
score: 27,
},
Team {
name: "Guangzhou".into(),
score: 22,
},
Team {
name: "Shandong".into(),
score: 12,
},
],
};
b.iter(|| teams_render(&teams).unwrap());
}
fn teams_render(teams: &Teams) -> Result<String, Error> {
let page = html! {
html {
head {
title { : teams.year }
}
body {
h1 { : Raw("CSL ");
: teams.year
}
ul {
@ for (idx, team) in teams.teams.iter().enumerate() {
li(class? = if idx == 0 { Some(Raw("champion")) } else { None }) {
b { : &team.name }
: Raw(": ");
: team.score
}
}
}
}
}
};
page.into_string()
}
struct Teams {
year: u16,
teams: Vec<Team>,
}
struct Team {
name: String,
score: u8,
}

7
benches/src/lib.rs Normal file
View File

@ -0,0 +1,7 @@
pub mod askama_bench;
pub mod fomat;
pub mod horrorshow_bench;
pub mod markup_bench;
pub mod sailfish;
pub mod std_write;
pub mod yarte_bench;

View File

@ -0,0 +1,83 @@
use criterion;
use markup::define;
// TODO: Switch to `markup::define!` when upgrading to Rust 2018.
define! {
BigTable<'a>(table: &'a [Vec<usize>]) {
table {
@for r1 in table.iter() {
tr {
@for r2 in r1.iter() {
td { {*r2} }
}
}
}
}
}
}
pub fn big_table(b: &mut criterion::Bencher<'_>, size: &usize) {
let mut table = Vec::with_capacity(*size);
for _ in 0..*size {
let mut inner = Vec::with_capacity(*size);
for i in 0..*size {
inner.push(i);
}
table.push(inner);
}
b.iter(|| BigTable { table: &table }.to_string());
}
pub struct Team {
name: String,
score: u8,
}
define! {
Teams<'a>(year: u32, teams: &'a [Team]) {
html {
head {
title { {year} }
}
body {
h1 { "CSL " {year} }
ul {
@for (index, team) in teams.iter().enumerate() {
li.{if index == 0 { Some("champion") } else { None }} {
b { {team.name} } ": " {team.score}
}
}
}
}
}
}
}
pub fn teams(b: &mut criterion::Bencher<'_>, _: &usize) {
let year = 2015;
let teams = vec![
Team {
name: "Jiangsu".into(),
score: 43,
},
Team {
name: "Beijing".into(),
score: 27,
},
Team {
name: "Guangzhou".into(),
score: 22,
},
Team {
name: "Shandong".into(),
score: 12,
},
];
b.iter(|| {
Teams {
year,
teams: &teams,
}
.to_string()
});
}

72
benches/src/sailfish.rs Normal file
View File

@ -0,0 +1,72 @@
use sailfish::TemplateOnce;
use sailfish_macros::TemplateOnce;
pub fn big_table(b: &mut criterion::Bencher<'_>, size: &usize) {
let mut table = Vec::with_capacity(*size);
for _ in 0..*size {
let mut inner = Vec::with_capacity(*size);
for i in 0..*size {
inner.push(i);
}
table.push(inner);
}
b.iter(|| {
let ctx = BigTable { table: &table };
ctx.render_once().unwrap()
});
}
pub fn teams(b: &mut criterion::Bencher<'_>) {
let teams = Teams {
year: 2015,
teams: vec![
Team {
name: "Jiangsu".into(),
score: 43,
},
Team {
name: "Beijing".into(),
score: 27,
},
Team {
name: "Guangzhou".into(),
score: 22,
},
Team {
name: "Shandong".into(),
score: 12,
},
],
};
b.iter(|| {
let teams = TeamsTemplate {
year: teams.year,
teams: &teams.teams
};
teams.render_once().unwrap()
});
}
#[derive(TemplateOnce)]
#[template(path = "big-table.stpl")]
struct BigTable<'a> {
table: &'a [Vec<usize>],
}
#[derive(TemplateOnce)]
#[template(path = "teams.stpl")]
struct TeamsTemplate<'a> {
year: u16,
teams: &'a [Team],
}
struct Teams {
year: u16,
teams: Vec<Team>,
}
struct Team {
name: String,
score: u8,
}

94
benches/src/std_write.rs Normal file
View File

@ -0,0 +1,94 @@
use std::io::Write;
use criterion;
pub fn big_table(b: &mut criterion::Bencher<'_>, size: &usize) {
let mut table = Vec::with_capacity(*size);
for _ in 0..*size {
let mut inner = Vec::with_capacity(*size);
for i in 0..*size {
inner.push(i);
}
table.push(inner);
}
b.iter(|| {
let mut output = Vec::new();
write!(&mut output, "<table>").unwrap();
for r1 in &table {
write!(&mut output, "<tr>\n").unwrap();
for r2 in r1 {
write!(&mut output, "<td>{col}</td>", col = r2).unwrap();
}
write!(&mut output, "</tr>\n").unwrap();
}
write!(&mut output, "</table>").unwrap();
});
}
pub fn teams(b: &mut criterion::Bencher<'_>, _: &usize) {
let teams = Teams {
year: 2015,
teams: vec![
Team {
name: "Jiangsu".into(),
score: 43,
},
Team {
name: "Beijing".into(),
score: 27,
},
Team {
name: "Guangzhou".into(),
score: 22,
},
Team {
name: "Shandong".into(),
score: 12,
},
],
};
b.iter(|| {
let mut output = Vec::new();
write!(
&mut output,
"<html>
<head>
<title>{year}</title>
</head>
<body>
<h1>CSL {year}</h1>
<ul>",
year = teams.year
)
.unwrap();
for (i, team) in (&teams).teams.iter().enumerate() {
let champion = if i != 0 { "" } else { "champion" };
write!(
&mut output,
"<li class=\"{champion}\">
<b>{name}</b>: {score}",
champion = champion,
name = team.name,
score = team.score
)
.unwrap();
}
write!(
&mut output,
" </ul>
</body>
</html>"
)
.unwrap();
});
}
struct Teams {
year: u16,
teams: Vec<Team>,
}
struct Team {
name: String,
score: u8,
}

View File

@ -0,0 +1,58 @@
use yarte::Template;
pub fn big_table(b: &mut criterion::Bencher<'_>, size: &usize) {
let mut table = Vec::with_capacity(*size);
for _ in 0..*size {
let mut inner = Vec::with_capacity(*size);
for i in 0..*size {
inner.push(i);
}
table.push(inner);
}
let ctx = BigTable { table };
b.iter(|| ctx.call().unwrap());
}
#[derive(Template)]
#[template(path = "big-table.hbs")]
struct BigTable {
table: Vec<Vec<usize>>,
}
pub fn teams(b: &mut criterion::Bencher<'_>) {
let teams = Teams {
year: 2015,
teams: vec![
Team {
name: "Jiangsu".into(),
score: 43,
},
Team {
name: "Beijing".into(),
score: 27,
},
Team {
name: "Guangzhou".into(),
score: 22,
},
Team {
name: "Shandong".into(),
score: 12,
},
],
};
b.iter(|| teams.call().unwrap());
}
#[derive(Template)]
#[template(path = "teams.hbs")]
struct Teams {
year: u16,
teams: Vec<Team>,
}
struct Team {
name: String,
score: u8,
}

BIN
benches/teams.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

View File

@ -0,0 +1 @@
<table>{{#each table}}<tr>{{#each this}}<td>{{ this }}</td>{{/each}}</tr>{{/each}}</table>

View File

@ -0,0 +1,5 @@
<table>
{% for row in table %}
<tr>{% for col in row %}<td>{{ col|escape }}</td>{% endfor %}</tr>
{% endfor %}
</table>

View File

@ -0,0 +1,3 @@
<table>
<% for row in table { %><tr><% for col in row { %><td><%= *col %></td><% } %></tr><% } %>
</table>

View File

@ -0,0 +1 @@
<html><head><title>{{ year }}</title></head><body><h1>CSL {{ year }}</h1><ul>{{#each teams }}<li class="{{#if index0 == 0 }}champion{{/if}}"><b>{{ name }}</b>: {{ score }}</li>{{/each}}</ul></body></html>

View File

@ -0,0 +1,15 @@
<html>
<head>
<title>{{ year }}</title>
</head>
<body>
<h1>CSL {{ year }}</h1>
<ul>
{% for team in teams %}
<li class="{% if loop.index0 == 0 %}champion{% endif %}">
<b>{{ team.name }}</b>: {{ team.score }}
</li>
{% endfor %}
</ul>
</body>
</html>

View File

@ -0,0 +1,11 @@
<html>
<head>
<title><%= year %></title>
</head>
<body>
<h1>CSL <%= year %></h1>
<ul>
<% for (i, team) in teams.iter().enumerate() { %><li class="<% if i == 0 { %>champion<% } %>"><b><%- team.name %></b>: <%= team.score %></li><% } %>
</ul>
</body>
</html>