Add option to specify an output file for the benchmarking results

master
Trivernis 5 years ago
parent 842401043f
commit dd72c4af5c

@ -1,6 +1,6 @@
[package] [package]
name = "benchlib-rs" name = "benchlib-rs"
version = "0.3.1" version = "0.3.2"
authors = ["Trivernis <trivernis@gmail.com>"] authors = ["Trivernis <trivernis@gmail.com>"]
edition = "2018" edition = "2018"
license-file = "LICENSE" license-file = "LICENSE"

@ -2,6 +2,8 @@ use std::time::{Duration, Instant};
use std::fmt::{self, Display}; use std::fmt::{self, Display};
use rayon::prelude::*; use rayon::prelude::*;
use termion::{color, style}; use termion::{color, style};
use std::io::{BufWriter, Write};
use std::fs::File;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct BenchVec { pub struct BenchVec {
@ -127,6 +129,7 @@ pub struct Bencher {
iterations: usize, iterations: usize,
max_auto_iterations: usize, max_auto_iterations: usize,
bench_duration: Duration, bench_duration: Duration,
writer: Option<BufWriter<File>>
} }
impl Bencher { impl Bencher {
@ -136,9 +139,11 @@ impl Bencher {
measurements: Vec::new(), measurements: Vec::new(),
iterations: 100, iterations: 100,
max_auto_iterations: 10000, max_auto_iterations: 10000,
writer: None,
} }
} }
/// Calculates the time it takes to measure a benchmark
fn calculate_bench_duration() -> Duration{ fn calculate_bench_duration() -> Duration{
let mut durations = BenchVec::new(); let mut durations = BenchVec::new();
for _ in 0..1000 { for _ in 0..1000 {
@ -204,6 +209,9 @@ impl Bencher {
} }
} }
println!("Result: {}", durations); println!("Result: {}", durations);
if let Some(writer) = &mut self.writer {
let _ = writer.write(format!("{}\t{:?}\t{:.2}ns\n", name, durations.average(), durations.standard_deviation()).as_bytes());
}
self.measurements.push(durations); self.measurements.push(durations);
self self
@ -233,4 +241,9 @@ impl Bencher {
self self
} }
/// Adds a file to write the output to
pub fn write_output_to(&mut self, writer: BufWriter<File>) {
self.writer = Some(writer);
}
} }
Loading…
Cancel
Save