diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-01-05 12:39:50 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-01-05 12:39:50 +0200 |
commit | 6e9e7e7af213c9258bc4009e05bb9a3ba5d172b6 (patch) | |
tree | dc8e1b2216c9bcd50a95623ab5dec32921757174 /src/bin/obnam-benchmark.rs | |
parent | d438cc0b64edf207ad565eede5c0ed829131a27b (diff) | |
download | obnam-benchmark-6e9e7e7af213c9258bc4009e05bb9a3ba5d172b6.tar.gz |
feat: output file for run command
Sponsored-by: author
Diffstat (limited to 'src/bin/obnam-benchmark.rs')
-rw-r--r-- | src/bin/obnam-benchmark.rs | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/src/bin/obnam-benchmark.rs b/src/bin/obnam-benchmark.rs index 5df2aeb..41118cb 100644 --- a/src/bin/obnam-benchmark.rs +++ b/src/bin/obnam-benchmark.rs @@ -4,6 +4,7 @@ use obnam_benchmark::result::SuiteMeasurements; use obnam_benchmark::specification::Specification; use obnam_benchmark::suite::Suite; use std::fs::File; +use std::io::Write; use std::path::PathBuf; use std::process::exit; use structopt::StructOpt; @@ -61,9 +62,14 @@ struct Run { #[structopt(parse(from_os_str))] spec: PathBuf, - /// Name of file where the results will be written. + /// Name of file where the results will be written. Default is + /// stdout. #[structopt(long, parse(from_os_str))] - output: PathBuf, + output: Option<PathBuf>, + + /// Construct output filename from benchmark run metadata. + #[structopt(short, long)] + auto: bool, } impl Run { @@ -76,9 +82,16 @@ impl Run { for step in spec.steps().iter() { result.push(suite.execute(step)?); } - debug!("writing results to {}", self.output.display()); - let output = File::create(&self.output)?; - serde_json::to_writer_pretty(&output, &result)?; + + let output = serde_json::to_string_pretty(&result)?; + if let Some(filename) = &self.output { + std::fs::write(filename, output)? + } else if self.auto { + let filename = format!("{}_{}.json", result.hostname(), result.timestamp()); + std::fs::write(filename, output)? + } else { + std::io::stdout().write_all(output.as_bytes())?; + }; Ok(()) } } |