Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
histogram.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
16#pragma once
17
18#include <atomic>
19#include <cstdint>
20#include <limits>
21#include <map>
22#include <memory>
23#include <mutex>
24#include <string>
25#include <vector>
26
28
34{
41 std::vector<double> bucket_boundaries;
42
48 {
49 return histogram_config{
50 {0.1, 0.5, 1.0, 2.0, 5.0, 10.0, 25.0, 50.0, 100.0, 250.0, 500.0, 1000.0, 2500.0, 5000.0,
51 10000.0}};
52 }
53};
54
60{
61 uint64_t count;
62 double sum;
63 double min_value;
64 double max_value;
65 std::map<double, double> percentiles;
66 std::vector<std::pair<double, uint64_t>> buckets;
67 std::map<std::string, std::string> labels;
68
74 [[nodiscard]] auto to_prometheus(const std::string& name) const -> std::string;
75
80 [[nodiscard]] auto to_json() const -> std::string;
81};
82
106{
107public:
113
117 ~histogram() = default;
118
119 // Non-copyable (contains atomics)
120 histogram(const histogram&) = delete;
121 auto operator=(const histogram&) -> histogram& = delete;
122
123 // Movable
124 histogram(histogram&& other) noexcept;
125 auto operator=(histogram&& other) noexcept -> histogram&;
126
134 void record(double value);
135
140 [[nodiscard]] auto count() const -> uint64_t;
141
146 [[nodiscard]] auto sum() const -> double;
147
152 [[nodiscard]] auto min() const -> double;
153
158 [[nodiscard]] auto max() const -> double;
159
164 [[nodiscard]] auto mean() const -> double;
165
173 [[nodiscard]] auto percentile(double p) const -> double;
174
179 [[nodiscard]] auto p50() const -> double { return percentile(0.50); }
180
185 [[nodiscard]] auto p95() const -> double { return percentile(0.95); }
186
191 [[nodiscard]] auto p99() const -> double { return percentile(0.99); }
192
197 [[nodiscard]] auto p999() const -> double { return percentile(0.999); }
198
203 [[nodiscard]] auto buckets() const -> std::vector<std::pair<double, uint64_t>>;
204
210 [[nodiscard]] auto snapshot(const std::map<std::string, std::string>& labels = {}) const
211 -> histogram_snapshot;
212
218 void reset();
219
220private:
221 std::vector<double> boundaries_;
222 std::unique_ptr<std::atomic<uint64_t>[]> bucket_counts_;
223 size_t bucket_count_{0};
224 std::atomic<uint64_t> count_{0};
225 std::atomic<double> sum_{0.0};
226 std::atomic<double> min_{std::numeric_limits<double>::infinity()};
227 std::atomic<double> max_{-std::numeric_limits<double>::infinity()};
228 mutable std::mutex mutex_;
229
235 [[nodiscard]] auto find_bucket(double value) const -> size_t;
236
241 void update_min(double value);
242
247 void update_max(double value);
248
253 void add_to_sum(double value);
254};
255
256} // namespace kcenon::network::metrics
Thread-safe histogram for capturing value distributions.
Definition histogram.h:106
histogram(const histogram &)=delete
auto p999() const -> double
Get 99.9th percentile.
Definition histogram.h:197
auto p99() const -> double
Get 99th percentile.
Definition histogram.h:191
auto operator=(const histogram &) -> histogram &=delete
std::vector< double > boundaries_
Definition histogram.h:221
auto p95() const -> double
Get 95th percentile.
Definition histogram.h:185
std::unique_ptr< std::atomic< uint64_t >[]> bucket_counts_
Definition histogram.h:222
Configuration for histogram bucket boundaries.
Definition histogram.h:34
std::vector< double > bucket_boundaries
Explicit bucket boundaries (upper bounds)
Definition histogram.h:41
static auto default_latency_config() -> histogram_config
Create default configuration for network latencies.
Definition histogram.h:47
Immutable snapshot of histogram state for export.
Definition histogram.h:60
std::map< double, double > percentiles
Percentile -> value mapping.
Definition histogram.h:65
std::map< std::string, std::string > labels
Additional metric labels.
Definition histogram.h:67
uint64_t count
Total number of observations.
Definition histogram.h:61
auto to_json() const -> std::string
Export histogram as JSON.
Definition histogram.cpp:82
double sum
Sum of all observed values.
Definition histogram.h:62
double min_value
Minimum observed value.
Definition histogram.h:63
std::vector< std::pair< double, uint64_t > > buckets
Boundary -> cumulative count.
Definition histogram.h:66
auto to_prometheus(const std::string &name) const -> std::string
Export histogram in Prometheus format.
Definition histogram.cpp:19
double max_value
Maximum observed value.
Definition histogram.h:64