use crate::core::constants::{HISTOGRAM_HIGH_US, HISTOGRAM_LOW_US, HISTOGRAM_SIGFIGS};
use hdrhistogram::Histogram;
use parking_lot::Mutex;
#[derive(Debug)]
pub struct LatencyHistogram {
inner: Mutex<Histogram<u64>>,
}
impl LatencyHistogram {
pub fn new() -> Self {
let inner = Histogram::<u64>::new_with_bounds(
HISTOGRAM_LOW_US,
HISTOGRAM_HIGH_US,
HISTOGRAM_SIGFIGS,
)
.expect("valid HDR histogram parameters");
Self {
inner: Mutex::new(inner),
}
}
pub fn record(&self, value_us: u64) {
let mut hist = self.inner.lock();
hist.saturating_record(value_us);
}
pub fn value_at_quantile(&self, quantile: f64) -> u64 {
let hist = self.inner.lock();
hist.value_at_quantile(quantile)
}
pub fn mean(&self) -> f64 {
let hist = self.inner.lock();
hist.mean()
}
pub fn max(&self) -> u64 {
let hist = self.inner.lock();
hist.max()
}
pub fn min(&self) -> u64 {
let hist = self.inner.lock();
hist.min()
}
pub fn len(&self) -> u64 {
let hist = self.inner.lock();
hist.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn reset(&self) {
self.inner.lock().reset();
}
}
impl Default for LatencyHistogram {
fn default() -> Self {
Self::new()
}
}