rippled
Loading...
Searching...
No Matches
Histogram.h
1#ifndef XRPL_TEST_CSF_HISTOGRAM_H_INCLUDED
2#define XRPL_TEST_CSF_HISTOGRAM_H_INCLUDED
3
4#include <algorithm>
5#include <cassert>
6#include <chrono>
7#include <cmath>
8#include <map>
9
10namespace ripple {
11namespace test {
12namespace csf {
13
25template <class T, class Compare = std::less<T>>
27{
28 // TODO: Consider logarithimic bins around expected median if this becomes
29 // unscaleable
32
33public:
35 void
36 insert(T const& s)
37 {
38 ++counts_[s];
39 ++samples;
40 }
41
44 size() const
45 {
46 return samples;
47 }
48
51 numBins() const
52 {
53 return counts_.size();
54 }
55
57 T
58 minValue() const
59 {
60 return counts_.empty() ? T{} : counts_.begin()->first;
61 }
62
64 T
65 maxValue() const
66 {
67 return counts_.empty() ? T{} : counts_.rbegin()->first;
68 }
69
71 T
72 avg() const
73 {
74 T tmp{};
75 if (samples == 0)
76 return tmp;
77 // Since counts are sorted, shouldn't need to worry much about numerical
78 // error
79 for (auto const& [bin, count] : counts_)
80 {
81 tmp += bin * count;
82 }
83 return tmp / samples;
84 }
85
92 T
93 percentile(float p) const
94 {
95 assert(p >= 0 && p <= 1);
97
98 if (counts_.empty())
99 return T{};
100
101 auto it = counts_.begin();
102 std::size_t cumsum = it->second;
103 while (it != counts_.end() && cumsum < pos)
104 {
105 ++it;
106 cumsum += it->second;
107 }
108 return it->first;
109 }
110};
111
112} // namespace csf
113} // namespace test
114} // namespace ripple
115
116#endif
T avg() const
Histogram average.
Definition Histogram.h:72
void insert(T const &s)
Insert an sample.
Definition Histogram.h:36
T maxValue() const
Maximum observed value.
Definition Histogram.h:65
T minValue() const
Minimum observed value.
Definition Histogram.h:58
T percentile(float p) const
Calculate the given percentile of the distribution.
Definition Histogram.h:93
std::size_t numBins() const
The number of distinct samples (bins)
Definition Histogram.h:51
std::size_t size() const
The number of samples.
Definition Histogram.h:44
std::map< T, std::size_t, Compare > counts_
Definition Histogram.h:30
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
T round(T... args)