pub struct FrameStats<const N: usize = 120> { /* private fields */ }Expand description
Allocation-free statistics over the most recent N accepted frame times.
Record elapsed seconds; diagnostics return milliseconds. During warm-up,
only recorded samples contribute. Zero durations are valid, and N == 0
creates a disabled, always-empty window. Invalid samples never evict history.
Recording takes constant time; queries scan at most N samples. Percentile
queries also copy the fixed array onto the stack and sort the populated part.
use ferravex_core::FrameStats;
let mut stats = FrameStats::<120>::default();
stats.record(0.010);
stats.record(0.030);
assert_eq!(stats.sample_count(), 2);
assert_eq!(stats.capacity(), 120);
assert_eq!(stats.mean_ms(), 20.0);
assert_eq!(stats.max_ms(), 30.0);
assert_eq!(stats.percentile_ms(95.0), Some(30.0));
assert_eq!(stats.jitter_ms(), 10.0);Implementations§
Source§impl<const N: usize> FrameStats<N>
impl<const N: usize> FrameStats<N>
Sourcepub fn record(&mut self, seconds: f32)
pub fn record(&mut self, seconds: f32)
Record elapsed seconds, silently ignoring invalid or disabled samples.
See Self::try_record to learn whether a sample was accepted. Neither
method allocates, including during warm-up and when the window wraps.
Sourcepub fn try_record(&mut self, seconds: f32) -> bool
pub fn try_record(&mut self, seconds: f32) -> bool
Record elapsed seconds, returning whether the sample was accepted.
Returns false without changing the window if N == 0, seconds is
negative or non-finite, or converting to f32 milliseconds overflows.
Zero (including negative zero) is accepted. When full, the oldest accepted
sample is replaced. This operation takes constant time and never allocates.
use ferravex_core::FrameStats;
let mut stats = FrameStats::<2>::default();
assert!(stats.try_record(0.016));
assert!(!stats.try_record(f32::NAN));
assert!(!stats.try_record(f32::MAX)); // Milliseconds would overflow.
assert_eq!(stats.sample_count(), 1);
assert!(!FrameStats::<0>::default().try_record(0.016));Sourcepub const fn sample_count(&self) -> usize
pub const fn sample_count(&self) -> usize
Number of accepted samples currently retained, at most Self::capacity.
This is a window count, not a lifetime counter. Compare it with capacity to determine whether a nonzero window has completed warm-up.
use ferravex_core::FrameStats;
let mut stats = FrameStats::<2>::default();
assert_eq!(stats.sample_count(), 0);
for _ in 0..3 { stats.record(0.016); }
assert_eq!(stats.sample_count(), 2);Sourcepub const fn capacity(&self) -> usize
pub const fn capacity(&self) -> usize
Maximum retained sample count; zero means recording is disabled.
use ferravex_core::FrameStats;
assert_eq!(FrameStats::<90>::default().capacity(), 90);
assert_eq!(FrameStats::<0>::default().capacity(), 0);Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Forget all samples and restart warm-up without allocating.
use ferravex_core::FrameStats;
let mut stats = FrameStats::<120>::default();
stats.record(0.016);
stats.clear();
assert_eq!(stats.sample_count(), 0);
assert_eq!(stats.mean_ms(), 0.0);
assert_eq!(stats.percentile_ms(95.0), None);Sourcepub fn mean_ms(&self) -> f32
pub fn mean_ms(&self) -> f32
Arithmetic mean in milliseconds, or zero when empty.
Accumulates in f64 so summing large accepted samples cannot overflow.
Sourcepub fn percentile_ms(&self, percentile: f32) -> Option<f32>
pub fn percentile_ms(&self, percentile: f32) -> Option<f32>
Nearest-rank percentile in milliseconds, using a percentage in 0..=100.
Returns None for an empty window or a non-finite/out-of-range percentage.
For sorted samples, selects rank ceil(percentile * sample_count / 100)
(one-based), with percentile zero selecting the minimum. Percentile 100
selects the maximum. Small windows can therefore share several percentiles;
values are never interpolated and unused warm-up slots do not contribute.
Does not change recording order or allocate on the heap. A query uses
N * size_of::<f32>() bytes of stack scratch space and sorts the m
retained samples in O(m log m) time. Choose N to fit your stack budget.
use ferravex_core::FrameStats;
let mut stats = FrameStats::<120>::default();
assert_eq!(stats.percentile_ms(99.0), None);
for seconds in [0.040, 0.010, 0.030, 0.020] { stats.record(seconds); }
assert_eq!(stats.percentile_ms(0.0), Some(10.0));
assert_eq!(stats.percentile_ms(50.0), Some(20.0));
assert_eq!(stats.percentile_ms(95.0), Some(40.0));
assert_eq!(stats.percentile_ms(101.0), None);Sourcepub fn jitter_ms(&self) -> f32
pub fn jitter_ms(&self) -> f32
Population standard deviation of retained frame times, in milliseconds.
Jitter is sqrt(sum((sample - mean)^2) / sample_count), measuring spread
around the mean, independent of sample order. It is not a difference between
consecutive frames. Empty, single-sample and constant windows return zero.
Uses two passes with f64 arithmetic to avoid overflow and cancellation
from subtracting squared means. Never allocates.
use ferravex_core::FrameStats;
let mut stats = FrameStats::<120>::default();
assert_eq!(stats.jitter_ms(), 0.0);
stats.record(0.010);
assert_eq!(stats.jitter_ms(), 0.0);
stats.record(0.030);
assert_eq!(stats.jitter_ms(), 10.0);