#[cfg(feature = "runtime-stats")]
use std::sync::Arc;
#[cfg(feature = "runtime-stats")]
use std::sync::Mutex;
#[cfg(feature = "runtime-stats")]
use base2histogram::Histogram;
#[derive(Debug, Clone)]
pub struct SharedReplicateBatch {
#[cfg(feature = "runtime-stats")]
inner: Arc<Mutex<Histogram>>,
}
impl Default for SharedReplicateBatch {
fn default() -> Self {
Self::new()
}
}
impl SharedReplicateBatch {
pub(crate) fn new() -> Self {
Self {
#[cfg(feature = "runtime-stats")]
inner: Arc::new(Mutex::new(Histogram::<()>::new())),
}
}
pub fn record(&self, value: u64) {
#[cfg(feature = "runtime-stats")]
{
let mut guard = self.inner.lock().unwrap();
guard.record(value);
}
#[cfg(not(feature = "runtime-stats"))]
let _ = value;
}
#[cfg(feature = "runtime-stats")]
pub(crate) fn snapshot(&self) -> Histogram {
let guard = self.inner.lock().unwrap();
guard.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(not(feature = "runtime-stats"))]
#[test]
fn test_disabled_handle_is_zero_sized() {
assert_eq!(size_of::<SharedReplicateBatch>(), 0);
}
#[cfg(feature = "runtime-stats")]
#[test]
fn test_new_and_default() {
let s1 = SharedReplicateBatch::new();
let s2 = SharedReplicateBatch::default();
s1.record(1);
s2.record(1);
}
#[cfg(feature = "runtime-stats")]
#[test]
fn test_clone_shares_state() {
let s1 = SharedReplicateBatch::new();
let s2 = s1.clone();
s1.record(100);
s2.record(200);
let snapshot = s2.snapshot();
assert_eq!(snapshot.total(), 2);
}
#[cfg(feature = "runtime-stats")]
#[test]
fn test_record_and_snapshot() {
let state = SharedReplicateBatch::new();
state.record(10);
state.record(20);
let snapshot = state.snapshot();
assert_eq!(snapshot.total(), 2);
}
}