1 | #[macro_use ]
|
2 | extern crate criterion;
|
3 | extern crate tinytemplate;
|
4 | #[macro_use ]
|
5 | extern crate serde_derive;
|
6 |
|
7 | use criterion::Criterion;
|
8 | use tinytemplate::TinyTemplate;
|
9 |
|
10 | static TABLE_SOURCE: &'static str = "<html>
|
11 | {{ for row in table }}
|
12 | <tr>{{ for value in row }}<td>{value}</td>{{ endfor }}</tr>
|
13 | {{ endfor }}
|
14 | </html>" ;
|
15 |
|
16 | #[derive(Serialize)]
|
17 | struct TableContext {
|
18 | table: Vec<Vec<usize>>,
|
19 | }
|
20 |
|
21 | fn make_table_context(size: usize) -> TableContext {
|
22 | let mut table = Vec::with_capacity(size);
|
23 | for _ in 0..size {
|
24 | let mut inner = Vec::with_capacity(size);
|
25 | for i in 0..size {
|
26 | inner.push(i);
|
27 | }
|
28 | table.push(inner);
|
29 | }
|
30 | TableContext { table }
|
31 | }
|
32 |
|
33 | fn parse(criterion: &mut Criterion) {
|
34 | criterion.bench_function("parse-table" , |b| {
|
35 | b.iter(|| {
|
36 | let mut tt = TinyTemplate::new();
|
37 | tt.add_template("table" , TABLE_SOURCE).unwrap()
|
38 | });
|
39 | });
|
40 | }
|
41 |
|
42 | fn render(criterion: &mut Criterion) {
|
43 | let mut tt = TinyTemplate::new();
|
44 | tt.add_template("table" , TABLE_SOURCE).unwrap();
|
45 |
|
46 | criterion.bench_function_over_inputs(
|
47 | "render-table" ,
|
48 | move |b, size| {
|
49 | let data = make_table_context(*size);
|
50 |
|
51 | b.iter(|| tt.render("table" , &data).unwrap());
|
52 | },
|
53 | vec![1usize, 5, 10, 50, 100, 200],
|
54 | );
|
55 | }
|
56 |
|
57 | criterion_group!(benchmarks, parse, render);
|
58 | criterion_main!(benchmarks);
|
59 | |