I am trying to use accumulators to find out statistics for a given activity per sec. Below are the two stats I would like to compute
Number of times activity has been triggered
Sum of the total weight triggered by the activity.
To achieve I have assumed a granularity by 10 msecs and considering 100 buckets ( per sec ).
Actvity Thread inserts into accumulator whenever there is an event
Null Activity Thread wakes up every 10 msecs to insert 0 in the weight.
Psuedo code below
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_count.hpp>
#include <boost/accumulators/statistics/rolling_sum.hpp>
using namespace boost::accumulators;
#define MAX_WEIGHT 100
#define MAX_ACIVITY 10
accumulator_set<double, features<tag::rolling_count, tag::rolling_sum> > acc(tag::rolling_window::window_size = 100);
void null_run()//invoked every 10 msecs
{
//mutex protected
acc(0);
}
void activity_triggered(int weight) // triggered by an external event
{
//mutex protected
acc(weight);
if (checkStatus() == false)
{
printf("Max quantity per sec reached stop ");
exit()
}
}
bool checkStatus()
{
int weightPerSec = rolling_sum(acc);
//here I would like to get the count only if non zero may be rolling_count_non_zero()??
int acitivitiesPersec = rolling_count(acc);
if (weightPerSec > MAX_WEIGHT)
return false;
if (acitivitiesPersec > MAX_ACTIVITY)
return false;
return true;
}
Using above technique i am able to achieve weight entered in the last second but how do I achieve how many times has the activity been triggered in the last sec using boost accumulators ?
Sure rolling_count_non_zero
sounds like an appropriate name for what you want (although it could have been somewhat more generic, maybe rolling_count_if
?).
Boost.accumulators users guide describes how to write your own accumulators, features, and extractors, simply following that description, the following seems to work:
namespace boost { namespace accumulators { namespace impl {
template<typename Sample>
struct rolling_count_non_zero : accumulator_base
{
typedef std::size_t result_type;
rolling_count_non_zero(dont_care) : cnt_() {}
template<typename Args>
void operator ()(Args const &args)
{
if(args[sample] != 0)
++cnt_;
if(is_rolling_window_plus1_full(args)
&& rolling_window_plus1(args).front() != 0 )
--cnt_;
}
template<typename Args>
result_type result(Args const &args) const { return cnt_; }
private:
std::size_t cnt_;
};
} namespace tag {
struct rolling_count_non_zero : depends_on< rolling_window_plus1 >
{
typedef accumulators::impl::rolling_count_non_zero< mpl::_1 > impl;
};
} namespace extract {
extractor<tag::rolling_count_non_zero> const rolling_count_non_zero = {};
}
using extract::rolling_count_non_zero;
}}
live demo: http://coliru.stacked-crooked.com/a/bc4bea090690f26d