Struct apollo_framework::average::Average[][src]

pub struct Average { /* fields omitted */ }

Computes a sliding average of a series of values.

This is intended to record performance measurements and to keep track of the sliding average as well as the total number of recorded values.

Note that this class overflows gracefully.

Example

let avg = Average::new();
avg.add(10);
avg.add(20);
avg.add(30);

assert_eq!(avg.avg(), 20);
assert_eq!(avg.count(), 3);

Implementations

impl Average[src]

pub fn new() -> Average[src]

Creates a new average.

pub fn add(&self, value: i32)[src]

Adds another value to be added to the average calculation.

Internally we simply update the global u64 counter to keep track of the total recorded values. Additionally, we have another u64 which is split into two i32 fields. One of these is used to keep the actual count of the sliding average and another is used to store the sum of the values.

Whenever we recorded 100 values or the sum counter might overflow, we divide both values by two and add the new values. This yields a sliding average which is fit for our purposes.

As the main task is to store the average duration of a task in microseconds, the i32 sum field shouldn’t overflow under normal conditions.

We perform this trickery (splitting a single field into two) so that this algorithm is completely lock and wait free, as we only utilize atomic load and store operations. This guarantees correctness while ensuring maximal performance.

pub fn count(&self) -> u64[src]

Returns the total number of recorded values (unless an overflow of the internal u64 counter occurred).

pub fn avg(&self) -> i32[src]

Computes the sliding average of the last 100 values.

Trait Implementations

impl Clone for Average[src]

impl Default for Average[src]

impl Display for Average[src]

Auto Trait Implementations

impl RefUnwindSafe for Average

impl Send for Average

impl Sync for Average

impl Unpin for Average

impl UnwindSafe for Average

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.