maud/benchmarks/benches/handlebars.rs

60 lines
1.3 KiB
Rust
Raw Normal View History

2016-10-02 17:24:36 +13:00
// https://github.com/sunng87/handlebars-rust/blob/master/benches/bench.rs
#![feature(test)]
extern crate test;
use serde_json::value::{Map, Value as Json};
use handlebars::{to_json, Handlebars};
2016-10-02 17:24:36 +13:00
static SOURCE: &'static str = "<html>
<head>
<title>{{year}}</title>
</head>
<body>
<h1>CSL {{year}}</h1>
<ul>
{{#each teams}}
<li class=\"{{#if @first}}champion{{/if}}\">
<b>{{name}}</b>: {{score}}
</li>
{{/each}}
</ul>
</body>
</html>";
fn make_data() -> Map<String, Json> {
let mut data = Map::new();
2016-10-02 17:24:36 +13:00
data.insert("year".to_string(), to_json(&"2015"));
2016-10-02 17:24:36 +13:00
let mut teams = Vec::new();
2016-11-06 12:40:57 +13:00
for &(name, score) in
&[
("Jiangsu", 43u16),
("Beijing", 27u16),
("Guangzhou", 22u16),
("Shandong", 12u16),
]
{
let mut t = Map::new();
t.insert("name".to_string(), to_json(&name));
t.insert("score".to_string(), to_json(&score));
2016-10-02 17:24:36 +13:00
teams.push(t)
}
data.insert("teams".to_string(), to_json(&teams));
2016-10-02 17:24:36 +13:00
data
}
#[bench]
fn render_template(b: &mut test::Bencher) {
let mut handlebars = Handlebars::new();
handlebars.register_template_string("table", SOURCE.to_string())
.expect("Invalid template format");
2016-10-02 17:24:36 +13:00
let data = make_data();
b.iter(|| handlebars.render("table", &data).ok().unwrap())
}