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

pub struct Average { /* fields omitted */ }
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

Creates a new average.

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.

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

Computes the sliding average of the last 100 values.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

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

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.