141 [[nodiscard]] std::chrono::seconds
window_size()
const;
157 std::atomic<std::uint64_t>
count{0};
167 :
count(other.count.load(std::memory_order_relaxed)),
168 timestamp_ms(other.timestamp_ms.load(std::memory_order_relaxed)) {}
171 if (
this != &other) {
172 count.store(other.
count.load(std::memory_order_relaxed),
173 std::memory_order_relaxed);
175 std::memory_order_relaxed);
181 if (
this != &other) {
182 count.store(other.count.load(std::memory_order_relaxed),
183 std::memory_order_relaxed);
184 timestamp_ms.store(other.timestamp_ms.load(std::memory_order_relaxed),
185 std::memory_order_relaxed);
237 std::uint64_t bucket_timestamp_ms,
238 std::uint64_t current_ms)
const;
245 void advance_bucket(std::size_t bucket_index, std::uint64_t current_ms);
Sliding window counter for throughput measurement.
SlidingWindowCounter & operator=(const SlidingWindowCounter &other)
Copy assignment operator.
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.
std::size_t bucket_count() const
Get the number of buckets.
void reset()
Reset the counter.
std::chrono::seconds window_size_
The sliding window duration.
std::size_t current_bucket_index() const
Get the current bucket index based on current time.
std::chrono::seconds window_size() const
Get the window size.
void increment(std::size_t count=1)
Increment the counter.
SlidingWindowCounter(std::chrono::seconds window_size, std::size_t buckets_per_second=DEFAULT_BUCKETS_PER_SECOND)
Constructs a sliding window counter.
std::uint64_t all_time_total() const
Get the all-time total count.
std::uint64_t total_in_window() const
Get the total count within the current window.
std::vector< Bucket > buckets_
Circular buffer of time buckets.
std::size_t bucket_index_for_time(std::uint64_t timestamp_ms) const
Get the bucket index for a specific timestamp.
double rate_per_second() const
Get the current rate per second.
std::atomic< std::uint64_t > all_time_total_
All-time total count.
~SlidingWindowCounter()=default
Destructor.
std::chrono::milliseconds bucket_duration_
Duration of each bucket.
void advance_bucket(std::size_t bucket_index, std::uint64_t current_ms)
Advance bucket to current time period if needed.
static constexpr std::size_t DEFAULT_BUCKETS_PER_SECOND
Default number of buckets per second.
static std::uint64_t current_time_ms()
Get current time in milliseconds since epoch.
std::atomic< std::uint64_t > timestamp_ms
Bucket & operator=(const Bucket &other)
Bucket & operator=(Bucket &&other) noexcept
Bucket(Bucket &&other) noexcept
Bucket(const Bucket &other)
std::atomic< std::uint64_t > count