|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
Sliding window counter for throughput measurement. More...
#include <sliding_window_counter.h>

Classes | |
| struct | Bucket |
| Time bucket structure. More... | |
Public Member Functions | |
| SlidingWindowCounter (std::chrono::seconds window_size, std::size_t buckets_per_second=DEFAULT_BUCKETS_PER_SECOND) | |
| Constructs a sliding window counter. | |
| SlidingWindowCounter (const SlidingWindowCounter &other) | |
| Copy constructor. | |
| SlidingWindowCounter (SlidingWindowCounter &&other) noexcept | |
| Move constructor. | |
| SlidingWindowCounter & | operator= (const SlidingWindowCounter &other) |
| Copy assignment operator. | |
| SlidingWindowCounter & | operator= (SlidingWindowCounter &&other) noexcept |
| Move assignment operator. | |
| ~SlidingWindowCounter ()=default | |
| Destructor. | |
| void | increment (std::size_t count=1) |
| Increment the counter. | |
| double | rate_per_second () const |
| Get the current rate per second. | |
| std::uint64_t | total_in_window () const |
| Get the total count within the current window. | |
| std::uint64_t | all_time_total () const |
| Get the all-time total count. | |
| void | reset () |
| Reset the counter. | |
| std::chrono::seconds | window_size () const |
| Get the window size. | |
| std::size_t | bucket_count () const |
| Get the number of buckets. | |
Static Public Attributes | |
| static constexpr std::size_t | DEFAULT_BUCKETS_PER_SECOND = 10 |
| Default number of buckets per second. | |
Private Member Functions | |
| std::size_t | current_bucket_index () const |
| Get the current bucket index based on current time. | |
| std::size_t | bucket_index_for_time (std::uint64_t timestamp_ms) const |
| Get the bucket index for a specific timestamp. | |
| bool | is_bucket_valid (std::uint64_t bucket_timestamp_ms, std::uint64_t current_ms) const |
| Check if a bucket is within the current window. | |
| void | advance_bucket (std::size_t bucket_index, std::uint64_t current_ms) |
| Advance bucket to current time period if needed. | |
Static Private Member Functions | |
| static std::uint64_t | current_time_ms () |
| Get current time in milliseconds since epoch. | |
Private Attributes | |
| std::chrono::seconds | window_size_ |
| The sliding window duration. | |
| std::chrono::milliseconds | bucket_duration_ |
| Duration of each bucket. | |
| std::vector< Bucket > | buckets_ |
| Circular buffer of time buckets. | |
| std::atomic< std::uint64_t > | all_time_total_ {0} |
| All-time total count. | |
Sliding window counter for throughput measurement.
This class provides a lock-free, thread-safe counter that tracks events within a sliding time window. It uses a circular buffer of time buckets to maintain accurate rate calculations.
The window is divided into multiple buckets (default: 10 per second). Each bucket covers a fixed time interval. As time advances, old buckets are automatically invalidated and reused for new time periods.
Example for a 1-second window with 10 buckets:
Definition at line 51 of file sliding_window_counter.h.
|
explicit |
Constructs a sliding window counter.
| window_size | The duration of the sliding window. |
| buckets_per_second | Number of buckets per second (default: 10). |
Higher buckets_per_second provides more precision but uses more memory. For a 60-second window with 10 buckets/second, uses 600 * 16 bytes = ~10KB.
Definition at line 11 of file sliding_window_counter.cpp.
References buckets_.
| kcenon::thread::metrics::SlidingWindowCounter::SlidingWindowCounter | ( | const SlidingWindowCounter & | other | ) |
Copy constructor.
| other | The counter to copy from. |
Definition at line 24 of file sliding_window_counter.cpp.
References all_time_total_, and buckets_.
|
noexcept |
Move constructor.
| other | The counter to move from. |
Definition at line 41 of file sliding_window_counter.cpp.
|
default |
Destructor.
|
private |
Advance bucket to current time period if needed.
| bucket_index | The bucket to advance. |
| current_ms | The current timestamp. |
Definition at line 174 of file sliding_window_counter.cpp.
References bucket_duration_, and buckets_.
Referenced by increment().

