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 {
17 template <
typename Hist>
19 void display(std::ostream& os, Hist
const& hist,
size_t sampleSize
20 , std::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_(std::numeric_limits<T>::max())
43 , worst_(std::numeric_limits<T>::min())
48 : threshold_(threshold)
49 , worst_(std::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_ = std::max(worst_, other.worst_);
77 HMBDC_THROW(std::runtime_error,
"histogram collection parameters mismatch - failed");
79 sampleSize_ += other.sampleSize_;
83 std::vector<T> report(std::vector<float> percentages
84 = {0, 1, 10, 50, 90, 99, 100})
const {
85 std::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(std::ostream& os
108 , std::vector<float> percentages = {0, 1, 10, 50, 90, 99, 100})
const {
109 StatHistogramBase::display(os, *
this, sampleSize_, percentages);
113 std::ostream& operator << (std::ostream& os,
StatHistogram const& hist) {
119 using Buckets = std::map<T, size_t, std::less<T>
120 , __gnu_cxx::__mt_alloc<std::pair<const T, size_t>>
128 template <
typename T>
134 ,
size_t bucketCount = 1000u)
135 : thresholdMin_(thresholdMin)
136 , thresholdMax_(thresholdMax)
137 , best_(std::numeric_limits<T>::max())
138 , worst_(std::numeric_limits<T>::min())
140 , unit_((thresholdMax - thresholdMin) / bucketCount)
141 , buckets_(bucketCount + 1) {
142 if (thresholdMax <= thresholdMin) {
143 HMBDC_THROW(std::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_ = std::max(worst_, other.worst_);
182 best_ = std::min(best_, other.best_);
183 sampleSize_ += other.sampleSize_;
185 HMBDC_THROW(std::runtime_error,
"thresholds or bucketCount mismatch - failed");
190 std::vector<T> report(std::vector<float> percentages
191 = {0, 1, 10, 50, 90, 99, 100})
const {
193 std::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] = std::min(val, worst_);
217 void display(std::ostream& os
218 , std::vector<float> percentages = {0, 1, 10, 50, 90, 99, 100})
const {
219 StatHistogramBase::display(os, *
this, sampleSize_, percentages);
223 std::ostream& operator << (std::ostream& os,
StatHistogram const& hist) {
234 using Buckets = std::vector<size_t>;
241 template <
typename T,
bool DETAILED = true>
Definition: StatHistogram.hpp:129
collect sample values and keep histogram for top percentages
Definition: StatHistogram.hpp:39
Definition: StatHistogram.hpp:16