|
CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
|
Sliding-window sample collector with average, variance, standard deviation and median. More...
#include <Stats.h>
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 |
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.
| cmlabs::Stats::Stats | ( | uint32 | maxCount = 10000 | ) |
Create a collector.
| maxCount | Maximum samples retained; adding beyond this evicts the oldest. |
Definition at line 12 of file Stats.cpp.
References maxCount.
Referenced by UnitTest().
| bool cmlabs::Stats::add | ( | double | val | ) |
Add a sample, evicting the oldest when the window is full.
| val | Sample value. |
Definition at line 29 of file Stats.cpp.
References entries, maxCount, and mutex.
Referenced by cmlabs::TestRequestClient::run(), and UnitTest().
| bool cmlabs::Stats::clear | ( | ) |
Remove all samples.
Definition at line 22 of file Stats.cpp.
References entries, and mutex.
Referenced by _wrap_Stats_clear().
| double cmlabs::Stats::getAverage | ( | ) |
Definition at line 48 of file Stats.cpp.
References entries, getSum(), and mutex.
Referenced by _wrap_Stats_getAverage(), cmlabs::TestRequestClient::run(), and UnitTest().
| uint32 cmlabs::Stats::getCount | ( | ) |
Definition at line 107 of file Stats.cpp.
References entries, and mutex.
Referenced by _wrap_Stats_getCount(), and UnitTest().
| double cmlabs::Stats::getMedian | ( | ) |
Definition at line 73 of file Stats.cpp.
References entries, and mutex.
Referenced by _wrap_Stats_getMedian(), and UnitTest().
| double cmlabs::Stats::getStdDev | ( | ) |
Definition at line 103 of file Stats.cpp.
References getVariance().
Referenced by _wrap_Stats_getStdDev(), and UnitTest().
| double cmlabs::Stats::getSum | ( | ) |
Definition at line 38 of file Stats.cpp.
References entries, and mutex.
Referenced by _wrap_Stats_getSum(), getAverage(), getVariance(), and UnitTest().
| double cmlabs::Stats::getVariance | ( | ) |
Definition at line 56 of file Stats.cpp.
References entries, getSum(), and mutex.
Referenced by _wrap_Stats_getVariance(), getStdDev(), and UnitTest().
|
static |
Self test.
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().
|
protected |
Definition at line 67 of file Stats.h.
Referenced by add(), clear(), getAverage(), getCount(), getMedian(), getSum(), getVariance(), and ~Stats().
|
protected |
|
protected |
Definition at line 66 of file Stats.h.
Referenced by add(), clear(), getAverage(), getCount(), getMedian(), getSum(), getVariance(), and ~Stats().