wireframe 0.4.0-alpha1

Simplify building servers and clients for custom binary protocols.
Documentation
//! Counters for the bytes copied by the zero-copy migration's seams.
//!
//! Allocation counters cannot see a copy into an already-allocated buffer, so
//! the zero-copy programme needs a byte-level metric as well. The seams that
//! copy are the serializer materialization, the `Bytes::from` hand-off in
//! `src/app/outbound_encoding.rs`, the codec write into the transport buffer,
//! and the inbound deserialization that materializes an envelope's payload.
//! Each seam reports through this module.
//!
//! The counters are compiled only when the `benchmark-counters` feature is
//! enabled. Without it every recorder is an empty inline function that the
//! optimizer removes, so production builds pay nothing for the call sites and
//! [`snapshot`] reports zeros. A harness that reports copied bytes must
//! therefore enable the feature; a test asserts that a known copy is visible
//! only in that configuration.
//!
//! Counters are process-global. A measurement must [`reset`] before its timed
//! region, read [`snapshot`] after it, and must not run concurrently with
//! another measurement in the same process.
//!
//! # Examples
//!
//! ```ignore
//! use wireframe::benchmark_counters::{self, CopiedBytes};
//!
//! benchmark_counters::reset();
//! let frame = codec.encode(payload);
//! let copied = benchmark_counters::snapshot();
//! assert_eq!(copied.framed, payload.len() as u64);
//! ```
//!
//! The example is marked `ignore` because it is only meaningful with the
//! `benchmark-counters` feature enabled.

#[cfg(feature = "benchmark-counters")]
use std::sync::atomic::{AtomicU64, Ordering};

use bytes::Bytes;

/// Bytes copied at each in-scope seam since the last [`reset`].
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CopiedBytes {
    /// Bytes written by a serializer into the `Vec<u8>` it returns.
    pub serialized: u64,
    /// Bytes moved when the serializer vector becomes a `Bytes` hand-off.
    pub handoff: u64,
    /// Bytes copied into the codec write buffer while framing.
    pub framed: u64,
    /// Bytes copied while an inbound frame became an owned envelope payload.
    pub deserialized: u64,
}

impl CopiedBytes {
    /// Sum of every counted seam.
    ///
    /// # Examples
    ///
    /// ```
    /// use wireframe::benchmark_counters::CopiedBytes;
    ///
    /// let copied = CopiedBytes {
    ///     serialized: 4,
    ///     handoff: 4,
    ///     framed: 4,
    ///     deserialized: 4,
    /// };
    /// assert_eq!(copied.total(), 16);
    /// ```
    #[must_use]
    pub const fn total(&self) -> u64 {
        self.serialized
            .saturating_add(self.handoff)
            .saturating_add(self.framed)
            .saturating_add(self.deserialized)
    }

    /// Per-operation copy count for a measured batch.
    ///
    /// Returns `None` when `operations` is zero, because a batch that performed
    /// no work has no per-operation cost to report.
    ///
    /// # Examples
    ///
    /// ```
    /// use wireframe::benchmark_counters::CopiedBytes;
    ///
    /// let copied = CopiedBytes {
    ///     serialized: 64,
    ///     handoff: 0,
    ///     framed: 64,
    ///     deserialized: 0,
    /// };
    /// let per_operation = copied.per_operation(2).expect("two operations");
    /// assert_eq!(per_operation.framed, 32);
    /// ```
    // Not `const`: `?` on `Option` inside a constant function is still an
    // unstable feature, and no caller needs this at compile time.
    #[must_use]
    pub fn per_operation(&self, operations: u64) -> Option<Self> {
        let serialized = self.serialized.checked_div(operations)?;
        let handoff = self.handoff.checked_div(operations)?;
        let framed = self.framed.checked_div(operations)?;
        let deserialized = self.deserialized.checked_div(operations)?;
        Some(Self {
            serialized,
            handoff,
            framed,
            deserialized,
        })
    }
}

/// A counter that accumulates when the feature is on and does nothing when it
/// is off.
///
/// Keeping the `cfg` switch inside this type lets each recorder stay a
/// one-line call, instead of repeating the same conditional block at every
/// seam.
#[cfg(feature = "benchmark-counters")]
struct Counter(AtomicU64);

