我正在尝试使用累加器来找出每秒给定活动的统计信息。以下是我要计算的两个统计数据
活动被触发的次数
活动触发的总权重之和。
为了实现这一目标,我假设了 10 毫秒的粒度并考虑了 100 个桶(每秒)。
每当有事件发生时,Activity 线程就会插入累加器
空活动线程每 10 毫秒唤醒一次,以在权重中插入 0。
下面的伪代码
#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;
}
使用上述技术,我能够实现在最后一秒输入的重量,但是如何使用增压蓄能器在最后一秒内触发活动的次数?