Skip to content
Snippets Groups Projects
Verified Commit e74726ae authored by STEVAN Antoine's avatar STEVAN Antoine :crab:
Browse files

add support for a log file

parent 5a54e85a
No related branches found
No related tags found
No related merge requests found
......@@ -66,17 +66,25 @@ struct Args {
#[arg(short, long)]
multiple: bool,
#[arg(short, long)]
log_file: Option<String>,
}
fn main() {
let args = Args::parse();
let bencher = plnk::Bencher::new(args.nb_measurements);
let bencher = match args.log_file {
Some(path) => plnk::Bencher::new(args.nb_measurements)
.with_file(path.into())
.overwrite(),
None => plnk::Bencher::new(args.nb_measurements),
};
if args.multiple {
arithmetic_multiple(&bencher.with_name("arithmetic"));
} else {
arithmetic(&bencher.with_name("arithmetic"));
}
random(&bencher.with_name("random"));
random(&bencher.with_name("random").append());
}
#![doc = include_str!("../README.md")]
use std::time::{Duration, Instant};
use std::{
fs::{create_dir_all, File},
io::Write,
path::PathBuf,
time::{Duration, Instant},
};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
......@@ -13,6 +18,9 @@ pub struct Bencher {
name: String,
quiet: bool,
file: Option<PathBuf>,
append: bool,
}
impl Bencher {
......@@ -21,6 +29,8 @@ impl Bencher {
nb_measurements,
name: "".to_string(),
quiet: false,
file: None,
append: false,
}
}
......@@ -30,6 +40,39 @@ impl Bencher {
nb_measurements: self.nb_measurements,
name: name.to_string(),
quiet: self.quiet,
file: self.file.clone(),
append: self.append,
}
}
/// add a file to a bencher
pub fn with_file(&self, path: PathBuf) -> Self {
Self {
nb_measurements: self.nb_measurements,
name: self.name.clone(),
quiet: self.quiet,
file: Some(path),
append: self.append,
}
}
/// append to file
pub fn append(&self) -> Self {
Self {
nb_measurements: self.nb_measurements,
name: self.name.clone(),
quiet: self.quiet,
file: self.file.clone(),
append: true,
}
}
/// overwrite file
pub fn overwrite(&self) -> Self {
Self {
nb_measurements: self.nb_measurements,
name: self.name.clone(),
quiet: self.quiet,
file: self.file.clone(),
append: false,
}
}
......@@ -82,7 +125,29 @@ where
pb.inc(1);
}
println!("{}", label! { label: label, name: b.name, times: times });
let output = format!("{}", label! { label: label, name: b.name, times: times });
if let Some(path) = &b.file {
create_dir_all(path.parent().unwrap()).expect(&format!(
"could not create parent directory of log file '{}'",
path.to_str().unwrap()
));
let mut f = File::options()
.write(true)
.append(b.append)
.truncate(!b.append)
.create(true)
.open(path)
.expect(&format!(
"could not open log file '{}'",
path.to_str().unwrap()
));
writeln!(&mut f, "{}", output).expect(&format!(
"could not write to log file '{}'",
path.to_str().unwrap()
));
} else {
println!("{}", output);
}
}
/// run a given piece of code a bunch of times and output the measurements
......@@ -105,6 +170,8 @@ pub fn bench_multiple(b: &Bencher, benches: Vec<(String, Box<dyn Fn() -> Duratio
let pb_main = mpb.add(ProgressBar::new(benches.len() as u64).with_style(style.clone()));
pb_main.set_message(b.name.clone());
let mut append = b.append;
for (label, f) in benches.iter() {
let pb = mpb.add(ProgressBar::new(b.nb_measurements as u64).with_style(style.clone()));
pb.set_message(label.to_string());
......@@ -118,7 +185,31 @@ pub fn bench_multiple(b: &Bencher, benches: Vec<(String, Box<dyn Fn() -> Duratio
pb_main.inc(1);
pb.finish_and_clear();
println!("{}", label! { label: label, name: b.name, times: times });
let output = format!("{}", label! { label: label, name: b.name, times: times });
if let Some(path) = &b.file {
create_dir_all(path.parent().unwrap()).expect(&format!(
"could not create parent directory of log file '{}'",
path.to_str().unwrap()
));
println!("append: {}", append);
let mut f = File::options()
.write(true)
.append(append)
.truncate(!append)
.create(true)
.open(path)
.expect(&format!(
"could not open log file '{}'",
path.to_str().unwrap()
));
writeln!(&mut f, "{}", output).expect(&format!(
"could not write to log file '{}'",
path.to_str().unwrap()
));
append = true;
} else {
println!("{}", output);
}
}
pb_main.finish_with_message(format!("{} done", b.name));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment