Merge pull request from TheNeikos/add_complicated_maud

Add complicated Maud benchmark
This commit is contained in:
Chris Wong 2016-10-18 22:27:14 +13:00 committed by GitHub
commit b9881e682c

View file

@ -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: u32,
}
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))
}
}
})
});
}