per-sample option in bench

This commit is contained in:
NikVolf 2016-11-24 21:32:36 +03:00
parent 7cba467342
commit 76ea2abf85
2 changed files with 19 additions and 1 deletions

View File

@ -8,6 +8,8 @@ pub fn fetch(benchmark: &mut Benchmark) {
// params
const BLOCKS: usize = 1000;
benchmark.samples(BLOCKS);
// test setup
let path = RandomTempPath::create_dir();
let store = Storage::new(path.as_path()).unwrap();
@ -49,6 +51,7 @@ pub fn fetch(benchmark: &mut Benchmark) {
pub fn write(benchmark: &mut Benchmark) {
// params
const BLOCKS: usize = 1000;
benchmark.samples(BLOCKS);
// setup
let path = RandomTempPath::create_dir();
@ -84,6 +87,7 @@ pub fn write(benchmark: &mut Benchmark) {
pub fn reorg_short(benchmark: &mut Benchmark) {
// params
const BLOCKS: usize = 1000;
benchmark.samples(BLOCKS);
// setup
let path = RandomTempPath::create_dir();
@ -166,6 +170,8 @@ pub fn write_heavy(benchmark: &mut Benchmark) {
const BLOCKS: usize = 100;
const TRANSACTIONS: usize = 100;
benchmark.samples(BLOCKS);
// test setup
let path = RandomTempPath::create_dir();
let store = Storage::new(path.as_path()).unwrap();

View File

@ -14,6 +14,7 @@ use std::str;
pub struct Benchmark {
start: Option<PreciseTime>,
end: Option<PreciseTime>,
samples: Option<usize>,
}
impl Benchmark {
@ -28,6 +29,10 @@ impl Benchmark {
pub fn evaluate(&self) -> Duration {
self.start.expect("benchmarch never ended").to(self.end.expect("benchmark never started"))
}
pub fn samples(&mut self, samples: usize) {
self.samples = Some(samples);
}
}
fn decimal_mark(s: String) -> String {
@ -44,7 +49,14 @@ fn run_benchmark<F>(name: &str, f: F) where F: FnOnce(&mut Benchmark) {
let mut benchmark = Benchmark::default();
f(&mut benchmark);
println!("{} ns", decimal_mark(format!("{}", benchmark.evaluate().num_nanoseconds().unwrap())));
if let Some(samples) = benchmark.samples {
println!("{} ns/sample",
decimal_mark(format!("{}", benchmark.evaluate().num_nanoseconds().unwrap() / samples as i64)),
);
}
else {
println!("{} ns", decimal_mark(format!("{}", benchmark.evaluate().num_nanoseconds().unwrap())));
}
}
macro_rules! benchmark {