CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
cmlabs::Stats Class Reference

Sliding-window sample collector with average, variance, standard deviation and median. More...

#include <Stats.h>

Collaboration diagram for cmlabs::Stats:
[legend]

Public Member Functions

 Stats (uint32 maxCount=10000)
 Create a collector.
 ~Stats ()
bool clear ()
 Remove all samples.
bool add (double val)
 Add a sample, evicting the oldest when the window is full.
double getAverage ()
double getSum ()
uint32 getCount ()
double getVariance ()
double getStdDev ()
double getMedian ()

Static Public Member Functions

static bool UnitTest ()
 Self test.

Protected Attributes

uint32 maxCount
utils::Mutex mutex
std::list< double > entries

Detailed Description

Sliding-window sample collector with average, variance, standard deviation and median.

Unlike MovingAverage (which only keeps per-bin sums), Stats retains the individual sample values in a std::list, capped at maxCount entries; add() evicts the oldest entry once the cap is reached. This makes order-dependent statistics (median, variance) possible at the cost of O(n) memory and O(n) or O(n log n) query time, so it is intended for moderate window sizes (the default cap is 10000), not unbounded streams.

All public methods take the internal mutex (recursive — aggregate getters may call each other under the lock) and are safe to call from multiple threads. Each getter walks the sample list while holding the lock, so very large windows queried at high frequency will contend.

Stats latency(1000); // keep the 1000 most recent samples
latency.add(12.5); // one measurement in ms
latency.add(9.1);
double mean = latency.getAverage();
double sd = latency.getStdDev();
double med = latency.getMedian();
Stats(uint32 maxCount=10000)
Create a collector.
Definition Stats.cpp:12
Warning
getMedian() sorts the underlying sample list in place. Afterwards the list is ordered by value rather than by insertion time, so subsequent evictions at the cap remove the smallest value instead of the oldest sample. Avoid interleaving getMedian() with continued add() calls when strict oldest-first window semantics matter.

Definition at line 43 of file Stats.h.

Constructor & Destructor Documentation

◆ Stats()

cmlabs::Stats::Stats ( uint32 maxCount = 10000)

Create a collector.

Parameters
maxCountMaximum samples retained; adding beyond this evicts the oldest.

Definition at line 12 of file Stats.cpp.

References maxCount.

Referenced by UnitTest().

◆ ~Stats()

cmlabs::Stats::~Stats ( )

Definition at line 16 of file Stats.cpp.

References entries, and mutex.

Member Function Documentation

◆ add()

bool cmlabs::Stats::add ( double val)

Add a sample, evicting the oldest when the window is full.

Parameters
valSample value.
Returns
true on success.

Definition at line 29 of file Stats.cpp.

References entries, maxCount, and mutex.

Referenced by cmlabs::TestRequestClient::run(), and UnitTest().

◆ clear()

bool cmlabs::Stats::clear ( )

Remove all samples.

Returns
true on success.

Definition at line 22 of file Stats.cpp.

References entries, and mutex.

Referenced by _wrap_Stats_clear().

◆ getAverage()

double cmlabs::Stats::getAverage ( )
Returns
Mean of the current window.
Warning
Divides by the sample count without an empty check; returns NaN/inf when no samples are present — check getCount() first.

Definition at line 48 of file Stats.cpp.

References entries, getSum(), and mutex.

Referenced by _wrap_Stats_getAverage(), cmlabs::TestRequestClient::run(), and UnitTest().

◆ getCount()

uint32 cmlabs::Stats::getCount ( )
Returns
Number of samples currently held.

Definition at line 107 of file Stats.cpp.

References entries, and mutex.

Referenced by _wrap_Stats_getCount(), and UnitTest().

◆ getMedian()

double cmlabs::Stats::getMedian ( )
Returns
Median of the current samples (0 when empty; mean of the two middle values for an even count).
Note
O(n log n): sorts the internal list in place, destroying insertion order — see the class-level warning.

Definition at line 73 of file Stats.cpp.

References entries, and mutex.

Referenced by _wrap_Stats_getMedian(), and UnitTest().

◆ getStdDev()

double cmlabs::Stats::getStdDev ( )
Returns
Population standard deviation (square root of getVariance()).

Definition at line 103 of file Stats.cpp.

References getVariance().

Referenced by _wrap_Stats_getStdDev(), and UnitTest().

◆ getSum()

double cmlabs::Stats::getSum ( )
Returns
Sum of the current window.

Definition at line 38 of file Stats.cpp.

References entries, and mutex.

Referenced by _wrap_Stats_getSum(), getAverage(), getVariance(), and UnitTest().

◆ getVariance()

double cmlabs::Stats::getVariance ( )
Returns
Population variance (sum of squared deviations divided by n, not n-1); 0 when fewer than 2 samples.

Definition at line 56 of file Stats.cpp.

References entries, getSum(), and mutex.

Referenced by _wrap_Stats_getVariance(), getStdDev(), and UnitTest().

◆ UnitTest()

bool cmlabs::Stats::UnitTest ( )
static

Self test.

Returns
true when all checks pass.

Definition at line 116 of file Stats.cpp.

References add(), cmlabs::unittest::fail(), getAverage(), getCount(), getMedian(), getStdDev(), getSum(), getVariance(), cmlabs::unittest::progress(), and Stats().

Referenced by _wrap_Stats_UnitTest(), and cmlabs::Register_Stats_Tests().

Member Data Documentation

◆ entries

std::list<double> cmlabs::Stats::entries
protected

Definition at line 67 of file Stats.h.

Referenced by add(), clear(), getAverage(), getCount(), getMedian(), getSum(), getVariance(), and ~Stats().

◆ maxCount

uint32 cmlabs::Stats::maxCount
protected

Definition at line 65 of file Stats.h.

Referenced by add(), and Stats().

◆ mutex

utils::Mutex cmlabs::Stats::mutex
protected

Definition at line 66 of file Stats.h.

Referenced by add(), clear(), getAverage(), getCount(), getMedian(), getSum(), getVariance(), and ~Stats().


The documentation for this class was generated from the following files:
  • /home/ubuntu/c/partner/psyclone2/CMSDK/include/Stats.h
  • /home/ubuntu/c/partner/psyclone2/CMSDK/src/Stats.cpp