From dd72c4af5c325ce4d1285428eeccb7d539ecd4ac Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 6 Apr 2020 08:17:47 +0200 Subject: [PATCH] Add option to specify an output file for the benchmarking results --- Cargo.toml | 2 +- src/benching.rs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 5360ea7..6f05e0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "benchlib-rs" -version = "0.3.1" +version = "0.3.2" authors = ["Trivernis "] edition = "2018" license-file = "LICENSE" diff --git a/src/benching.rs b/src/benching.rs index 3f0395f..bb45017 100644 --- a/src/benching.rs +++ b/src/benching.rs @@ -2,6 +2,8 @@ use std::time::{Duration, Instant}; use std::fmt::{self, Display}; use rayon::prelude::*; use termion::{color, style}; +use std::io::{BufWriter, Write}; +use std::fs::File; #[derive(Debug, Clone)] pub struct BenchVec { @@ -127,6 +129,7 @@ pub struct Bencher { iterations: usize, max_auto_iterations: usize, bench_duration: Duration, + writer: Option> } impl Bencher { @@ -136,9 +139,11 @@ impl Bencher { measurements: Vec::new(), iterations: 100, max_auto_iterations: 10000, + writer: None, } } + /// Calculates the time it takes to measure a benchmark fn calculate_bench_duration() -> Duration{ let mut durations = BenchVec::new(); for _ in 0..1000 { @@ -204,6 +209,9 @@ impl Bencher { } } 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 @@ -233,4 +241,9 @@ impl Bencher { self } + + /// Adds a file to write the output to + pub fn write_output_to(&mut self, writer: BufWriter) { + self.writer = Some(writer); + } } \ No newline at end of file