Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Utility.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2021 Reactive Markets Limited
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#include "Utility.hpp"
17
18#include "Histogram.hpp"
19#include "Iterator.hpp"
20
21#include <boost/io/ios_state.hpp>
22
23#include <cmath>
24#include <iomanip>
25#include <ostream>
26
27namespace toolbox {
28inline namespace hdr {
29using namespace std;
30namespace {
31int64_t get_count_at_percentile(const Histogram& h, double percentile) noexcept
32{
33 if (percentile > 100.0) {
34 percentile = 100.0;
35 }
36 const int count_at_percentile = (percentile * h.total_count() / 100) + 0.5;
37 return std::max<int64_t>(count_at_percentile, 1);
38}
39} // namespace
40
41int64_t min(const Histogram& h) noexcept
42{
43 return h.min();
44}
45
46int64_t max(const Histogram& h) noexcept
47{
48 return h.max();
49}
50
51int64_t value_at_percentile(const Histogram& h, double percentile) noexcept
52{
54
55 int64_t total{0};
56 Iterator iter{h};
57 while (iter.next()) {
58 total += iter.count();
60 return h.highest_equivalent_value(iter.value());
61 }
62 }
63 return 0;
64}
65
66double mean(const Histogram& h) noexcept
67{
68 const auto total_count = h.total_count();
69
70 int64_t total{0};
71 Iterator iter{h};
72 while (iter.next()) {
73 if (iter.count() != 0) {
74 total += iter.count() * h.median_equivalent_value(iter.value());
75 }
76 }
77 return double(total) / total_count;
78}
79
80double stddev(const Histogram& h) noexcept
81{
82 const int64_t total_count{h.total_count()};
83 const double mean_val{mean(h)};
84
85 double geometric_dev_total{0.0};
86 Iterator iter{h};
87 while (iter.next()) {
88 if (iter.count() != 0) {
89 const double dev{h.median_equivalent_value(iter.value()) - mean_val};
91 }
92 }
93 return sqrt(geometric_dev_total / total_count);
94}
95
96ostream& operator<<(ostream& os, PutPercentiles pp)
97{
98 const auto sf = pp.h.significant_figures();
99 boost::io::ios_all_saver all_saver{os};
100
101 os << " Value Percentile TotalCount 1/(1-Percentile)\n\n";
102
103 PercentileIterator iter{pp.h, pp.ticks_per_half_distance};
104 while (iter.next()) {
105 const double value{iter.highest_equivalent_value() / pp.value_scale};
106 const double percentile{iter.percentile() / 100.0};
107 const int64_t total_count{iter.cumulative_count()};
108
109 // clang-format off
110 os << setw(12) << fixed << setprecision(sf) << value
111 << setw(15) << fixed << setprecision(6) << percentile
112 << setw(11) << total_count;
113 // clang-format on
114
115 if (percentile < 1.0) {
116 const double inverted_percentile{(1.0 / (1.0 - percentile))};
117 os << setw(15) << fixed << setprecision(2) << inverted_percentile;
118 }
119 os << '\n';
120 }
121
122 const double mean_val{mean(pp.h) / pp.value_scale};
123 const double stddev_val{stddev(pp.h)};
124 const double max_val{pp.h.max() / pp.value_scale};
125 const int64_t total_val{pp.h.total_count()};
126
127 // clang-format off
128 return os
129 << "#[Mean = " << setw(12) << fixed << setprecision(sf) << mean_val
130 << ", StdDeviation = " << setw(12) << fixed << setprecision(sf) << stddev_val
131 << "]\n"
132 "#[Max = " << setw(12) << fixed << setprecision(sf) << max_val
133 << ", TotalCount = " << setw(12) << total_val
134 << "]\n"
135 "#[Buckets = " << setw(12) << pp.h.bucket_count()
136 << ", SubBuckets = " << setw(12) << pp.h.sub_bucket_count()
137 << "]";
138 // clang-format on
139}
140
141} // namespace hdr
142} // namespace toolbox
A High Dynamic Range (HDR) Histogram.
Definition Histogram.hpp:64
Iterator is the base iterator for all iterator types.
Definition Iterator.hpp:28
std::int64_t highest_equivalent_value() const noexcept
Definition Iterator.hpp:50
std::int64_t count() const noexcept
Value directly from array for the current counts_index.
Definition Iterator.hpp:45
PercentileIterator is a percentile iterator.
Definition Iterator.hpp:87
STL namespace.
int64_t min(const Histogram &h) noexcept
Definition Utility.cpp:41
double mean(const Histogram &h) noexcept
Definition Utility.cpp:66
int64_t value_at_percentile(const Histogram &h, double percentile) noexcept
Definition Utility.cpp:51
int64_t max(const Histogram &h) noexcept
Definition Utility.cpp:46
ostream & operator<<(ostream &os, PutPercentiles pp)
Definition Utility.cpp:96
double stddev(const Histogram &h) noexcept
Definition Utility.cpp:80
constexpr auto bind() noexcept
Definition Slot.hpp:92