JayBeams  0.1
Another project to have fun coding.
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
jb::histogram< binning_strategy_t, counter_type_t > Class Template Reference

A histogram class with controllable binning and range strategy. More...

#include <histogram.hpp>

Classes

struct  check_constraints
 Verify the constraints on the histogram template class template parameters, and generate better error messages when they are not met. More...
 

Public Types

typedef std::vector< counter_typecounters
 The type used to store the bins. More...
 
type traits
typedef binning_strategy_t binning_strategy
 
typedef binning_strategy::sample_type sample_type
 
typedef counter_type_t counter_type
 

Public Member Functions

 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...
 

Private Member Functions

std::size_t nbins () const
 Compute the maximum number of bins that might be needed. More...
 
sample_type midpoint (sample_type const &a, sample_type const &b) const
 Estimate a midpoint. More...
 

Private Attributes

binning_strategy binning_
 
std::uint64_t underflow_count_
 
std::uint64_t overflow_count_
 
sample_type observed_min_
 
sample_type observed_max_
 
std::uint64_t nsamples_
 
counters bins_
 

Detailed Description

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
class jb::histogram< binning_strategy_t, counter_type_t >

A histogram class with controllable binning and range strategy.

We are interested in capturing histograms of latency, and of rate measurements, and other things. The challenge is that we need good precision, but we also need to keep thousands of these histograms in memory. To increase precision in histograms one must increase the number of bins, but that increases the memory requirements.

Consider, for example, a typical latency measurement that typically ranges from 1 to 400 microseconds, but sometimes can peak to seconds. Capturing the full range at microsecond precision would require MiB of memory. While this is not a problem for a single histogram, what if we want to keep this histogram per symbol?

As an alternative, we can keep this histogram at full resolution between 0 and 1,000 microseconds, drop to 10 microsecond bins from there to 10,000 microseconds, then increase to 100 microsecond bins and so forth. For values higher than a prescribed limit we simply record "overflow".

This class implements such a histogram, with a user-provided mapping strategy to define the bins.

Template Parameters
binning_strategy_tPlease see jb::binning_strategy_concept.
counter_typeThe type used for the counters. In most applications using int is sufficient, but if you are counting really large numbers you might consider using std::uint64_t, or if you are memory constrained consider using std::int16_t.

Definition at line 46 of file histogram.hpp.

Member Typedef Documentation

◆ binning_strategy

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
typedef binning_strategy_t jb::histogram< binning_strategy_t, counter_type_t >::binning_strategy

Definition at line 54 of file histogram.hpp.

◆ counter_type

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
typedef counter_type_t jb::histogram< binning_strategy_t, counter_type_t >::counter_type

Definition at line 56 of file histogram.hpp.

◆ counters

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
typedef std::vector<counter_type> jb::histogram< binning_strategy_t, counter_type_t >::counters

The type used to store the bins.

Definition at line 230 of file histogram.hpp.

◆ sample_type

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
typedef binning_strategy::sample_type jb::histogram< binning_strategy_t, counter_type_t >::sample_type

Definition at line 55 of file histogram.hpp.

Constructor & Destructor Documentation

◆ histogram()

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
jb::histogram< binning_strategy_t, counter_type_t >::histogram ( binning_strategy const &  mapping = binning_strategy())
inlineexplicit

Construct a histogram given a mapping strategy.

Most mapping strategies are likely to be stateless, so the default constructor is adequate.

Parameters
mappingDefine how samples are mapped into bins.

Definition at line 67 of file histogram.hpp.

Member Function Documentation

◆ estimated_mean()

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
sample_type jb::histogram< binning_strategy_t, counter_type_t >::estimated_mean ( ) const
inline

Estimate the mean of the sample distribution.

Estimating the mean is O(N) where N is the number of bins.

Definition at line 98 of file histogram.hpp.

Referenced by jb::event_rate_histogram< std::chrono::nanoseconds, std::int64_t >::last_rate().

◆ estimated_quantile()

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
sample_type jb::histogram< binning_strategy_t, counter_type_t >::estimated_quantile ( double  q) const
inline

Estimate a quantile of the sample distribution.

The inverse of the accumulated density function. Find the smallest value Q such that at most q * nsamples of the samples are smaller than Q. If you prefer to think in percentiles make

q = percentile / 100.0

Estimating quantiles is O(N) where N is the number of bins.

Definition at line 136 of file histogram.hpp.

Referenced by jb::event_rate_histogram< std::chrono::nanoseconds, std::int64_t >::last_rate(), and jb::histogram< integer_range_binning< std::uint64_t >, counter_type >::summary().

◆ midpoint()

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
sample_type jb::histogram< binning_strategy_t, counter_type_t >::midpoint ( sample_type const &  a,
sample_type const &  b 
) const
inlineprivate

◆ nbins()

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
std::size_t jb::histogram< binning_strategy_t, counter_type_t >::nbins ( ) const
inlineprivate

Compute the maximum number of bins that might be needed.

Definition at line 234 of file histogram.hpp.

◆ nsamples()

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
std::uint64_t jb::histogram< binning_strategy_t, counter_type_t >::nsamples ( ) const
inline

◆ observed_max()

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
sample_type jb::histogram< binning_strategy_t, counter_type_t >::observed_max ( ) const
inline

◆ observed_min()

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
sample_type jb::histogram< binning_strategy_t, counter_type_t >::observed_min ( ) const
inline

◆ overflow_count()

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
std::uint64_t jb::histogram< binning_strategy_t, counter_type_t >::overflow_count ( ) const
inline

◆ reset()

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
void jb::histogram< binning_strategy_t, counter_type_t >::reset ( )
inline

Reset all counters.

Definition at line 224 of file histogram.hpp.

◆ sample()

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
void jb::histogram< binning_strategy_t, counter_type_t >::sample ( sample_type const &  t)
inline

◆ summary()

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
histogram_summary jb::histogram< binning_strategy_t, counter_type_t >::summary ( ) const
inline

Return a simple summary.

Definition at line 185 of file histogram.hpp.

◆ underflow_count()

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
std::uint64_t jb::histogram< binning_strategy_t, counter_type_t >::underflow_count ( ) const
inline

◆ weighted_sample()

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
void jb::histogram< binning_strategy_t, counter_type_t >::weighted_sample ( sample_type const &  t,
counter_type  weight 
)
inline

Member Data Documentation

◆ binning_

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
binning_strategy jb::histogram< binning_strategy_t, counter_type_t >::binning_
private

◆ bins_

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
counters jb::histogram< binning_strategy_t, counter_type_t >::bins_
private

◆ nsamples_

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
std::uint64_t jb::histogram< binning_strategy_t, counter_type_t >::nsamples_
private

◆ observed_max_

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
sample_type jb::histogram< binning_strategy_t, counter_type_t >::observed_max_
private

◆ observed_min_

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
sample_type jb::histogram< binning_strategy_t, counter_type_t >::observed_min_
private

◆ overflow_count_

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
std::uint64_t jb::histogram< binning_strategy_t, counter_type_t >::overflow_count_
private

◆ underflow_count_

template<typename binning_strategy_t, typename counter_type_t = unsigned int>
std::uint64_t jb::histogram< binning_strategy_t, counter_type_t >::underflow_count_
private

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