Mads
Multi-Agent Distributed System
Loading...
Searching...
No Matches
execution_time_window_stats.hpp
Go to the documentation of this file.
1/*
2 _____ _ _
3 | ____|_ _____ ___ _ _| |_(_) ___ _ __ ___
4 | _| \ \/ / _ \/ __| | | | __| |/ _ \| '_ \/ __|
5 | |___ > < __/ (__| |_| | |_| | (_) | | | \__ \
6 |_____/_/\_\___|\___|\__,_|\__|_|\___/|_| |_|___/
7
8Running window statistics for execution times.
9*/
10
11#ifndef EXECUTION_TIME_WINDOW_STATS_HPP
12#define EXECUTION_TIME_WINDOW_STATS_HPP
13
14#include <algorithm>
15#include <chrono>
16#include <cmath>
17#include <cstddef>
18#include <deque>
19
20namespace Mads {
21
39public:
45 explicit ExecutionTimeWindowStats(std::size_t window_width = 100)
46 : _window_width(window_width) {}
47
54 void tic() {
55 _start = clock_t::now();
56 _ticking = true;
57 }
58
67 double toc() {
68 const auto stop = clock_t::now();
69 if (!_ticking) {
70 return 0.0;
71 }
72 _ticking = false;
73 const double elapsed_ms =
74 std::chrono::duration<double, std::milli>(stop - _start).count();
75 append_duration(elapsed_ms);
76 return elapsed_ms;
77 }
78
87 void set_window_width(std::size_t window_width) {
88 _window_width = window_width;
89 trim_to_window();
90 }
91
97 std::size_t window_width() const { return _window_width; }
98
104 std::size_t size() const { return _samples.size(); }
105
111 double average_ms() const {
112 if (_samples.empty()) {
113 return 0.0;
114 }
115 return _sum / static_cast<double>(_samples.size());
116 }
117
128 double stddev_ms() const {
129 if (_samples.empty()) {
130 return 0.0;
131 }
132 const double n = static_cast<double>(_samples.size());
133 const double mean = _sum / n;
134 const double variance = std::max(0.0, (_sum_sq / n) - (mean * mean));
135 return std::sqrt(variance);
136 }
137
141 void clear() {
142 _samples.clear();
143 _sum = 0.0;
144 _sum_sq = 0.0;
145 _ticking = false;
146 }
147
148private:
149 using clock_t = std::chrono::steady_clock;
150
156 void append_duration(double duration_ms) {
157 if (_window_width == 0) {
158 return;
159 }
160 _samples.push_back(duration_ms);
161 _sum += duration_ms;
162 _sum_sq += duration_ms * duration_ms;
163 trim_to_window();
164 }
165
169 void trim_to_window() {
170 while (_samples.size() > _window_width) {
171 const double oldest = _samples.front();
172 _samples.pop_front();
173 _sum -= oldest;
174 _sum_sq -= oldest * oldest;
175 }
176 if (_samples.empty()) {
177 _sum = 0.0;
178 _sum_sq = 0.0;
179 }
180 }
181
182 std::size_t _window_width = 0;
183 std::deque<double> _samples;
184 double _sum = 0.0;
185 double _sum_sq = 0.0;
186 clock_t::time_point _start{};
187 bool _ticking = false;
188};
189
190} // namespace Mads
191
192#endif // EXECUTION_TIME_WINDOW_STATS_HPP
Running window statistics for execution time samples.
double toc()
Mark the stop time, compute elapsed time, and append it.
void clear()
Clear all stored samples and reset timing state.
double stddev_ms() const
Get the population standard deviation of stored samples.
ExecutionTimeWindowStats(std::size_t window_width=100)
Construct a statistics window with a maximum number of samples.
std::size_t size() const
Get the number of currently stored samples.
std::size_t window_width() const
Get the current configured window width.
double average_ms() const
Get the arithmetic mean of stored samples.
void set_window_width(std::size_t window_width)
Set the running window width at runtime.
void tic()
Mark the start time of the next measured interval.
Definition agent.hpp:67