1 #include "hmbdc/Copyright.hpp" 4 #include "hmbdc/Exception.hpp" 11 #include <ext/mt_allocator.h> 13 namespace hmbdc {
namespace numeric {
14 namespace stathistogram_detail {
18 template <
typename Hist>
20 void display(ostream& os, Hist
const& hist,
size_t sampleSize
21 , vector<float> percentages = {0, 1, 10, 50, 90, 99, 100}) {
22 auto h = hist.report(percentages);
23 for (
auto i = 0u; i < percentages.size(); ++i) {
24 os << percentages[i] <<
"%=" << h[i] <<
',';
26 os <<
"sample=" << sampleSize;
39 template <
typename T,
bool DETAILED = true>
43 : threshold_(numeric_limits<T>::max())
44 , worst_(numeric_limits<T>::min())
49 : threshold_(threshold)
50 , worst_(numeric_limits<T>::min())
55 if (sample < threshold_)
58 buckets_[threshold_]++;
60 if (sample > worst_) {
67 size_t sampleSize()
const {
72 if (threshold_ == other.threshold_) {
73 for (
auto const& v : other.buckets_) {
74 buckets_[v.first] += v.second;
76 worst_ = max(worst_, other.worst_);
78 HMBDC_THROW(runtime_error,
"histogram collection parameters mismatch - failed");
80 sampleSize_ += other.sampleSize_;
84 vector<T> report(vector<float> percentages
85 = {0, 1, 10, 50, 90, 99, 100})
const {
86 vector<T> p(percentages.size());
87 if (!buckets_.empty() && !p.empty()) {
88 *p.begin() = buckets_.begin()->first;
93 for(
auto& i : buckets_) {
95 for (
auto j = perIndex; j < percentages.size() - 1; ++j) {
96 if (count * 100ul >= percentages[j] * sampleSize_) {
108 void display(ostream& os
109 , vector<float> percentages = {0, 1, 10, 50, 90, 99, 100})
const {
110 StatHistogramBase::display(os, *
this, sampleSize_, percentages);
114 ostream& operator << (ostream& os,
StatHistogram const& hist) {
120 using Buckets = map<T, size_t, less<T>
121 , __gnu_cxx::__mt_alloc<pair<const T, size_t>>
129 template <
typename T>
135 ,
size_t bucketCount = 1000u)
136 : thresholdMin_(thresholdMin)
137 , thresholdMax_(thresholdMax)
138 , best_(numeric_limits<T>::max())
139 , worst_(numeric_limits<T>::min())
141 , unit_((thresholdMax - thresholdMin) / bucketCount)
142 , buckets_(bucketCount + 1) {
143 if (thresholdMax <= thresholdMin) {
144 HMBDC_THROW(runtime_error,
"thresholdMax <= thresholdMin");
151 if (sample < thresholdMin_)
153 else if (sample < thresholdMax_)
154 buckets_[(sample - thresholdMin_) / unit_]++;
156 buckets_[buckets_.size() - 1]++;
159 if (sample < best_) {
163 if (sample > worst_) {
171 size_t sampleSize()
const {
176 if (thresholdMax_ == other.thresholdMax_ &&
177 thresholdMin_ == other.thresholdMin_ &&
178 buckets_.size() == other.buckets_.size()) {
179 for (
auto i = 0u; i < buckets_.size(); ++i) {
180 buckets_[i] += other.buckets_[i];
182 worst_ = max(worst_, other.worst_);
183 best_ = min(best_, other.best_);
184 sampleSize_ += other.sampleSize_;
186 HMBDC_THROW(runtime_error,
"thresholds or bucketCount mismatch - failed");
191 vector<T> report(vector<float> percentages
192 = {0, 1, 10, 50, 90, 99, 100})
const {
194 vector<T> p(percentages.size());
195 if (sampleSize_ && !p.empty()) {
197 *p.rbegin() = worst_;
199 auto val = thresholdMin_;
201 for(
auto& i : buckets_) {
204 for (
auto j = perIndex; j < percentages.size() - 1; ++j) {
205 if (count * 100ul >= percentages[j] * sampleSize_) {
206 p[j] = min(val, worst_);
218 void display(ostream& os
219 , vector<float> percentages = {0, 1, 10, 50, 90, 99, 100})
const {
220 StatHistogramBase::display(os, *
this, sampleSize_, percentages);
224 ostream& operator << (ostream& os, StatHistogram
const& hist) {
235 using Buckets = vector<size_t>;
242 template <
typename T,
bool DETAILED = true>
Definition: StatHistogram.hpp:130
Definition: TypedString.hpp:76
collect sample values and keep histogram for top percentages
Definition: StatHistogram.hpp:40
Definition: StatHistogram.hpp:17