Struct jupiter::average::Average

source ·
pub struct Average { /* private fields */ }
Expand description

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§

source§

impl Average

source

pub fn new() -> Average

Creates a new average.

source

pub fn add(&self, value: i32)

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.

source

pub fn count(&self) -> u64

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

source

pub fn avg(&self) -> i32

Computes the sliding average of the last 100 values.

Trait Implementations§

source§

impl Clone for Average

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Default for Average

source§

fn default() -> Average

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

impl Display for Average

source§

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

Formats the value using the given formatter. Read more

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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

source§

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

§

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

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more