GCC Code Coverage Report


Directory: ./
File: include/real_time_tools/timer.hpp
Date: 2022-06-29 13:58:11
Exec Total Coverage
Lines: 20 20 100.0%
Branches: 2 4 50.0%

Line Branch Exec Source
1 /**
2 * @file timer.hpp
3 * @author Maximilien Naveau (maximilien.naveau@gmail.com)
4 * license License BSD-3-Clause
5 * @copyright Copyright (c) 2019, New York University and Max Planck
6 * Gesellschaft.
7 * @date 2019-05-22
8 *
9 * @brief Some tools to measure (ellapsed) time.
10 */
11
12 #ifndef TIMER_HPP
13 #define TIMER_HPP
14
15 #include <unistd.h>
16 #include <chrono>
17 #include <cmath>
18 #include <deque>
19 #include <string>
20
21 #include "real_time_tools/iostream.hpp"
22
23 namespace real_time_tools
24 {
25 /**
26 * @brief The timer class is a simple time measurement class that measure
27 * between tic and tac and with a memory buffer of a certain size.
28 */
29 1006 class Timer
30 {
31 public:
32 /**
33 * @brief timer constructor
34 */
35 Timer();
36
37 /**
38 * @brief tic measures the time when it is called. This is to be used with
39 * the tac method that will return the time elapsed between tic and tac.
40 */
41 void tic();
42
43 /**
44 * @brief tac is to be used after tic has been called.
45 * @return the duration in seconds between the call of tic() and the call of
46 * tac(). if tic() has not been called previously this will return nan
47 */
48 double tac();
49
50 /**
51 * @brief this is like a tac() followed by a tic(), making sure the
52 * previous tac_time becomes the tic_time
53 */
54 double tac_tic();
55
56 /**
57 * @brief Save the time interval measured
58 *
59 * @param time_interval
60 */
61 void log_time_interval(double time_interval);
62
63 /**
64 * IOSTREAM functions
65 */
66
67 /**
68 * @brief dump_tic_tac_measurements writes in a file the time elapsed
69 * between every tick
70 * @param file_name is the path to the file.
71 */
72 void dump_measurements(std::string file_name) const;
73
74 /**
75 * @brief print_statistics display in real time the statistics of the time
76 * measurements acquiered so far.
77 */
78 void print_statistics() const;
79
80 /**
81 * SETTERS
82 */
83
84 /**
85 * @brief set_memory_size sets the buffer size. It resets all value of the
86 * buffer to zero.
87 * !! WARNING non real time method. !!
88 * @param memory_buffer_size is the size use to reset the size of the
89 */
90 2008 void set_memory_size(const unsigned memory_buffer_size)
91 {
92 2008 count_ = 0;
93 2008 memory_buffer_size_ = memory_buffer_size;
94
1/2
✓ Branch 1 taken 2008 times.
✗ Branch 2 not taken.
2008 time_measurement_buffer_.resize(memory_buffer_size_, 0.0);
95 2008 }
96
97 /**
98 * @brief set_name modify the name of the object for display purposes.
99 * @param name is the new name of the object.
100 */
101 void set_name(std::string name)
102 {
103 name_ = name;
104 }
105
106 /**
107 * GETTERS
108 */
109
110 /**
111 * @brief get_min_elapsed_sec
112 * @return a copy of the minimum elapsed times
113 */
114 3 double get_min_elapsed_sec() const
115 {
116 3 return min_elapsed_time_;
117 }
118
119 /**
120 * @brief get_max_elapsed_sec
121 * @return a copy of the maximum elapsed times
122 */
123 3 double get_max_elapsed_sec() const
124 {
125 3 return max_elapsed_time_;
126 }
127
128 /**
129 * @brief get_avg_elapsed_sec
130 * @return a copy of the average elapsed time
131 */
132 3 double get_avg_elapsed_sec() const
133 {
134 3 return avg_elapsed_time_;
135 }
136
137 /**
138 * @brief get_std_dev_elapsed_sec
139 * @return a copy of the standard deviation of the elapsed times
140 */
141 3 double get_std_dev_elapsed_sec() const
142 {
143 6 return std::sqrt(second_moment_elapsed_time_ -
144 6 avg_elapsed_time_ * avg_elapsed_time_);
145 }
146
147 protected:
148 /**
149 * @brief tic_time_ time at which tic() was called
150 */
151 double tic_time_;
152
153 /**
154 * @brief time_measurement_buffer_ this is a chained list of double
155 */
156 std::deque<double> time_measurement_buffer_;
157
158 /**
159 * @brief count_time_buffer_ is a counter that manages the
160 * time_measurement_buffer_ fill in.
161 */
162 long unsigned count_;
163
164 /**
165 * @brief memory_buffer_size_ is the max size of the memory buffer.
166 */
167 unsigned memory_buffer_size_;
168
169 /**
170 * @brief min_elapsed_time_ is the minimum measured elapsed time
171 */
172 double min_elapsed_time_;
173
174 /**
175 * @brief max_elapsed_time_ is the maximum measured elapsed time
176 */
177 double max_elapsed_time_;
178
179 /**
180 * @brief avg_elapsed_time_ is the average measured elapsed time
181 */
182 double avg_elapsed_time_;
183
184 /**
185 * @brief avg_elapsed_time_ is the second moment measured elapsed
186 * time
187 */
188 double second_moment_elapsed_time_;
189
190 /**
191 * @brief name_ of the timer object
192 */
193 std::string name_;
194
195 /**
196 * Some utilities
197 */
198 public:
199 /**
200 * @brief get_current_time_sec gives the current time in double and in
201 * seconds
202 * @return
203 */
204 static double get_current_time_sec();
205
206 /**
207 * @brief get_current_time_ms gives the current time in double and in
208 * milli seconds
209 * @return
210 */
211 1199 static double get_current_time_ms()
212 {
213 1199 return 1e3 * get_current_time_sec();
214 }
215
216 /**
217 * @brief puts the current thread to sleep for the duration
218 * of "sleep_duration_us" micro-seconds.
219 * @param sleep_time_us is the sleeping duration asked in micro-seconds.
220 * @return 0 on success, error code otherwise
221 */
222 static int sleep_microseconds(int sleep_duration_us);
223
224 /**
225 * @brief sleep_sec puts the current thread to sleep for the duration
226 * of "sleep_time_sec" seconds.
227 * @param sleep_time_sec is the sleeping duration asked in seconds.
228 */
229 static void sleep_sec(const double& sleep_time_sec);
230
231 /**
232 * @brief sleep_ms puts the current thread to sleep for the duration
233 * of "sleep_time_sec" seconds.
234 * @param sleep_time_ms is the sleeping duration asked in seconds.
235 */
236 399 static void sleep_ms(const double& sleep_time_ms)
237 {
238
1/2
✓ Branch 1 taken 399 times.
✗ Branch 2 not taken.
399 sleep_sec(1e-3 * sleep_time_ms);
239 399 }
240
241 /**
242 * @brief sleep_until_sec puts the threads to sleep until the date
243 * "date_sec" is reached.
244 * @param date_sec is the date until when to sleep in seconds.
245 */
246 static void sleep_until_sec(const double& date_sec);
247
248 #ifndef MAC_OS
249 /**
250 * @brief timespec_add_sec posix type of a date in time.
251 * @param date_spec is the date to be changed
252 * @param duration_sec the duration to be added to "t" in seconds
253 */
254 static void timespec_add_sec(struct timespec& date_spec,
255 const double duration_sec);
256
257 /**
258 * @brief sec_to_timespec converts a double representing the time in seconds
259 * to a struct timespec.
260 * @param[in] date_sec is the time in sec to be converted.
261 * @param[out] date_spec is the converted structure.
262 */
263 static void sec_to_timespec(double date_sec, struct timespec& date_spec);
264
265 #endif // MAC_OS
266
267 /**
268 * @brief get_current_date_str get the current date and format it in a
269 * string with "year_month_day_hour_minute_sec"
270 */
271 static std::string get_current_date_str();
272 };
273
274 } // namespace real_time_tools
275
276 #endif // TIMER_HPP
277