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
impl Average
sourcepub fn add(&self, value: i32)
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.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Average
impl RefUnwindSafe for Average
impl Send for Average
impl Sync for Average
impl Unpin for Average
impl UnwindSafe for Average
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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