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