diff --git a/benches/bench.rs b/benches/bench.rs index 50ee058b7..c84a97939 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -3,32 +3,39 @@ extern crate handlebars; extern crate rustc_serialize as serialize; extern crate test; -use std::io::prelude::*; -use std::io::Result; -use std::fs::File; -use std::path::Path; use std::collections::BTreeMap; use handlebars::{Handlebars, Template}; use serialize::json::{Json, ToJson}; -fn load_template_source(name: &str) -> Result { - let path = Path::new(name); +static SOURCE: &'static str = " + + {{year}} + + +

CSL {{year}}

+ + +"; - let mut file = try!(File::open(&path)); - let mut buf = String::new(); - try!(file.read_to_string(&mut buf)); - Ok(buf) -} - -fn make_data () -> BTreeMap { +fn make_data() -> BTreeMap { let mut data = BTreeMap::new(); data.insert("year".to_string(), "2015".to_json()); let mut teams = Vec::new(); - for v in vec![("Jiangsu", 43u16), ("Beijing", 27u16), ("Guangzhou", 22u16), ("Shandong", 12u16)].iter() { + for v in vec![("Jiangsu", 43u16), + ("Beijing", 27u16), + ("Guangzhou", 22u16), + ("Shandong", 12u16)] + .iter() { let (name, score) = *v; let mut t = BTreeMap::new(); t.insert("name".to_string(), name.to_json()); @@ -42,22 +49,16 @@ fn make_data () -> BTreeMap { #[bench] fn parse_template(b: &mut test::Bencher) { - let source = load_template_source("./benches/template.hbs").ok().unwrap(); - b.iter(|| { - Template::compile(source.clone()).ok().unwrap() - }); + b.iter(|| Template::compile(SOURCE.clone()).ok().unwrap()); } #[bench] fn render_template(b: &mut test::Bencher) { - let source = load_template_source("./benches/template.hbs").ok().unwrap(); - let mut handlebars = Handlebars::new(); - handlebars.register_template_string("table", source) - .ok().expect("Invalid template format"); + handlebars.register_template_string("table", SOURCE.to_string()) + .ok() + .expect("Invalid template format"); let data = make_data(); - b.iter(|| { - handlebars.render("table", &data).ok().unwrap() - }) + b.iter(|| handlebars.render("table", &data).ok().unwrap()) } diff --git a/benches/template.hbs b/benches/template.hbs deleted file mode 100644 index 908185454..000000000 --- a/benches/template.hbs +++ /dev/null @@ -1,15 +0,0 @@ - - - {{year}} - - -

CSL {{year}}

-
    - {{#each teams}} -
  • - {{name}}: {{score}} -
  • - {{/each}} -
- -