Struct FrameStats

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

Source

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.

Source

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

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

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

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

pub fn mean_ms(&self) -> f32

Arithmetic mean in milliseconds, or zero when empty.

Accumulates in f64 so summing large accepted samples cannot overflow.

Source

pub fn max_ms(&self) -> f32

Largest retained frame time in milliseconds, or zero when empty.

Source

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

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

Trait Implementations§

Source§

impl<const N: usize> Default for FrameStats<N>

Source§

fn default() -> FrameStats<N>

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

Auto Trait Implementations§

§

impl<const N: usize> Freeze for FrameStats<N>

§

impl<const N: usize> RefUnwindSafe for FrameStats<N>

§

impl<const N: usize> Send for FrameStats<N>

§

impl<const N: usize> Sync for FrameStats<N>

§

impl<const N: usize> Unpin for FrameStats<N>

§

impl<const N: usize> UnwindSafe for FrameStats<N>

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, 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.