From 82968fdf4ae02ea6931b0166c519376127723e03 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 5 Dec 2021 19:31:42 +0200 Subject: feat: record name of benchmark for each measurement Sponsored-by: author --- src/bin/obnam-benchmark.rs | 3 ++- src/result.rs | 6 ++++-- src/step.rs | 8 ++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/bin/obnam-benchmark.rs b/src/bin/obnam-benchmark.rs index 21012b3..9a326ec 100644 --- a/src/bin/obnam-benchmark.rs +++ b/src/bin/obnam-benchmark.rs @@ -53,8 +53,9 @@ impl Run { fn run(&self) -> anyhow::Result<()> { let spec = Specification::from_file(&self.spec)?; let mut result = Result::default(); + let mut current_benchmark = None; for step in spec.steps() { - if let Some(m) = step.execute()? { + if let Some(m) = step.execute(&mut current_benchmark)? { result.push(m); } } diff --git a/src/result.rs b/src/result.rs index f5d8598..cb23d35 100644 --- a/src/result.rs +++ b/src/result.rs @@ -7,6 +7,7 @@ pub struct Result { #[derive(Debug, Serialize)] pub struct Measurement { + benchmark: String, op: Operation, ms: u128, } @@ -24,7 +25,8 @@ impl Result { } impl Measurement { - pub fn new(op: Operation, ms: u128) -> Self { - Self { op, ms } + pub fn new(benchmark: &str, op: Operation, ms: u128) -> Self { + let benchmark = benchmark.to_string(); + Self { benchmark, op, ms } } } diff --git a/src/step.rs b/src/step.rs index 391e7d9..0ca7729 100644 --- a/src/step.rs +++ b/src/step.rs @@ -50,14 +50,16 @@ impl Step { } } - pub fn execute(&self) -> Result, StepError> { + pub fn execute(&self, current: &mut Option) -> Result, StepError> { let now = Instant::now(); let op = match self { Self::Start(name) => { + *current = Some(name.to_string()); println!("start benchmark {}", name); None } Self::Stop(name) => { + *current = None; println!("stop benchmark {}", name); None } @@ -88,7 +90,9 @@ impl Step { let m = if let Some(op) = op { let ms = now.elapsed().as_millis(); - Some(Measurement::new(op, ms)) + assert!(current.is_some()); + let current = current.as_ref().unwrap(); + Some(Measurement::new(current, op, ms)) } else { None }; -- cgit v1.2.1