#[cfg(feature = "benchmark-counters")]
impl Counter {
    /// Creates a counter at zero.
    const fn new() -> Self { Self(AtomicU64::new(0)) }

    /// Adds `bytes` to the accumulated total.
    #[inline]
    fn add(&self, bytes: usize) { self.0.fetch_add(bytes as u64, Ordering::Relaxed); }

    /// Returns the bytes accumulated so far.
    fn load(&self) -> u64 { self.0.load(Ordering::Relaxed) }

    /// Resets the accumulated total to zero.
    fn clear(&self) { self.0.store(0, Ordering::Relaxed); }
}

/// Inert stand-in for [`Counter`] in a build without the feature. It is a
/// zero-sized type, so the statics below occupy no storage.
#[cfg(not(feature = "benchmark-counters"))]
struct Counter;

#[cfg(not(feature = "benchmark-counters"))]
impl Counter {
    /// Creates the inert counter.
    const fn new() -> Self { Self }

    /// Discards `bytes`; nothing is accumulated without the feature.
    #[inline]
    fn add(&self, bytes: usize) { let _ = bytes; }

    /// Always reports zero, because nothing was accumulated.
    fn load(&self) -> u64 { 0 }

    /// Does nothing, because there is no total to reset.
    fn clear(&self) {}
}

/// Serializer materialization counter.
static SERIALIZED_BYTES: Counter = Counter::new();

/// Serializer-to-`Bytes` hand-off counter.
static HANDOFF_BYTES: Counter = Counter::new();

/// Codec framing counter.
static FRAMED_BYTES: Counter = Counter::new();

/// Inbound deserialization counter.
static DESERIALIZED_BYTES: Counter = Counter::new();

/// Number of times the serializer-to-`Bytes` hand-off seam ran.
///
/// [`CopiedBytes::handoff`] counts bytes, and `bytes` 1.12.1 shares the vector
/// at every hand-off, so that count stays zero whether the seam ran or was
/// bypassed. This count distinguishes the two, which is what lets the client
/// send path's regression test fail if it goes back to calling `Bytes::from`
/// directly.
static HANDOFF_SEAM_CALLS: Counter = Counter::new();

/// Records bytes written into a serializer's returned vector.
#[inline]
pub fn record_serialized(bytes: usize) { SERIALIZED_BYTES.add(bytes); }

/// Records bytes copied by the serializer-to-`Bytes` hand-off.
#[inline]
pub fn record_handoff(bytes: usize) { HANDOFF_BYTES.add(bytes); }

/// Records bytes copied into a codec write buffer.
#[inline]
pub fn record_framed(bytes: usize) { FRAMED_BYTES.add(bytes); }

/// Records bytes copied into a decoded envelope's owned payload.
///
/// The inbound path materializes a frame's payload into its own `Vec<u8>`, so
/// an inbound message copies its payload once even though the reader's frame
/// borrowed it. Recording from the decoded envelope's payload length keeps the
/// count independent of the frame's metadata bytes.
#[inline]
pub fn record_deserialized(bytes: usize) { DESERIALIZED_BYTES.add(bytes); }

/// Returns the counters accumulated since the last [`reset`].
///
/// Every field is zero when the `benchmark-counters` feature is disabled, so a
/// caller cannot mistake a disabled counter for a measured zero-copy path.
#[must_use]
pub fn snapshot() -> CopiedBytes {
    CopiedBytes {
        serialized: SERIALIZED_BYTES.load(),
        handoff: HANDOFF_BYTES.load(),
        framed: FRAMED_BYTES.load(),
        deserialized: DESERIALIZED_BYTES.load(),
    }
}

/// Clears every counter.
///
/// Call this immediately before the timed region so setup work cannot be
/// mistaken for per-operation cost.
pub fn reset() {
    SERIALIZED_BYTES.clear();
    HANDOFF_BYTES.clear();
    FRAMED_BYTES.clear();
    DESERIALIZED_BYTES.clear();
    HANDOFF_SEAM_CALLS.clear();
}

/// Number of times the serializer-to-`Bytes` hand-off seam has run since the
/// last [`reset`].
///
/// A hand-off written directly as `Bytes::from` leaves this unchanged even
/// though it copies just as few bytes, so the bench gate's regression test can
/// assert that the send path went through the seam. It is empty
/// instrumentation for that gate rather than an API, so it is hidden from the
/// rendered documentation.
#[doc(hidden)]
#[must_use]
pub fn handoff_seam_calls() -> u64 { HANDOFF_SEAM_CALLS.load() }

