euv-performance 0.48.1

Reproducible CPU, layout, rasterization, and allocation benchmarks for the cross-platform euv runtime.
Documentation
use super::*;

/// Constructors and allocation attachment builders for `BenchMetric`.
impl BenchMetric {
    /// Constructs a `BenchMetric` from elapsed wall duration and iteration count.
    ///
    /// # Arguments
    ///
    /// - `&str` - Canonical benchmark metric name.
    /// - `usize` - Number of completed measured iterations.
    /// - `Duration` - Total elapsed wall duration across iterations.
    /// - `String` - Extra verification metadata string.
    ///
    /// # Returns
    ///
    /// - `Self` - Populated benchmark metric ready for optional allocation attachment.
    #[must_use]
    pub fn from_elapsed(name: &str, iterations: usize, elapsed: Duration, detail: String) -> Self {
        let iters: usize = iterations.max(1);
        let total_nanos: f64 = elapsed.as_nanos().max(1) as f64;
        let ns_per_op: f64 = total_nanos / (iters as f64);
        let ops_per_sec: f64 = 1_000_000_000.0 / ns_per_op.max(1.0);
        Self::new(
            name.to_string(),
            iters,
            elapsed.as_micros() as u64,
            ns_per_op,
            ops_per_sec,
            detail,
        )
    }

    /// Attaches measured steady-state `AllocationSnapshot` counters to this `BenchMetric`.
    ///
    /// # Arguments
    ///
    /// - `Self` - Benchmark metric builder.
    /// - `AllocationSnapshot` - Measured heap allocation counts and bytes across all iterations.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated metric populated with total and per-iteration allocation stats.
    #[must_use]
    pub fn with_allocations(mut self, snapshot: AllocationSnapshot) -> Self {
        let Self {
            iterations,
            alloc_count,
            bytes_allocated,
            allocs_per_op,
            bytes_per_op,
            ..
        } = &mut self;
        let denom: f64 = (*iterations).max(1) as f64;
        *alloc_count = snapshot.get_alloc_count();
        *bytes_allocated = snapshot.get_bytes_allocated();
        *allocs_per_op = (snapshot.get_alloc_count() as f64) / denom;
        *bytes_per_op = (snapshot.get_bytes_allocated() as f64) / denom;
        self
    }
}