1 #ifndef jb_event_rate_estimator_hpp 2 #define jb_event_rate_estimator_hpp 57 typename duration_t = std::chrono::microseconds,
typename counter_t =
int>
79 duration_type measurement_period,
100 template <
typename functor>
101 void sample(duration_type ts, functor update) {
121 while (last_bucket_ < bucket and running_total_ > 0) {
137 last_bucket_ = bucket;
170 duration_type measurement_period, duration_type sampling_period) {
172 std::ostringstream os;
173 os <<
"jb::event_rate_estimate - sampling period (" 174 << sampling_period.count() <<
") must be a positive number";
175 throw std::invalid_argument(os.str());
177 if (sampling_period > measurement_period) {
178 std::ostringstream os;
179 os <<
"jb::event_rate_estimate - measurement period (" 180 << measurement_period.count() <<
") is smaller than sampling period (" 181 << sampling_period.count() <<
")";
182 throw std::invalid_argument(os.str());
185 if ((measurement_period % sampling_period).count() != 0) {
186 std::ostringstream os;
187 os <<
"jb::event_rate_estimate - measurement period (" 188 << measurement_period.count()
189 <<
") must be a multiple of the sampling period (" 190 << sampling_period.count() <<
")";
191 throw std::invalid_argument(os.str());
196 typedef typename duration_type::rep rep;
197 typedef typename std::make_unsigned<rep>::type unsigned_rep;
198 unsigned_rep N = measurement_period / sampling_period;
201 if (N >= std::numeric_limits<std::size_t>::max()) {
202 std::ostringstream os;
203 os <<
"jb::event_rate_estimate - measurement period (" 204 << measurement_period.count() <<
") is too large for sampling period (" 205 << sampling_period.count() <<
")";
206 throw std::invalid_argument(os.str());
208 return static_cast<std::size_t
>(N);
231 #endif // jb_event_rate_estimator_hpp Estimate event rates over a trailing measurement period.
buckets buckets_
The time period is bucketized in intervals of 1 sampling period.
std::size_t bucket_count(duration_type measurement_period, duration_type sampling_period)
Estimate the necessary number of buckets.
duration_type::rep last_bucket_
Current number for the sampling period.
std::vector< counter_type > buckets
void init(duration_type ts)
Just initialize the circular buffer.
event_rate_estimator(duration_type measurement_period, duration_type sampling_period=duration_type(1))
Build an accumulator to estimate event rates.
std::uint64_t running_total_
Current number of events across all buckets.
void sample(duration_type ts, functor update)
Record a sample.
std::size_t end_pos_
We treat the buckets as a circular buffer, this is the pointer to the end of the buffer.
void rotate()
Rotate the circular buffer.
duration_type sampling_period_
The sampling period.
The top-level namespace for the JayBeams library.