pub struct LatencyTracker { /* private fields */ }Expand description
Captures per-operation latency samples for a single thread.
The tracker is intentionally thread-local: each thread keeps a
Vec<Duration> and the runner concatenates them at finish. There
is no shared state, so sample collection introduces no
synchronization that would distort the measurement.
Implementations§
Source§impl LatencyTracker
impl LatencyTracker
Sourcepub fn new(rate: usize) -> Self
pub fn new(rate: usize) -> Self
Create a tracker that samples 1 of every rate iterations.
rate = 1 records every iteration. rate = 100 records 1%.
Pass at least 1; values below are clamped.
Sourcepub fn record<F, R>(&mut self, iter_index: usize, f: F) -> Rwhere
F: FnOnce() -> R,
pub fn record<F, R>(&mut self, iter_index: usize, f: F) -> Rwhere
F: FnOnce() -> R,
Run the closure and, if iter_index is on the sampling
schedule, record its duration.
iter_index is the 0-based iteration counter on the calling
thread. The tracker records the sample when
iter_index % sample_rate == 0.
§Example
use dev_stress::LatencyTracker;
let mut t = LatencyTracker::new(1);
t.record(0, || std::hint::black_box(1 + 1));
t.record(1, || std::hint::black_box(1 + 1));
assert_eq!(t.samples_count(), 2);Sourcepub fn samples_count(&self) -> usize
pub fn samples_count(&self) -> usize
Number of samples currently held by this tracker.
Sourcepub fn into_samples(self) -> Vec<Duration>
pub fn into_samples(self) -> Vec<Duration>
Move all samples out of this tracker.