6 #include <gtest/gtest.h> 12 #define RATE_TEST_TOL_LOCAL 0.001 13 #define RATE_TEST_TOL_BUILD_SERVER 0.007 16 #define RATE_TEST_TOL RATE_TEST_TOL_BUILD_SERVER 24 std::this_thread::sleep_for(std::chrono::nanoseconds(static_cast<int64_t>(1e9 * duration)));
27 TEST(RateTest, Initialization)
29 const std::string name =
"Test";
30 const double time_step = 0.1;
49 const double time_step = 0.1;
50 const double processing_time = 0.05;
66 TEST(RateTest, SleepWithEnforceRate)
68 const double time_step = 0.1;
74 const double processing_time = 0.05;
75 std::vector<double> processing_times;
76 std::vector<double> summed_step_times;
79 clock_gettime(CLOCK_MONOTONIC, &
start);
82 clock_gettime(CLOCK_MONOTONIC, &end);
86 clock_gettime(CLOCK_MONOTONIC, &
start);
90 clock_gettime(CLOCK_MONOTONIC, &end);
94 processing_times = { 0.02, 0.02, 0.15, 0.02, 0.02 };
95 summed_step_times = { 0.0, 0.1, 0.2, 0.35, 0.4, 0.5 };
97 clock_gettime(CLOCK_MONOTONIC, &
start);
98 for (
unsigned int i = 0; i < processing_times.size(); i++)
100 clock_gettime(CLOCK_MONOTONIC, &end);
105 clock_gettime(CLOCK_MONOTONIC, &end);
110 processing_times = { 0.02, 0.02, 0.19, 0.02, 0.02, 0.02 };
111 summed_step_times = { 0.0, 0.1, 0.2, 0.39, 0.41, 0.5, 0.6 };
113 clock_gettime(CLOCK_MONOTONIC, &
start);
114 for (
unsigned int i = 0; i < processing_times.size(); i++)
116 clock_gettime(CLOCK_MONOTONIC, &end);
121 clock_gettime(CLOCK_MONOTONIC, &end);
126 processing_times = { 0.02, 0.02, 0.12, 0.12, 0.02, 0.02 };
127 summed_step_times = { 0.0, 0.1, 0.2, 0.32, 0.44, 0.5, 0.6 };
129 clock_gettime(CLOCK_MONOTONIC, &
start);
130 for (
unsigned int i = 0; i < processing_times.size(); i++)
132 clock_gettime(CLOCK_MONOTONIC, &end);
137 clock_gettime(CLOCK_MONOTONIC, &end);
142 processing_times = { 0.02, 0.02, 0.12, 0.12, 0.08, 0.02, 0.02 };
143 summed_step_times = { 0.0, 0.1, 0.2, 0.32, 0.44, 0.52, 0.6, 0.7 };
145 clock_gettime(CLOCK_MONOTONIC, &
start);
146 for (
unsigned int i = 0; i < processing_times.size(); i++)
148 clock_gettime(CLOCK_MONOTONIC, &end);
153 clock_gettime(CLOCK_MONOTONIC, &end);
158 TEST(RateTest, SleepWithoutEnforceRate)
160 const double time_step = 0.1;
166 const double processing_time = 0.05;
167 std::vector<double> processing_times;
168 std::vector<double> summed_step_times;
171 clock_gettime(CLOCK_MONOTONIC, &
start);
174 clock_gettime(CLOCK_MONOTONIC, &end);
178 clock_gettime(CLOCK_MONOTONIC, &
start);
182 clock_gettime(CLOCK_MONOTONIC, &end);
186 processing_times = { 0.02, 0.02, 0.15, 0.02, 0.02 };
187 summed_step_times = { 0.0, 0.1, 0.2, 0.35, 0.45, 0.55 };
189 clock_gettime(CLOCK_MONOTONIC, &
start);
190 for (
unsigned int i = 0; i < processing_times.size(); i++)
192 clock_gettime(CLOCK_MONOTONIC, &end);
197 clock_gettime(CLOCK_MONOTONIC, &end);
202 processing_times = { 0.02, 0.02, 0.12, 0.12, 0.02, 0.02 };
203 summed_step_times = { 0.0, 0.1, 0.2, 0.32, 0.44, 0.54, 0.64 };
205 clock_gettime(CLOCK_MONOTONIC, &
start);
206 for (
unsigned int i = 0; i < processing_times.size(); i++)
208 clock_gettime(CLOCK_MONOTONIC, &end);
213 clock_gettime(CLOCK_MONOTONIC, &end);
218 TEST(RateTest, WarningsAndErrors)
220 const double time_step = 0.1;
236 TEST(RateTest, StatisticsWithEnforceRate)
238 const double time_step = 0.1;
243 const double processing_time = 0.05;
254 const unsigned int num_time_steps = 10;
256 for (
unsigned int i = 0; i < num_time_steps; i++)
269 const std::vector<double> processing_times = { 0.04, 0.02, 0.04, 0.07, 0.05, 0.05, 0.04, 0.09, 0.05 };
271 for (
double processing_time : processing_times)
285 for (
double processing_time : processing_times)
297 TEST(RateTest, StatisticsWithoutEnforceRate)
299 const double time_step = 0.1;
304 const double processing_time = 0.05;
315 const unsigned int num_time_steps = 10;
317 for (
unsigned int i = 0; i < num_time_steps; i++)
330 const std::vector<double> processing_times = { 0.04, 0.02, 0.04, 0.07, 0.05, 0.05, 0.04, 0.09, 0.05 };
332 for (
double processing_time : processing_times)
346 for (
double processing_time : processing_times)
double getAwakeTimeMean() const
double getAwakeTime() const
TEST(RateTest, Initialization)
unsigned int getNumWarnings() const
unsigned int getNumTimeSteps() const
double getAwakeTimeStdDev() const
std::atomic< double > maxTimeStepFactorError_
If the awake time is bigger than the time step multiplied by this factor, it counts as an error...
void doSomething(const double duration)
std::atomic< double > timeStep_
Time step in seconds.
RateOptions & getOptions()
std::atomic< bool > enforceRate_
Boolean indicating whether the rate should be enforced.
std::string name_
Name for printing.
unsigned int getNumErrors() const
std::atomic< clockid_t > clockId_
Linux clock ID.
static double getDuration(const timespec &start, const timespec &end)
std::atomic< double > maxTimeStepFactorWarning_
If the awake time is bigger than the time step multiplied by this factor, it counts as an warning...