summaryrefslogtreecommitdiffstats
path: root/vendor/tinytemplate/benches/benchmarks.rs
blob: 5170f9810b75c2025e46e19a313e503725e87201 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#[macro_use]
extern crate criterion;
extern crate tinytemplate;
#[macro_use]
extern crate serde_derive;

use criterion::Criterion;
use tinytemplate::TinyTemplate;

static TABLE_SOURCE: &'static str = "<html>
    {{ for row in table }}
        <tr>{{ for value in row }}<td>{value}</td>{{ endfor }}</tr>
    {{ endfor }}
</html>";

#[derive(Serialize)]
struct TableContext {
    table: Vec<Vec<usize>>,
}

fn make_table_context(size: usize) -> TableContext {
    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);
    }
    TableContext { table }
}

fn parse(criterion: &mut Criterion) {
    criterion.bench_function("parse-table", |b| {
        b.iter(|| {
            let mut tt = TinyTemplate::new();
            tt.add_template("table", TABLE_SOURCE).unwrap()
        });
    });
}

fn render(criterion: &mut Criterion) {
    let mut tt = TinyTemplate::new();
    tt.add_template("table", TABLE_SOURCE).unwrap();

    criterion.bench_function_over_inputs(
        "render-table",
        move |b, size| {
            let data = make_table_context(*size);

            b.iter(|| tt.render("table", &data).unwrap());
        },
        vec![1usize, 5, 10, 50, 100, 200],
    );
}

criterion_group!(benchmarks, parse, render);
criterion_main!(benchmarks);