Timer

Struct Timer 

Source
#[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

Source

pub fn new() -> Self

Create new timer

Source

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)));
Source

pub fn start(&self) -> RunningTimer<'_>

Start timing - returns RAII guard that auto-records on drop

Source

pub fn record(&self, duration: Duration)

Record a duration manually

Source

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());
Source

pub fn record_ns(&self, duration_ns: u64)

Record nanoseconds directly - fastest path

Source

pub fn record_batch(&self, durations: &[Duration])

Record multiple durations at once

Source

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);
Source

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.

Source

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.

Source

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.

Source

pub fn min(&self) -> Duration

Get minimum duration

Note: #[must_use]. Often used for alerting/thresholds.

Source

pub fn max(&self) -> Duration

Get maximum duration

Note: #[must_use]. Often used for alerting/thresholds.

Source

pub fn reset(&self)

Reset all statistics

Source

pub fn stats(&self) -> TimerStats

Get comprehensive statistics

Note: #[must_use]. Statistics summarize current state; dropping the result may indicate a logic bug.

Source

pub fn age(&self) -> Duration

Get age since creation

Note: #[must_use]. Age is used in rate calculations; don’t call for side effects.

Source

pub fn is_empty(&self) -> bool

Check if timer has recorded any samples

Note: #[must_use]. The boolean result determines control flow.

Source

pub fn rate_per_second(&self) -> f64

Get samples per second rate

Note: #[must_use]. Derived metric; consume the result.

Trait Implementations§

Source§

impl AsyncTimerExt for Timer

Source§

fn start_async(&self) -> AsyncTimerGuard<'_>

Start an async-aware timer
Source§

fn time_async<F, Fut, T>(&self, f: F) -> TimedFuture<'_, Fut>
where F: FnOnce() -> Fut, Fut: Future<Output = T>,

Time an async function
Source§

impl Debug for Timer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Timer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Timer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Send for Timer

Source§

impl Sync for Timer

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.