#[repr(align(64))]pub struct Timer { /* private fields */ }
Expand description
High-precision timer with automatic statistics
Optimized for sub-10ns timing operations. Cache-line aligned to prevent false sharing.
Implementations§
Source§impl Timer
impl Timer
Sourcepub fn try_record_ns(&self, duration_ns: u64) -> Result<()>
pub fn try_record_ns(&self, duration_ns: u64) -> Result<()>
Try to record nanoseconds directly with overflow checks
Returns Err(MetricsError::Overflow)
if adding duration_ns
would
overflow internal counters (total_nanos
or count
). On success
updates min/max as needed.
Example
use metrics_lib::{Timer, MetricsError};
let t = Timer::new();
t.try_record_ns(1_000).unwrap();
assert_eq!(t.count(), 1);
assert!(matches!(t.try_record_ns(u64::MAX), Err(MetricsError::Overflow)));
Sourcepub fn start(&self) -> RunningTimer<'_>
pub fn start(&self) -> RunningTimer<'_>
Start timing - returns RAII guard that auto-records on drop
Sourcepub fn try_record(&self, duration: Duration) -> Result<()>
pub fn try_record(&self, duration: Duration) -> Result<()>
Try to record a duration with overflow checks
Returns Err(MetricsError::Overflow)
if adding this sample would overflow
internal counters.
Example
use metrics_lib::{Timer, MetricsError};
use std::time::Duration;
let t = Timer::new();
assert!(t.try_record(Duration::from_millis(1)).is_ok());
Sourcepub fn record_batch(&self, durations: &[Duration])
pub fn record_batch(&self, durations: &[Duration])
Record multiple durations at once
Sourcepub fn try_record_batch(&self, durations: &[Duration]) -> Result<()>
pub fn try_record_batch(&self, durations: &[Duration]) -> Result<()>
Try to record multiple durations at once with overflow checks
Returns Err(MetricsError::Overflow)
if the aggregated additions would
overflow internal counters. Updates min/max based on batch extrema.
Example
use metrics_lib::Timer;
use std::time::Duration;
let t = Timer::new();
t.try_record_batch(&[Duration::from_micros(5), Duration::from_micros(10)]).unwrap();
assert_eq!(t.count(), 2);
Sourcepub fn count(&self) -> u64
pub fn count(&self) -> u64
Get current count of samples
Note: #[must_use]
. The count informs control flow and sanity checks;
ignoring it may indicate a logic bug.
Sourcepub fn total(&self) -> Duration
pub fn total(&self) -> Duration
Get total accumulated time
Note: #[must_use]
. The total duration is commonly used for reporting
and ratios; dropping it may indicate a bug.
Sourcepub fn average(&self) -> Duration
pub fn average(&self) -> Duration
Get average duration
Note: #[must_use]
. The average is a derived metric; call this only
when you consume the result.
Sourcepub fn min(&self) -> Duration
pub fn min(&self) -> Duration
Get minimum duration
Note: #[must_use]
. Often used for alerting/thresholds.
Sourcepub fn max(&self) -> Duration
pub fn max(&self) -> Duration
Get maximum duration
Note: #[must_use]
. Often used for alerting/thresholds.
Sourcepub fn stats(&self) -> TimerStats
pub fn stats(&self) -> TimerStats
Get comprehensive statistics
Note: #[must_use]
. Statistics summarize current state; dropping the
result may indicate a logic bug.
Sourcepub fn age(&self) -> Duration
pub fn age(&self) -> Duration
Get age since creation
Note: #[must_use]
. Age is used in rate calculations; don’t call for
side effects.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if timer has recorded any samples
Note: #[must_use]
. The boolean result determines control flow.
Sourcepub fn rate_per_second(&self) -> f64
pub fn rate_per_second(&self) -> f64
Get samples per second rate
Note: #[must_use]
. Derived metric; consume the result.