maud/benchmarks/benches/tera.rs
2018-08-18 16:25:15 -07:00

50 lines
1.1 KiB
Rust

#![feature(test)]
#[macro_use]
extern crate serde_derive;
extern crate tera;
extern crate test;
use tera::{Context, Tera};
#[derive(Serialize, Debug)]
struct Entry {
name: &'static str,
score: u16,
}
static SOURCE: &'static str = "<html>
<head>
<title>{{ year }}</title>
</head>
<body>
<h1>CSL {{ year }}</h1>
<ul>
{% for team in teams %}
<li class=\"{% if loop.first %}champion{% endif %}\">
<b>{{ team.name }}</b>: {{ team.score }}
</li>
{% endfor %}
</ul>
</body>
</html>";
#[bench]
fn render_template(b: &mut test::Bencher) {
let mut tera = test::black_box(Tera::default());
tera.add_raw_template("table", SOURCE).unwrap();
let context = test::black_box({
let mut context = Context::new();
context.add("teams", &[
Entry { name: "Jiangsu", score: 43 },
Entry { name: "Beijing", score: 27 },
Entry { name: "Guangzhou", score: 22 },
Entry { name: "Shandong", score: 12 },
]);
context.add("year", &"2015");
context
});
b.iter(|| tera.render("table", &context));
}