summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2022-01-05 12:39:50 +0200
committerLars Wirzenius <liw@liw.fi>2022-01-05 12:39:50 +0200
commit6e9e7e7af213c9258bc4009e05bb9a3ba5d172b6 (patch)
treedc8e1b2216c9bcd50a95623ab5dec32921757174
parentd438cc0b64edf207ad565eede5c0ed829131a27b (diff)
downloadobnam-benchmark-6e9e7e7af213c9258bc4009e05bb9a3ba5d172b6.tar.gz
feat: output file for run command
Sponsored-by: author
-rw-r--r--src/bin/obnam-benchmark.rs23
-rw-r--r--src/result.rs10
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<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(())
}
}
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);
}