#[cfg(feature = "benchmark-counters")]
use std::sync::atomic::{AtomicU64, Ordering};
use bytes::Bytes;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CopiedBytes {
pub serialized: u64,
pub handoff: u64,
pub framed: u64,
pub deserialized: u64,
}
impl CopiedBytes {
#[must_use]
pub const fn total(&self) -> u64 {
self.serialized
.saturating_add(self.handoff)
.saturating_add(self.framed)
.saturating_add(self.deserialized)
}
#[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,
})
}
}
#[cfg(feature = "benchmark-counters")]
struct Counter(AtomicU64);
#[cfg(feature = "benchmark-counters")]
impl Counter {
const fn new() -> Self { Self(AtomicU64::new(0)) }
#[inline]
fn add(&self, bytes: usize) { self.0.fetch_add(bytes as u64, Ordering::Relaxed); }
fn load(&self) -> u64 { self.0.load(Ordering::Relaxed) }
fn clear(&self) { self.0.store(0, Ordering::Relaxed); }
}
#[cfg(not(feature = "benchmark-counters"))]
struct Counter;
#[cfg(not(feature = "benchmark-counters"))]
impl Counter {
const fn new() -> Self { Self }
#[inline]
fn add(&self, bytes: usize) { let _ = bytes; }
fn load(&self) -> u64 { 0 }
fn clear(&self) {}
}
static SERIALIZED_BYTES: Counter = Counter::new();
static HANDOFF_BYTES: Counter = Counter::new();
static FRAMED_BYTES: Counter = Counter::new();
static DESERIALIZED_BYTES: Counter = Counter::new();
static HANDOFF_SEAM_CALLS: Counter = Counter::new();
#[inline]
pub fn record_serialized(bytes: usize) { SERIALIZED_BYTES.add(bytes); }
#[inline]
pub fn record_handoff(bytes: usize) { HANDOFF_BYTES.add(bytes); }
#[inline]
pub fn record_framed(bytes: usize) { FRAMED_BYTES.add(bytes); }
#[inline]
pub fn record_deserialized(bytes: usize) { DESERIALIZED_BYTES.add(bytes); }
#[must_use]
pub fn snapshot() -> CopiedBytes {
CopiedBytes {
serialized: SERIALIZED_BYTES.load(),
handoff: HANDOFF_BYTES.load(),
framed: FRAMED_BYTES.load(),
deserialized: DESERIALIZED_BYTES.load(),
}
}
pub fn reset() {
SERIALIZED_BYTES.clear();
HANDOFF_BYTES.clear();
FRAMED_BYTES.clear();
DESERIALIZED_BYTES.clear();
HANDOFF_SEAM_CALLS.clear();
}
#[doc(hidden)]
#[must_use]
pub fn handoff_seam_calls() -> u64 { HANDOFF_SEAM_CALLS.load() }
#[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 {
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() {
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() {
let before = snapshot().handoff;
record_handoff(7);
record_handoff(5);
assert_eq!(
snapshot().handoff - before,
12,
"snapshot().handoff - before should equal 12"
);
}
}