maud/benchmarks/benches/liquid.rs

48 lines
1.3 KiB
Rust
Raw Normal View History

2016-10-02 17:24:36 +13:00
#![feature(test)]
extern crate test;
2019-03-16 21:17:03 +13:00
use liquid::ParserBuilder;
use liquid::value::{Object, Value};
2016-10-02 17:24:36 +13:00
static SOURCE: &'static str = "<html>
<head>
<title>{{year}}</title>
</head>
<body>
<h1>CSL {{year}}</h1>
<ul>
{% for team in teams %}
2019-03-16 21:17:03 +13:00
<li class=\"{% if forloop.first %}champion{% endif %}\">
2016-10-02 17:24:36 +13:00
<b>{{team.name}}</b>: {{team.score}}
</li>
{% endfor %}
</ul>
</body>
</html>";
fn make_team(name: &'static str, score: u16) -> Value {
let mut team = Object::new();
2019-03-16 21:17:03 +13:00
team.insert("name".into(), Value::scalar(name));
team.insert("score".into(), Value::scalar(score as i32));
2016-10-02 17:24:36 +13:00
Value::Object(team)
}
#[bench]
fn render_template(b: &mut test::Bencher) {
2019-03-16 21:17:03 +13:00
let template = test::black_box(ParserBuilder::with_liquid().build().unwrap().parse(SOURCE).unwrap());
let mut globals = test::black_box({
let mut globals = Object::new();
2019-03-16 21:17:03 +13:00
globals.insert("year".into(), Value::scalar(2015));
2016-10-02 17:24:36 +13:00
let teams = vec![
make_team("Jiangsu", 43),
make_team("Beijing", 27),
make_team("Guangzhou", 22),
make_team("Shandong", 12),
];
2019-03-16 21:17:03 +13:00
globals.insert("teams".into(), Value::Array(teams));
globals
2016-10-02 17:24:36 +13:00
});
b.iter(|| template.render(&mut globals).unwrap());
2016-10-02 17:24:36 +13:00
}