Collect statistics and generate log messages. More...
#include <senf/Utils/Statistics.hh>
Public Member Functions | |
Statistics () | |
void | operator() (unsigned n, unsigned cnt, float min, float avg, float max, float dev) |
Enter new data. More... | |
void | operator() (unsigned cnt, float min, float avg, float max, float dev=0.0f) |
Same as operator() with n==1. More... | |
void | operator() (unsigned cnt, float value, float dev=0.0f) |
Same as operator() with min == avg == max. More... | |
void | operator() (StatisticsData const &data) |
Same as operator(), but imports statistics data from a StatisticsData object. More... | |
template<class Value > | |
void | operator() (unsigned n, StatisticAccumulator< Value > &sa) |
Same as operator() gathers values from StatisticAccumulator. More... | |
StatisticsBase::OutputProxy< Statistics > | output (unsigned n=1u) |
Public Member Functions inherited from senf::StatisticsBase | |
StatisticsData | data () const |
Get the Statistics data as senf::StatisticsData. More... | |
void | consoleList (unsigned level, std::ostream &os) const |
unsigned | cnt () const |
Last cnt value entered. More... | |
float | min () const |
Last min value entered. More... | |
float | avg () const |
Last avg value entered. More... | |
float | max () const |
Last max value entered. More... | |
float | dev () const |
Last dev value entered. More... | |
virtual unsigned | rank () const |
Return collectors rank value. More... | |
Collector & | operator[] (unsigned rank) |
Get child collector. More... | |
Collector const & | operator[] (unsigned rank) const |
Get child collector. More... | |
CollectorRange | collectors () |
List all child collectors. More... | |
const_CollectorRange | collectors () const |
List all child collectors. More... | |
Collector & | collect (unsigned rank) |
Register a new collector. More... | |
Statistics const & | base () const |
Statistics & | base () |
Get base statistics object. More... | |
std::string | path () const |
Get the path to this collector. More... | |
OutputProxy< StatisticsBase > | output (unsigned n=1u) |
Register output. More... | |
Public Attributes | |
console::ScopedDirectory | dir |
Additional Inherited Members | |
Public Types inherited from senf::StatisticsBase | |
typedef boost::iterator_range< ValueIterator > | CollectorRange |
typedef boost::iterator_range< const_ValueIterator > | const_CollectorRange |
Protected Member Functions inherited from senf::StatisticsBase | |
StatisticsBase () | |
virtual | ~StatisticsBase () |
void | enter (unsigned n, unsigned cnt, float min, float avg, float max, float dev) |
Collect statistics and generate log messages.
The Statistics class collects information about a single value. The assumption is, that Statistics::operator() is called periodically to supply new data.
Each data entry is comprised of a minimum, average and maximum value. These values should describe the value of whatever parameter is analyzed by a Statistics instance over the last period. The length of the period is defined by the interval, at which data is entered.
The Statistics class combines successive data values into ever larger groups. These groups form a tree where each node combines a fixed number of data values of it's parent node. An example:
Let us assume, that Statistics::operator() is called every 100ms. We can than build a chain of collectors:
This way, we create a hierarchy of values. Each collector manages a minimum, average and maximum value always created over it's the last complete interval.
It is possible to have more than one collector based on the same basic interval. In above scenario, we might want to add another collector which for example collects information with a 100 second scale. This is possible and changes the list of collectors into a tree.
Now to turn all this into code:
This code will collect the statistics as described in the Example above. However, no output will be generated. For every collector, any number of outputs may be defined. Each output consists of the number of values to calculate a sliding average over and an identifying label. Lets say, we want to produce the following outputs: \li A sliding average of 5 values based on the raw 1/10th second data. \li Three different outputs from the seconds statistics: current value without average, sliding average over 10 seconds and sliding average over 60 seconds. \li Output the minutes and hourly value without averaging. To achieve this, we can augment above code in the following way:
We use a StatisticsLogger to send the %log messages to the senf::StatisticsStream %log stream. The stream, area an level to send the statistics %log messages to may be configured using template arguments to StatisticsLogger. It is also possible to skip sending the output to any target or send one output to several targets. Here we have opted to use a label which explicitly describes the name of the variable, the basic interval and the size of the sliding average window. However, the label is completely arbitrary. All output is generated using the Senf Logger on the senf::StatisticsStream %log stream. In the example above, we have configured a special logfile \c stats.log which contains the statistics values each prefixed with a timestamp in nanoseconds (but no further information like %log level or tag):
0000000000.000000000 pps 100ms 5 43.3 43.3 43.3 0000000000.010311928 pps 100ms 5 42.5 42.5 42.5 ... 0000000001.002413391 pps 100ms 5 62.0 62.0 62.0 0000000001.003920018 pps 1s 1 42.1 53.4 62.0 ...
(the nanosecond values will of course be somewhat different ...)
Definition at line 435 of file Statistics.hh.
senf::Statistics::Statistics | ( | ) |
Definition at line 184 of file Statistics.cc.
void senf::Statistics::operator() | ( | unsigned | n, |
unsigned | cnt, | ||
float | min, | ||
float | avg, | ||
float | max, | ||
float | dev | ||
) |
Enter new data.
This member must be called whenever new data is available.
If min and max values are not available, this member should be called with min, avg and max set to the same value. If no error estimate is available, call with dev = 0.
In the most common case, this member is to be called periodically and n will be 1 on all calls. The interval, at which this member is called then defines the statistics time scale.
Calling with n > 1 will submit the value n times. This makes it possible to aggregate multiple time slices into a single call. This does not change the basic time scale but can change the number of submits per unit time. If the basic time slice is small, this allows to submit values almost arbitrarily non-periodic.
[in] | n | number of time-slices |
[in] | min | minimal data value since last call |
[in] | avg | average data value since last call |
[in] | max | maximal data values since last call |
[in] | dev | standard deviation of avg value |
void senf::Statistics::operator() | ( | unsigned | cnt, |
float | min, | ||
float | avg, | ||
float | max, | ||
float | dev = 0.0f |
||
) |
Same as operator() with n==1.
Provided so a Statistics instance can be directly used as a signal target.
void senf::Statistics::operator() | ( | unsigned | cnt, |
float | value, | ||
float | dev = 0.0f |
||
) |
Same as operator() with min == avg == max.
Provided so a Statistics instance can be directly used as a signal target.
void senf::Statistics::operator() | ( | StatisticsData const & | data | ) |
Same as operator(), but imports statistics data from a StatisticsData object.
Provided so a Statistics instance can be directly used as a signal target.
void senf::Statistics::operator() | ( | unsigned | n, |
StatisticAccumulator< Value > & | sa | ||
) |
Same as operator() gathers values from StatisticAccumulator.
Provided so a Statistics instance can be directly used as a signal target. Caution: Clears values in StatisticAccumulator afterwards
[in] | n | number of time-slices |
[in] | sa | StatisticAccumulator |
StatisticsBase::OutputProxy<Statistics> senf::Statistics::output | ( | unsigned | n = 1u | ) |
console::ScopedDirectory senf::Statistics::dir |
Definition at line 440 of file Statistics.hh.