JayBeams  0.1
Another project to have fun coding.
Public Member Functions | Private Attributes | List of all members
jb::event_rate_histogram< duration_type, counter_type, rate_counter_type > Class Template Reference

Keep a histogram of observed event rates. More...

#include <event_rate_histogram.hpp>

Inheritance diagram for jb::event_rate_histogram< duration_type, counter_type, rate_counter_type >:
jb::histogram< integer_range_binning< std::uint64_t >, counter_type >

Public Types

Type traits.
typedef integer_range_binning< std::uint64_t > binning_strategy
 
typedef histogram< binning_strategy, counter_typerate_histogram
 

Public Member Functions

 event_rate_histogram (std::uint64_t max_expected_rate, duration_type measurement_period, duration_type sampling_period=duration_type(1))
 Constructor. More...
 
void sample (duration_type ts)
 Record a new sample. More...
 
std::uint64_t last_rate () const
 Get the last sample, if any. More...
 

Private Attributes

event_rate_estimator< duration_type, rate_counter_type > rate_
 
std::uint64_t last_rate_
 

Additional Inherited Members

- Private Types inherited from jb::histogram< integer_range_binning< std::uint64_t >, counter_type >
typedef std::vector< counter_typecounters
 The type used to store the bins. More...
 
typedef integer_range_binning< std::uint64_t > binning_strategy
 
typedef binning_strategy::sample_type sample_type
 
typedef counter_type counter_type
 
- Private Member Functions inherited from jb::histogram< integer_range_binning< std::uint64_t >, counter_type >
 histogram (binning_strategy const &mapping=binning_strategy())
 Construct a histogram given a mapping strategy. More...
 
std::uint64_t nsamples () const
 Return the number of samples observed to this point. More...
 
sample_type observed_min () const
 Return the smallest sample value observed to this point. More...
 
sample_type observed_max () const
 Return the largest sample value observed to this point. More...
 
sample_type estimated_mean () const
 Estimate the mean of the sample distribution. More...
 
sample_type estimated_quantile (double q) const
 Estimate a quantile of the sample distribution. More...
 
std::uint64_t underflow_count () const
 Return the number of samples smaller than the histogram range. More...
 
std::uint64_t overflow_count () const
 Return the number of samples larger than the histogram range. More...
 
histogram_summary summary () const
 Return a simple summary. More...
 
void sample (sample_type const &t)
 Record a new sample. More...
 
void weighted_sample (sample_type const &t, counter_type weight)
 Record a new sample. More...
 
void reset ()
 Reset all counters. More...
 

Detailed Description

template<typename duration_type = std::chrono::microseconds, typename counter_type = int, typename rate_counter_type = int>
class jb::event_rate_histogram< duration_type, counter_type, rate_counter_type >

Keep a histogram of observed event rates.

This class collects running statistics about event rates. While an event_rate_estimator computes the number of events in the last X seconds, this class keeps a tally of the observed event rates, which can be used to build median, maximum and any other quantile of the message rate.

The user specifies the maximum message rate to keep at full resolution, any rate above that value is simply recorded in the histogram overflow bucket.

For example to keep track of per-millisecond message rate you would instantiate the class as follows:

int, std::chrono::microseconds> my_rate_histogram;
int max_expected_rate = 1000000;
my_rate_histogram rate_histo(
max_expected_rate, std::chrono::microseconds(1000));
...
rate_histo.sample(timestamp);
...
rate_histo.sample(timestamp);

The user of the class must provide a maximum expected message rate, higher rates are recorded in a single 'overflow' bucket. If you guess too high on the rate the rate_histo object would consume more memory than necessary. If you guess too low the high quantiles might be less accurate. Memory requirements are low (the data structure is basically a vector of ints), but can be an issue if you create many histograms (for example, one histogram per security when analyzing a market data feed).

Template Parameters
duration_typeDefine the units used to measure time. This class assumes all events are timestamped with a class compatible with std::chrono::duration<>, against some epoch that is defined by convention but not explicitly provided to the class. This is convenient because some feeds just provide microseconds or nanoseconds since midnight, while others use microseconds since the Unix Epoch. If you are using some kind of std::chrono::time_point<> object for your timestamps simply call time_since_epoch() on the object before sampling.
counter_typeThe type used in the histogram counters. For most applications int is adequate, but you may use a smaller type if (a) you are memory constrained, and (b) you do not expect high values in the counters. You might also consider a wider integral type if you expect very high number of events.
rate_counter_typeThe type used in the event rate counters. Similar tradeofss as counter_type.

Definition at line 68 of file event_rate_histogram.hpp.

Member Typedef Documentation

◆ binning_strategy

template<typename duration_type = std::chrono::microseconds, typename counter_type = int, typename rate_counter_type = int>
typedef integer_range_binning<std::uint64_t> jb::event_rate_histogram< duration_type, counter_type, rate_counter_type >::binning_strategy

Definition at line 75 of file event_rate_histogram.hpp.

◆ rate_histogram

template<typename duration_type = std::chrono::microseconds, typename counter_type = int, typename rate_counter_type = int>
typedef histogram<binning_strategy, counter_type> jb::event_rate_histogram< duration_type, counter_type, rate_counter_type >::rate_histogram

Definition at line 76 of file event_rate_histogram.hpp.

Constructor & Destructor Documentation

◆ event_rate_histogram()

template<typename duration_type = std::chrono::microseconds, typename counter_type = int, typename rate_counter_type = int>
jb::event_rate_histogram< duration_type, counter_type, rate_counter_type >::event_rate_histogram ( std::uint64_t  max_expected_rate,
duration_type  measurement_period,
duration_type  sampling_period = duration_type(1) 
)
inline

Constructor.

Parameters
max_expected_rateThe histogram is kept at full resolution up to this rate, any periods with more events are counted only in the overflow bin. If this value is too low for the observed rates, the high quantiles may not be very accurate.
measurement_periodover what period we measure event rates.
sampling_periodhow often do we measure event rates.

Definition at line 89 of file event_rate_histogram.hpp.

Member Function Documentation

◆ last_rate()

template<typename duration_type = std::chrono::microseconds, typename counter_type = int, typename rate_counter_type = int>
std::uint64_t jb::event_rate_histogram< duration_type, counter_type, rate_counter_type >::last_rate ( ) const
inline

Get the last sample, if any.

Definition at line 106 of file event_rate_histogram.hpp.

◆ sample()

template<typename duration_type = std::chrono::microseconds, typename counter_type = int, typename rate_counter_type = int>
void jb::event_rate_histogram< duration_type, counter_type, rate_counter_type >::sample ( duration_type  ts)
inline

Record a new sample.

Definition at line 98 of file event_rate_histogram.hpp.

Referenced by jb::offline_feed_statistics::record_sample().

Member Data Documentation

◆ last_rate_

template<typename duration_type = std::chrono::microseconds, typename counter_type = int, typename rate_counter_type = int>
std::uint64_t jb::event_rate_histogram< duration_type, counter_type, rate_counter_type >::last_rate_
private

◆ rate_

template<typename duration_type = std::chrono::microseconds, typename counter_type = int, typename rate_counter_type = int>
event_rate_estimator<duration_type, rate_counter_type> jb::event_rate_histogram< duration_type, counter_type, rate_counter_type >::rate_
private

The documentation for this class was generated from the following file: