Skip to content

Commit

Permalink
Adds a benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
tailhook committed Sep 23, 2017
1 parent 2334449 commit 91e5468
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
85 changes: 85 additions & 0 deletions benches/swindon_dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#![feature(test)]

extern crate trimmer;
extern crate test;
extern crate serde_json;

use std::str::FromStr;
use trimmer::{Template, Context};
use serde_json::Value;
use test::Bencher;


fn template() -> Template {
trimmer::Parser::new().parse(r###"## syntax: indent
<!DOCTYPE html>
<html>
<head>
<title>Listing of the directory {{ path }}</title>
</head>
<body>
<h1>Listing of the directory {{ path }}</h1>
<ul>
## for entry in entries
<li>
## if entry.is_dir
<a href="{{ path }}/{{ entry.name }}/">{{ entry.name }}/</a>
## else
<a href="{{ path }}/{{ entry.name }}">{{ entry.name }}</a>
## endif
{# TODO(tailhook) add some file attributes #}
</li>
## endfor
</ul>
<hr>
<p>Yours faithfully,<br>
swindon web server
</p>
</body>
</html>
"###).unwrap()
}

fn data() -> Value {
Value::from_str(r#"[
{"name": "file1", "is_dir": false},
{"name": "file2", "is_dir": false},
{"name": "file3", "is_dir": false},
{"name": "file4", "is_dir": false},
{"name": "file5", "is_dir": false},
{"name": "file6", "is_dir": false},
{"name": "dir1", "is_dir": true},
{"name": "dir2", "is_dir": true},
{"name": "dir3", "is_dir": true},
{"name": "dir4", "is_dir": true},
{"name": "dir5", "is_dir": true},
{"name": "file7", "is_dir": false},
{"name": "file8", "is_dir": false},
{"name": "file9", "is_dir": false},
{"name": "file10", "is_dir": false}
]"#).unwrap()
}

#[test]
fn works() {
let tpl = template();
let path = "/my/path";
let data = data();
let mut ctx = Context::new();
ctx.set("path", &path);
ctx.set("entries", &data);
tpl.render(&ctx).unwrap();
}

#[bench]
fn render(b: &mut Bencher) {
let tpl = template();
let path = "/my/path";
let data = data();
let mut ctx = Context::new();
ctx.set("path", &path);
ctx.set("entries", &data);
b.iter(|| {
tpl.render(&ctx).unwrap();
});
}
19 changes: 19 additions & 0 deletions vagga.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ commands:
prerequisites: [test-no-default-features]
run: [cargo, test]

bench: !Command
description: Run benchmarks
container: bench
environ: { RUST_BACKTRACE: 1 }
run: [cargo, bench]

_bulk: !Command
description: Run `bulk` command (for version bookkeeping)
container: ubuntu
Expand Down Expand Up @@ -71,6 +77,19 @@ containers:
HOME: /work/target
USER: pc

bench:
setup:
- !Ubuntu xenial
- !Install [ca-certificates, wget, build-essential]
- !TarInstall
url: https://static.rust-lang.org/dist/rust-nightly-x86_64-unknown-linux-gnu.tar.gz
script: |
./install.sh --prefix=/usr \
--components=rustc,rust-std-x86_64-unknown-linux-gnu,cargo
environ:
HOME: /work/target
USER: pc

doc:
setup:
- !Alpine v3.4
Expand Down

0 comments on commit 91e5468

Please sign in to comment.