Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Utility.hpp
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#ifndef TOOLBOX_HDR_UTILITY
16#define TOOLBOX_HDR_UTILITY
17
18#include "Iterator.hpp"
19#include "Histogram.hpp"
20
22#include <toolbox/Config.h>
23
24#include <cstdint>
25#include <format>
26
27namespace toolbox {
29inline namespace hdr {
30class Histogram;
31
32TOOLBOX_API std::int64_t min(const Histogram& h) noexcept;
33TOOLBOX_API std::int64_t max(const Histogram& h) noexcept;
34
40TOOLBOX_API std::int64_t value_at_percentile(const Histogram& h, double percentile) noexcept;
41
46TOOLBOX_API double mean(const Histogram& h) noexcept;
47
52TOOLBOX_API double stddev(const Histogram& h) noexcept;
53
55 const Histogram& h;
56 std::int32_t ticks_per_half_distance{5};
57 double value_scale{1000.0};
58};
59
60inline auto put_percentiles(const Histogram& h, std::int32_t ticks_per_half_distance,
61 double value_scale) noexcept
62{
63 return PutPercentiles{h, ticks_per_half_distance, value_scale};
64}
65
66template <typename StreamT>
67 requires Streamable<StreamT>
69{
70 const auto sf = pp.h.significant_figures();
71
72 os << " Value Percentile TotalCount 1/(1-Percentile)\n\n";
73
74 PercentileIterator iter{pp.h, pp.ticks_per_half_distance};
75 while (iter.next()) {
76 const double value{iter.highest_equivalent_value() / pp.value_scale};
77 const double percentile{iter.percentile() / 100.0};
78 const int64_t total_count{iter.cumulative_count()};
79
80 os << std::format("{:12.{}f}", value, sf);
81 os << std::format("{:15.6f}", percentile);
82 os << std::format("{:11}", total_count);
83
84 if (percentile < 1.0) {
85 const double inverted_percentile{(1.0 / (1.0 - percentile))};
86 os << std::format("{:15.2f}", inverted_percentile);
87 }
88 os << '\n';
89 }
90
91 const double mean_val{mean(pp.h) / pp.value_scale};
92 const double stddev_val{stddev(pp.h)};
93 const double max_val{pp.h.max() / pp.value_scale};
94 const int64_t total_val{pp.h.total_count()};
95
96 // clang-format off
97 os << "#[Mean = " << std::format("{:12.{}f}", mean_val, sf)
98 << ", StdDeviation = " << std::format("{:12.{}f}", stddev_val, sf)
99 << "]\n#[Max = " << std::format("{:12.{}f}", max_val, sf)
100 << ", TotalCount = " << std::format("{:12}", total_val)
101 << "]\n#[Buckets = " << std::format("{:12}", pp.h.bucket_count())
102 << ", SubBuckets = " << std::format("{:12}", pp.h.sub_bucket_count())
103 << "]";
104 return os;
105}
106
107
108} // namespace hdr
109} // namespace toolbox
110
111#endif // TOOLBOX_HDR_UTILITY
#define TOOLBOX_API
Definition Config.h:39
A High Dynamic Range (HDR) Histogram.
Definition Histogram.hpp:64
std::int32_t significant_figures() const noexcept
Definition Histogram.hpp:81
std::int64_t highest_equivalent_value() const noexcept
Definition Iterator.hpp:50
PercentileIterator is a percentile iterator.
Definition Iterator.hpp:87
int64_t min(const Histogram &h) noexcept
Definition Utility.cpp:37
double mean(const Histogram &h) noexcept
Definition Utility.cpp:62
auto put_percentiles(const Histogram &h, std::int32_t ticks_per_half_distance, double value_scale) noexcept
Definition Utility.hpp:60
int64_t value_at_percentile(const Histogram &h, double percentile) noexcept
Definition Utility.cpp:47
int64_t max(const Histogram &h) noexcept
Definition Utility.cpp:42
double stddev(const Histogram &h) noexcept
Definition Utility.cpp:76
StreamT & operator<<(StreamT &os, PutPercentiles pp)
Definition Utility.hpp:68
constexpr auto bind() noexcept
Definition Slot.hpp:92
const Histogram & h
Definition Utility.hpp:55
std::int32_t ticks_per_half_distance
Definition Utility.hpp:56