From 970e3ecfbdf9e4e4c4faacdb87efa656e812398a Mon Sep 17 00:00:00 2001
From: Dirkjan Ochtman <dirkjan@ochtman.nl>
Date: Thu, 28 Jun 2018 17:01:12 +0200
Subject: Add benchmarks to the Askama repo

---
 testing/Cargo.toml               |  5 ++++
 testing/benches/all.rs           | 59 ++++++++++++++++++++++++++++++++++++++++
 testing/templates/big-table.html |  5 ++++
 testing/templates/teams.html     | 15 ++++++++++
 4 files changed, 84 insertions(+)
 create mode 100644 testing/benches/all.rs
 create mode 100644 testing/templates/big-table.html
 create mode 100644 testing/templates/teams.html

diff --git a/testing/Cargo.toml b/testing/Cargo.toml
index 06fdf56..ff67e2e 100644
--- a/testing/Cargo.toml
+++ b/testing/Cargo.toml
@@ -10,6 +10,7 @@ nightly = ["rocket", "rocket_codegen", "askama/with-rocket"]
 
 [dependencies]
 askama = { path = "../askama", version = "*", features = ["with-iron", "serde-json"] }
+criterion = "0.2"
 iron = "0.6"
 rocket = { version = "0.3", optional = true }
 rocket_codegen = { version = "0.3", optional = true }
@@ -17,3 +18,7 @@ serde_json = "1.0"
 
 [build-dependencies]
 askama = { path = "../askama", version = "*", features = ["with-iron", "serde-json"] }
+
+[[bench]]
+name = "all"
+harness = false
diff --git a/testing/benches/all.rs b/testing/benches/all.rs
new file mode 100644
index 0000000..862bd06
--- /dev/null
+++ b/testing/benches/all.rs
@@ -0,0 +1,59 @@
+#[macro_use]
+extern crate askama;
+#[macro_use]
+extern crate criterion;
+
+use askama::Template;
+use criterion::Criterion;
+
+criterion_main!(benches);
+criterion_group!(benches, functions);
+
+fn functions(c: &mut Criterion) {
+    c.bench_function("Big table", |b| big_table(b, &100));
+    c.bench_function("Teams", |b| teams(b));
+}
+
+fn big_table(b: &mut criterion::Bencher, size: &usize) {
+    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);
+    }
+    let ctx = BigTable { table };
+    b.iter(|| ctx.render().unwrap());
+}
+
+#[derive(Template)]
+#[template(path = "big-table.html")]
+struct BigTable {
+    table: Vec<Vec<usize>>,
+}
+
+fn teams(b: &mut criterion::Bencher) {
+    let teams = Teams {
+        year: 2015,
+        teams: vec![
+            Team { name: "Jiangsu".into(), score: 43 },
+            Team { name: "Beijing".into(), score: 27 },
+            Team { name: "Guangzhou".into(), score: 22 },
+            Team { name: "Shandong".into(), score: 12 },
+        ],
+    };
+    b.iter(|| teams.render().unwrap());
+}
+
+#[derive(Template)]
+#[template(path = "teams.html")]
+struct Teams {
+    year: u16,
+    teams: Vec<Team>,
+}
+
+struct Team {
+    name: String,
+    score: u8,
+}
diff --git a/testing/templates/big-table.html b/testing/templates/big-table.html
new file mode 100644
index 0000000..2fbfce4
--- /dev/null
+++ b/testing/templates/big-table.html
@@ -0,0 +1,5 @@
+<table>
+{% for row in table %}
+<tr>{% for col in row %}<td>{{ col|escape }}</td>{% endfor %}</tr>
+{% endfor %}
+</table>
diff --git a/testing/templates/teams.html b/testing/templates/teams.html
new file mode 100644
index 0000000..cbeaec9
--- /dev/null
+++ b/testing/templates/teams.html
@@ -0,0 +1,15 @@
+<html>
+  <head>
+    <title>{{ year }}</title>
+  </head>
+  <body>
+    <h1>CSL {{ year }}</h1>
+    <ul>
+    {% for team in teams %}
+      <li class="{% if loop.index0 == 0 %}champion{% endif %}">
+      <b>{{ team.name }}</b>: {{ team.score }}
+      </li>
+    {% endfor %}
+    </ul>
+  </body>
+</html>
-- 
cgit