From 6e9e7e7af213c9258bc4009e05bb9a3ba5d172b6 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 5 Jan 2022 12:39:50 +0200 Subject: feat: output file for run command Sponsored-by: author --- src/bin/obnam-benchmark.rs | 23 ++++++++++++++++++----- src/result.rs | 10 +++++++++- 2 files changed, 27 insertions(+), 6 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, + + /// 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(()) } } diff --git a/src/result.rs b/src/result.rs index 4a0277e..22a49a0 100644 --- a/src/result.rs +++ b/src/result.rs @@ -67,13 +67,21 @@ impl SuiteMeasurements { measurements: vec![], obnam_version, obnam_benchmark_version: render_testament!(TESTAMENT), - benchmark_started: Utc::now().to_string(), + benchmark_started: Utc::now().format("%Y-%m-%dT%H%M%S").to_string(), hostname: hostname.to_string(), host_ram: mem.mem_total, host_cpus: cpu.num_cores(), }) } + pub fn hostname(&self) -> &str { + &self.hostname + } + + pub fn timestamp(&self) -> &str { + &self.benchmark_started + } + pub fn push(&mut self, m: OpMeasurements) { self.measurements.push(m); } -- cgit v1.2.1