/// Converts a serializer vector into the codec hand-off container.
///
/// `bytes` 1.12.1 never copies the payload here. With no spare capacity it
/// takes the vector's boxed slice in place; with spare capacity it allocates a
/// small shared control block that still points at the vector's buffer. The
/// pointer comparison therefore records zero, and the counter is a guard: a
/// future `bytes` change, or a hand-off written some other way, would show up
/// as a non-zero `handoff` instead of passing unmeasured. The seam also counts
/// its own calls (see [`handoff_seam_calls`]), so a caller that bypasses it is
/// detectable even while no copy is recorded.
///
/// # Examples
///
/// ```
/// use wireframe::benchmark_counters::bytes_from_vec;
///
/// let mut spare_capacity = Vec::with_capacity(64);
/// spare_capacity.extend_from_slice(&[1, 2, 3, 4]);
/// let bytes = bytes_from_vec(spare_capacity);
/// assert_eq!(bytes.as_ref(), &[1, 2, 3, 4]);
/// ```
#[must_use]
pub fn bytes_from_vec(bytes: Vec<u8>) -> Bytes {
    HANDOFF_SEAM_CALLS.add(1);
    #[cfg(feature = "benchmark-counters")]
    {
        let source = bytes.as_ptr();
        let len = bytes.len();
        let handoff = Bytes::from(bytes);
        if handoff.as_ptr() != source {
            record_handoff(len);
        }
        handoff
    }
    #[cfg(not(feature = "benchmark-counters"))]
    {
        Bytes::from(bytes)
    }
}

#[cfg(test)]
mod tests {
    //! Unit tests for counter accounting.

    use super::*;

    #[test]
    fn per_operation_reports_none_for_an_empty_batch() {
        assert!(
            CopiedBytes::default().per_operation(0).is_none(),
            "assertion failed: CopiedBytes::default().per_operation(0).is_none()"
        );
    }

    #[test]
    fn per_operation_divides_each_seam() {
        let copied = CopiedBytes {
            serialized: 96,
            handoff: 48,
            framed: 32,
            deserialized: 64,
        };
        let per_operation = copied.per_operation(4).expect("four operations");
        assert_eq!(
            per_operation,
            CopiedBytes {
                serialized: 24,
                handoff: 12,
                framed: 8,
                deserialized: 16,
            },
            "per_operation should equal CopiedBytes {{ serialized: 24, handoff: 12, framed: 8, \
             deserialized: 16, }}"
        );
    }

    #[cfg(feature = "benchmark-counters")]
    #[test]
    fn handoff_preserves_the_payload_pointer() {
        // Pointer identity is the property the recorder relies on, so assert it
        // directly rather than through the process-global counter, which other
        // tests in this binary also write to.
        let mut spare_capacity = Vec::with_capacity(64);
        spare_capacity.extend_from_slice(&[1, 2, 3, 4]);
        let source = spare_capacity.as_ptr();
        let bytes = bytes_from_vec(spare_capacity);
        assert_eq!(bytes.as_ptr(), source, "bytes.as_ptr() should equal source");

        let exact_capacity = vec![1, 2, 3, 4];
        assert_eq!(
            exact_capacity.len(),
            exact_capacity.capacity(),
            "exact_capacity.len() should equal exact_capacity.capacity()"
        );
        let exact_source = exact_capacity.as_ptr();
        let exact_bytes = bytes_from_vec(exact_capacity);
        assert_eq!(
            exact_bytes.as_ptr(),
            exact_source,
            "exact_bytes.as_ptr() should equal exact_source"
        );
    }

    #[cfg(feature = "benchmark-counters")]
    #[test]
    fn record_handoff_accumulates_bytes() {
        // The hand-off records nothing with the current `bytes` version, so the
        // counter is exercised directly. A delta is used rather than a reset so
        // a test running concurrently can only add to the counter, never
        // invalidate the expectation.
        let before = snapshot().handoff;
        record_handoff(7);
        record_handoff(5);
        assert_eq!(
            snapshot().handoff - before,
            12,
            "snapshot().handoff - before should equal 12"
        );
    }
}