Skip to main content

euv_performance/suite/
impl.rs

1use super::*;
2
3/// Constructors and allocation attachment builders for `BenchMetric`.
4impl BenchMetric {
5    /// Constructs a `BenchMetric` from elapsed wall duration and iteration count.
6    ///
7    /// # Arguments
8    ///
9    /// - `&str` - Canonical benchmark metric name.
10    /// - `usize` - Number of completed measured iterations.
11    /// - `Duration` - Total elapsed wall duration across iterations.
12    /// - `String` - Extra verification metadata string.
13    ///
14    /// # Returns
15    ///
16    /// - `Self` - Populated benchmark metric ready for optional allocation attachment.
17    #[must_use]
18    pub fn from_elapsed(name: &str, iterations: usize, elapsed: Duration, detail: String) -> Self {
19        let iters: usize = iterations.max(1);
20        let total_nanos: f64 = elapsed.as_nanos().max(1) as f64;
21        let ns_per_op: f64 = total_nanos / (iters as f64);
22        let ops_per_sec: f64 = 1_000_000_000.0 / ns_per_op.max(1.0);
23        Self::new(
24            name.to_string(),
25            iters,
26            elapsed.as_micros() as u64,
27            ns_per_op,
28            ops_per_sec,
29            detail,
30        )
31    }
32
33    /// Attaches measured steady-state `AllocationSnapshot` counters to this `BenchMetric`.
34    ///
35    /// # Arguments
36    ///
37    /// - `Self` - Benchmark metric builder.
38    /// - `AllocationSnapshot` - Measured heap allocation counts and bytes across all iterations.
39    ///
40    /// # Returns
41    ///
42    /// - `Self` - Updated metric populated with total and per-iteration allocation stats.
43    #[must_use]
44    pub fn with_allocations(mut self, snapshot: AllocationSnapshot) -> Self {
45        let Self {
46            iterations,
47            alloc_count,
48            bytes_allocated,
49            allocs_per_op,
50            bytes_per_op,
51            ..
52        } = &mut self;
53        let denom: f64 = (*iterations).max(1) as f64;
54        *alloc_count = snapshot.get_alloc_count();
55        *bytes_allocated = snapshot.get_bytes_allocated();
56        *allocs_per_op = (snapshot.get_alloc_count() as f64) / denom;
57        *bytes_per_op = (snapshot.get_bytes_allocated() as f64) / denom;
58        self
59    }
60}