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>>>;
128 template <
typename T>
134 ,
size_t bucketCount = 1000u)
135 : thresholdMin_(thresholdMin)
136 , thresholdMax_(thresholdMax)
137 , best_(numeric_limits<T>::max())
138 , worst_(numeric_limits<T>::min())
140 , unit_((thresholdMax - thresholdMin) / bucketCount)
141 , buckets_(bucketCount + 1) {
142 if (thresholdMax <= thresholdMin) {
143 HMBDC_THROW(runtime_error,
"thresholdMax <= thresholdMin");
150 if (sample < thresholdMin_)
152 else if (sample < thresholdMax_)
153 buckets_[(sample - thresholdMin_) / unit_]++;
155 buckets_[buckets_.size() - 1]++;
158 if (sample < best_) {
162 if (sample > worst_) {
170 size_t sampleSize()
const {
175 if (thresholdMax_ == other.thresholdMax_ &&
176 thresholdMin_ == other.thresholdMin_ &&
177 buckets_.size() == other.buckets_.size()) {
178 for (
auto i = 0u; i < buckets_.size(); ++i) {
179 buckets_[i] += other.buckets_[i];
181 worst_ = max(worst_, other.worst_);
182 best_ = min(best_, other.best_);
183 sampleSize_ += other.sampleSize_;
185 HMBDC_THROW(runtime_error,
"thresholds or bucketCount mismatch - failed");
190 vector<T> report(vector<float> percentages
191 = {0, 1, 10, 50, 90, 99, 100})
const {
193 vector<T> p(percentages.size());
194 if (sampleSize_ && !p.empty()) {
196 *p.rbegin() = worst_;
198 auto val = thresholdMin_;
200 for(
auto& i : buckets_) {
203 for (
auto j = perIndex; j < percentages.size() - 1; ++j) {
204 if (count * 100ul >= percentages[j] * sampleSize_) {
205 p[j] = min(val, worst_);
217 void display(ostream& os
218 , vector<float> percentages = {0, 1, 10, 50, 90, 99, 100})
const {
219 StatHistogramBase::display(os, *
this, sampleSize_, percentages);
223 ostream& operator << (ostream& os, StatHistogram
const& hist) {
234 using Buckets = vector<size_t>;
241 template <
typename T,
bool DETAILED = true>
Definition: StatHistogram.hpp:129
Definition: TypedString.hpp:74
collect sample values and keep histogram for top percentages
Definition: StatHistogram.hpp:40
Definition: StatHistogram.hpp:17