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#ifndef RIPPLE_TEST_CSF_HISTOGRAM_H_INCLUDED
20#define RIPPLE_TEST_CSF_HISTOGRAM_H_INCLUDED
21
22#include <algorithm>
23#include <cassert>
24#include <chrono>
25#include <cmath>
26#include <map>
27
28namespace ripple {
29namespace test {
30namespace csf {
31
43template <class T, class Compare = std::less<T>>
45{
46 // TODO: Consider logarithimic bins around expected median if this becomes
47 // unscaleable
50
51public:
53 void
54 insert(T const& s)
55 {
56 ++counts_[s];
57 ++samples;
58 }
59
62 size() const
63 {
64 return samples;
65 }
66
69 numBins() const
70 {
71 return counts_.size();
72 }
73
75 T
76 minValue() const
77 {
78 return counts_.empty() ? T{} : counts_.begin()->first;
79 }
80
82 T
83 maxValue() const
84 {
85 return counts_.empty() ? T{} : counts_.rbegin()->first;
86 }
87
89 T
90 avg() const
91 {
92 T tmp{};
93 if (samples == 0)
94 return tmp;
95 // Since counts are sorted, shouldn't need to worry much about numerical
96 // error
97 for (auto const& [bin, count] : counts_)
98 {
99 tmp += bin * count;
100 }
101 return tmp / samples;
102 }
103
110 T
111 percentile(float p) const
112 {
113 assert(p >= 0 && p <= 1);
114 std::size_t pos = std::round(p * samples);
115
116 if (counts_.empty())
117 return T{};
118
119 auto it = counts_.begin();
120 std::size_t cumsum = it->second;
121 while (it != counts_.end() && cumsum < pos)
122 {
123 ++it;
124 cumsum += it->second;
125 }
126 return it->first;
127 }
128};
129
130} // namespace csf
131} // namespace test
132} // namespace ripple
133
134#endif
Basic histogram.
Definition: Histogram.h:45
T avg() const
Histogram average.
Definition: Histogram.h:90
void insert(T const &s)
Insert an sample.
Definition: Histogram.h:54
T maxValue() const
Maximum observed value.
Definition: Histogram.h:83
T minValue() const
Minimum observed value.
Definition: Histogram.h:76
T percentile(float p) const
Calculate the given percentile of the distribution.
Definition: Histogram.h:111
std::size_t numBins() const
The number of distinct samples (bins)
Definition: Histogram.h:69
std::size_t size() const
The number of samples.
Definition: Histogram.h:62
std::map< T, std::size_t, Compare > counts_
Definition: Histogram.h:48
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
T round(T... args)