rippled
Loading...
Searching...
No Matches
Histogram.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2017 Ripple Labs Inc
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_TEST_CSF_HISTOGRAM_H_INCLUDED
21#define RIPPLE_TEST_CSF_HISTOGRAM_H_INCLUDED
22
23#include <algorithm>
24#include <cassert>
25#include <chrono>
26#include <cmath>
27#include <map>
28
29namespace ripple {
30namespace test {
31namespace csf {
32
44template <class T, class Compare = std::less<T>>
46{
47 // TODO: Consider logarithimic bins around expected median if this becomes
48 // unscaleable
51
52public:
54 void
55 insert(T const& s)
56 {
57 ++counts_[s];
58 ++samples;
59 }
60
63 size() const
64 {
65 return samples;
66 }
67
70 numBins() const
71 {
72 return counts_.size();
73 }
74
76 T
77 minValue() const
78 {
79 return counts_.empty() ? T{} : counts_.begin()->first;
80 }
81
83 T
84 maxValue() const
85 {
86 return counts_.empty() ? T{} : counts_.rbegin()->first;
87 }
88
90 T
91 avg() const
92 {
93 T tmp{};
94 if (samples == 0)
95 return tmp;
96 // Since counts are sorted, shouldn't need to worry much about numerical
97 // error
98 for (auto const& [bin, count] : counts_)
99 {
100 tmp += bin * count;
101 }
102 return tmp / samples;
103 }
104
111 T
112 percentile(float p) const
113 {
114 assert(p >= 0 && p <= 1);
115 std::size_t pos = std::round(p * samples);
116
117 if (counts_.empty())
118 return T{};
119
120 auto it = counts_.begin();
121 std::size_t cumsum = it->second;
122 while (it != counts_.end() && cumsum < pos)
123 {
124 ++it;
125 cumsum += it->second;
126 }
127 return it->first;
128 }
129};
130
131} // namespace csf
132} // namespace test
133} // namespace ripple
134
135#endif
T avg() const
Histogram average.
Definition Histogram.h:91
void insert(T const &s)
Insert an sample.
Definition Histogram.h:55
T maxValue() const
Maximum observed value.
Definition Histogram.h:84
T minValue() const
Minimum observed value.
Definition Histogram.h:77
T percentile(float p) const
Calculate the given percentile of the distribution.
Definition Histogram.h:112
std::size_t numBins() const
The number of distinct samples (bins)
Definition Histogram.h:70
std::size_t size() const
The number of samples.
Definition Histogram.h:63
std::map< T, std::size_t, Compare > counts_
Definition Histogram.h:49
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
T round(T... args)