1 #include "hmbdc/Copyright.hpp" 4 #include "hmbdc/Exception.hpp" 11 #include <ext/mt_allocator.h> 13 namespace hmbdc {
namespace numeric {
17 template <
typename Hist>
19 void display(ostream& os, Hist
const& hist,
size_t sampleSize
20 , vector<float> percentages = {0, 1, 10, 50, 90, 99, 100}) {
21 auto h = hist.report(percentages);
22 for (
auto i = 0u; i < percentages.size(); ++i) {
23 os << percentages[i] <<
"%=" << h[i] <<
',';
25 os <<
"sample=" << sampleSize;
38 template <
typename T,
bool DETAILED = true>
42 : threshold_(numeric_limits<T>::max())
43 , worst_(numeric_limits<T>::min())
48 : threshold_(threshold)
49 , worst_(numeric_limits<T>::min())
54 if (sample < threshold_)
57 buckets_[threshold_]++;
59 if (sample > worst_) {
66 size_t sampleSize()
const {
71 if (threshold_ == other.threshold_) {
72 for (
auto const& v : other.buckets_) {
73 buckets_[v.first] += v.second;
75 worst_ = max(worst_, other.worst_);
77 HMBDC_THROW(runtime_error,
"histogram collection parameters mismatch - failed");
79 sampleSize_ += other.sampleSize_;
83 vector<T> report(vector<float> percentages
84 = {0, 1, 10, 50, 90, 99, 100})
const {
85 vector<T> p(percentages.size());
86 if (!buckets_.empty() && !p.empty()) {
87 *p.begin() = buckets_.begin()->first;
92 for(
auto& i : buckets_) {
94 for (
auto j = perIndex; j < percentages.size() - 1; ++j) {
95 if (count * 100ul >= percentages[j] * sampleSize_) {
107 void display(ostream& os
108 , vector<float> percentages = {0, 1, 10, 50, 90, 99, 100})
const {
109 StatHistogramBase::display(os, *
this, sampleSize_, percentages);
113 ostream& operator << (ostream& os,
StatHistogram const& hist) {
119 using Buckets = map<T, size_t, less<T>
120 , __gnu_cxx::__mt_alloc<pair<const T, size_t>>>;
127 template <
typename T>
133 ,
size_t bucketCount = 1000u)
134 : thresholdMin_(thresholdMin)
135 , thresholdMax_(thresholdMax)
136 , best_(numeric_limits<T>::max())
137 , worst_(numeric_limits<T>::min())
139 , unit_((thresholdMax - thresholdMin) / bucketCount)
140 , buckets_(bucketCount + 1) {
141 if (thresholdMax <= thresholdMin) {
142 HMBDC_THROW(runtime_error,
"thresholdMax <= thresholdMin");
149 if (sample < thresholdMin_)
151 else if (sample < thresholdMax_)
152 buckets_[(sample - thresholdMin_) / unit_]++;
154 buckets_[buckets_.size() - 1]++;
157 if (sample < best_) {
161 if (sample > worst_) {
169 size_t sampleSize()
const {
174 if (thresholdMax_ == other.thresholdMax_ &&
175 thresholdMin_ == other.thresholdMin_ &&
176 buckets_.size() == other.buckets_.size()) {
177 for (
auto i = 0u; i < buckets_.size(); ++i) {
178 buckets_[i] += other.buckets_[i];
180 worst_ = max(worst_, other.worst_);
181 best_ = min(best_, other.best_);
182 sampleSize_ += other.sampleSize_;
184 HMBDC_THROW(runtime_error,
"thresholds or bucketCount mismatch - failed");
189 vector<T> report(vector<float> percentages
190 = {0, 1, 10, 50, 90, 99, 100})
const {
192 vector<T> p(percentages.size());
193 if (sampleSize_ && !p.empty()) {
195 *p.rbegin() = worst_;
197 auto val = thresholdMin_;
199 for(
auto& i : buckets_) {
202 for (
auto j = perIndex; j < percentages.size() - 1; ++j) {
203 if (count * 100ul >= percentages[j] * sampleSize_) {
204 p[j] = min(val, worst_);
216 void display(ostream& os
217 , vector<float> percentages = {0, 1, 10, 50, 90, 99, 100})
const {
218 StatHistogramBase::display(os, *
this, sampleSize_, percentages);
222 ostream& operator << (ostream& os, StatHistogram
const& hist) {
233 using Buckets = vector<size_t>;
Definition: TypedString.hpp:74
Definition: StatHistogram.hpp:128
collect sample values and keep histogram for top percentages
Definition: StatHistogram.hpp:39
Definition: StatHistogram.hpp:16
Definition: Client.hpp:11