From 2cbd848fc67a641004ddd69dce9b7d9781a179f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= <neikos@neikos.email> Date: Mon, 17 Oct 2016 21:48:08 +0200 Subject: [PATCH 1/2] Add complicated Maud benchmark --- benchmarks/benches/complicated_maud.rs | 102 +++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 benchmarks/benches/complicated_maud.rs diff --git a/benchmarks/benches/complicated_maud.rs b/benchmarks/benches/complicated_maud.rs new file mode 100644 index 0000000..4a198d0 --- /dev/null +++ b/benchmarks/benches/complicated_maud.rs @@ -0,0 +1,102 @@ +#![feature(plugin, test)] +#![plugin(maud_macros)] + +extern crate maud; +extern crate test; + +use maud::Markup; + +#[derive(Debug)] +struct Entry { + name: &'static str, + score: usize, +} + +mod btn { + use maud::Render; + + #[derive(Copy, Clone)] + pub enum RequestMethod { + Get, Post + } + + #[derive(Copy, Clone)] + pub struct Button<'a> { + label: &'a str, + path: &'a str, + req_meth: RequestMethod, + } + + impl<'a> Button<'a> { + pub fn new(label: &'a str, path: &'a str) -> Button<'a> { + Button { + label: label, + path: path, + req_meth: RequestMethod::Get, + } + } + + pub fn with_method(mut self, meth: RequestMethod) -> Button<'a> { + self.req_meth = meth; + self + } + } + + impl<'a> Render for Button<'a> { + fn render_to(&self, mut f: &mut String) { + match self.req_meth { + RequestMethod::Get => { + f.push_str(&html!( + a.btn href=(self.path) (self.label) + ).into_string()) + } + RequestMethod::Post => { + f.push_str(&html!( + form method="POST" action=(self.path) { + input.btn type="submit" value=(self.label) / + } + ).into_string()) + } + } + } + } +} + +fn layout<S: AsRef<str>>(title: S, inner: Markup) -> Markup { + html! { + html { + head { + title (title.as_ref()) + } + body { + (inner) + } + } + } +} + +#[bench] +fn render_complicated_template(b: &mut test::Bencher) { + let year = test::black_box("2015"); + let teams = test::black_box(vec![ + Entry { name: "Jiangsu", score: 43 }, + Entry { name: "Beijing", score: 27 }, + Entry { name: "Guangzhou", score: 22 }, + Entry { name: "Shandong", score: 12 }, + ]); + b.iter(|| { + use btn::{Button, RequestMethod}; + layout(format!("Homepage of {}", year), html! { + h1 { "Hello there!" } + + @for entry in &teams { + div { + strong (entry.name) + (Button::new("Edit", "edit")) + (Button::new("Delete", "edit") + .with_method(RequestMethod::Post)) + } + } + }) + }); +} From be43b067bacc9eb54bf56ce77ec9e1f55113df01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= <com2040@gmail.com> Date: Tue, 18 Oct 2016 11:20:08 +0200 Subject: [PATCH 2/2] Use u32 instead of usize --- benchmarks/benches/complicated_maud.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/benches/complicated_maud.rs b/benchmarks/benches/complicated_maud.rs index 4a198d0..4d86b61 100644 --- a/benchmarks/benches/complicated_maud.rs +++ b/benchmarks/benches/complicated_maud.rs @@ -9,7 +9,7 @@ use maud::Markup; #[derive(Debug)] struct Entry { name: &'static str, - score: usize, + score: u32, } mod btn {