Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
sliding_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
19
20#include <chrono>
21#include <deque>
22#include <memory>
23#include <mutex>
24
26
32{
34 std::chrono::seconds window_duration{60};
35 size_t bucket_count{6};
36
42 {
44 std::chrono::seconds{60}, 6};
45 }
46};
47
67{
68public:
74
78 ~sliding_histogram() = default;
79
80 // Non-copyable
83
84 // Movable
85 sliding_histogram(sliding_histogram&& other) noexcept;
86 auto operator=(sliding_histogram&& other) noexcept -> sliding_histogram&;
87
95 void record(double value);
96
101 [[nodiscard]] auto count() const -> uint64_t;
102
107 [[nodiscard]] auto sum() const -> double;
108
113 [[nodiscard]] auto mean() const -> double;
114
120 [[nodiscard]] auto percentile(double p) const -> double;
121
126 [[nodiscard]] auto p50() const -> double { return percentile(0.50); }
127
132 [[nodiscard]] auto p95() const -> double { return percentile(0.95); }
133
138 [[nodiscard]] auto p99() const -> double { return percentile(0.99); }
139
144 [[nodiscard]] auto p999() const -> double { return percentile(0.999); }
145
151 [[nodiscard]] auto snapshot(const std::map<std::string, std::string>& labels = {}) const
152 -> histogram_snapshot;
153
158 [[nodiscard]] auto window_duration() const -> std::chrono::seconds
159 {
161 }
162
168 void reset();
169
170private:
172 {
174 std::chrono::steady_clock::time_point start_time;
175
176 explicit time_bucket(const histogram_config& cfg)
177 : hist(cfg), start_time(std::chrono::steady_clock::now())
178 {
179 }
180 };
181
183 std::chrono::milliseconds bucket_duration_;
184 std::deque<std::unique_ptr<time_bucket>> buckets_;
185 mutable std::mutex mutex_;
186
190 void expire_old_buckets();
191
197
202 [[nodiscard]] auto aggregate() const -> histogram_snapshot;
203};
204
205} // namespace kcenon::network::metrics
Thread-safe histogram for capturing value distributions.
Definition histogram.h:106
Time-windowed histogram for tracking recent latency distributions.
auto percentile(double p) const -> double
Calculate percentile value for current window.
auto mean() const -> double
Get mean of all observations in current window.
auto operator=(const sliding_histogram &) -> sliding_histogram &=delete
auto p50() const -> double
Get 50th percentile (median) for current window.
std::deque< std::unique_ptr< time_bucket > > buckets_
void expire_old_buckets()
Expire old buckets outside the window.
auto aggregate() const -> histogram_snapshot
Create aggregated histogram from all buckets.
auto sum() const -> double
Get sum of all observations in current window.
auto count() const -> uint64_t
Get total number of observations in current window.
auto p99() const -> double
Get 99th percentile for current window.
auto p95() const -> double
Get 95th percentile for current window.
void record(double value)
Record a value observation.
sliding_histogram(sliding_histogram_config cfg=sliding_histogram_config::default_config())
Construct sliding histogram with configuration.
auto get_current_bucket() -> time_bucket &
Get or create current time bucket.
auto window_duration() const -> std::chrono::seconds
Get the window duration.
sliding_histogram(const sliding_histogram &)=delete
auto snapshot(const std::map< std::string, std::string > &labels={}) const -> histogram_snapshot
Create snapshot aggregating all time buckets in current window.
auto p999() const -> double
Get 99.9th percentile for current window.
Histogram metric implementation for latency distribution tracking.
Configuration for histogram bucket boundaries.
Definition histogram.h:34
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
Configuration for sliding window histogram.
std::chrono::seconds window_duration
Total window duration.
static auto default_config() -> sliding_histogram_config
Create default configuration (60 second window, 6 buckets = 10s each)
histogram_config hist_config
Histogram bucket configuration.