|
nodiscard |
Get the all-time total count.
Definition at line 123 of file sliding_window_counter.cpp.
References all_time_total_.
|
nodiscard |
Get the number of buckets.
Definition at line 139 of file sliding_window_counter.cpp.
References buckets_.
|
nodiscardprivate |
Get the bucket index for a specific timestamp.
| timestamp_ms | Timestamp in milliseconds since epoch. |
Definition at line 147 of file sliding_window_counter.cpp.
References bucket_duration_, and buckets_.
Referenced by current_bucket_index(), and increment().

|
nodiscardprivate |
Get the current bucket index based on current time.
Definition at line 143 of file sliding_window_counter.cpp.
References bucket_index_for_time(), and current_time_ms().

|
staticnodiscardprivate |
Get current time in milliseconds since epoch.
Definition at line 155 of file sliding_window_counter.cpp.
Referenced by current_bucket_index(), increment(), and total_in_window().

| void kcenon::thread::metrics::SlidingWindowCounter::increment | ( | std::size_t | count = 1 | ) |
Increment the counter.
| count | The value to add (default: 1). |
This operation is lock-free and thread-safe. Complexity: O(1).
Definition at line 89 of file sliding_window_counter.cpp.
References advance_bucket(), all_time_total_, bucket_index_for_time(), buckets_, and current_time_ms().
Referenced by kcenon::thread::metrics::EnhancedThreadPoolMetrics::record_execution().


|
nodiscardprivate |
Check if a bucket is within the current window.
| bucket_timestamp_ms | The bucket's timestamp. |
| current_ms | The current timestamp. |
Definition at line 162 of file sliding_window_counter.cpp.
References window_size_.
Referenced by total_in_window().

| SlidingWindowCounter & kcenon::thread::metrics::SlidingWindowCounter::operator= | ( | const SlidingWindowCounter & | other | ) |
Copy assignment operator.
| other | The counter to copy from. |
Definition at line 50 of file sliding_window_counter.cpp.
References all_time_total_, bucket_duration_, buckets_, and window_size_.
|
noexcept |
Move assignment operator.
| other | The counter to move from. |
Definition at line 76 of file sliding_window_counter.cpp.
|
nodiscard |
Get the current rate per second.
This calculates the total count in the window divided by window duration.
Definition at line 103 of file sliding_window_counter.cpp.
References total_in_window(), and window_size_.
Referenced by kcenon::thread::metrics::EnhancedThreadPoolMetrics::snapshot().


| void kcenon::thread::metrics::SlidingWindowCounter::reset | ( | ) |
Reset the counter.
Clears all buckets and resets the all-time total.
Definition at line 127 of file sliding_window_counter.cpp.
References all_time_total_, and buckets_.
Referenced by kcenon::thread::metrics::EnhancedThreadPoolMetrics::reset().

|
nodiscard |
Get the total count within the current window.
Definition at line 109 of file sliding_window_counter.cpp.
References buckets_, current_time_ms(), and is_bucket_valid().
Referenced by rate_per_second().


|
nodiscard |
Get the window size.
Definition at line 135 of file sliding_window_counter.cpp.
References window_size_.
|
private |
All-time total count.
Definition at line 209 of file sliding_window_counter.h.
Referenced by all_time_total(), increment(), operator=(), reset(), and SlidingWindowCounter().
|
private |
Duration of each bucket.
Definition at line 199 of file sliding_window_counter.h.
Referenced by advance_bucket(), bucket_index_for_time(), and operator=().
|
private |
Circular buffer of time buckets.
Definition at line 204 of file sliding_window_counter.h.
Referenced by advance_bucket(), bucket_count(), bucket_index_for_time(), increment(), operator=(), reset(), SlidingWindowCounter(), SlidingWindowCounter(), and total_in_window().
|
staticconstexpr |
Default number of buckets per second.
Definition at line 56 of file sliding_window_counter.h.
|
private |
The sliding window duration.
Definition at line 194 of file sliding_window_counter.h.
Referenced by is_bucket_valid(), operator=(), rate_per_second(), and window_size().