micro-test-plus 6.0.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
stopwatch.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2021-2026 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can be
9 * obtained from https://opensource.org/licenses/mit.
10 */
11
12// ----------------------------------------------------------------------------
13
31
32// ----------------------------------------------------------------------------
33
35
36// ----------------------------------------------------------------------------
37
38#if defined(__GNUC__)
39#pragma GCC diagnostic ignored "-Waggregate-return"
40#if defined(__clang__)
41#pragma clang diagnostic ignored "-Wpre-c++17-compat"
42#pragma clang diagnostic ignored "-Wc++98-compat"
43#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
44#endif // defined(__clang__)
45#endif // defined(__GNUC__)
46
47// ============================================================================
48
49#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_ENABLED)
50
51// ----------------------------------------------------------------------------
52
54{
55 // --------------------------------------------------------------------------
56
65 void
66 read_clock (timespec& ts) noexcept
67 {
68#if defined(_WIN32)
69 timespec_get (&ts, TIME_UTC);
70#elif defined(CLOCK_MONOTONIC)
71 clock_gettime (CLOCK_MONOTONIC, &ts);
72#else
73 (void)ts;
74#endif // defined(_WIN32) || defined(CLOCK_MONOTONIC)
75 }
76
77 // --------------------------------------------------------------------------
78
86 void
87 stopwatch::start (void) noexcept
88 {
89 start_time_ = {};
90 stop_time_ = {};
92 }
93
100 void
101 stopwatch::stop (void) noexcept
102 {
103 stop_time_ = {};
105 }
106
115 std::chrono::microseconds
116 stopwatch::elapsed (void) const noexcept
117 {
118 if (!has_elapsed ())
119 {
120 return std::chrono::microseconds::zero ();
121 }
122
123 const std::chrono::nanoseconds delta = std::chrono::seconds{
124 stop_time_.tv_sec - start_time_.tv_sec
125 } + std::chrono::nanoseconds{ stop_time_.tv_nsec - start_time_.tv_nsec };
126
127 return std::chrono::duration_cast<std::chrono::microseconds> (delta);
128 }
129
130#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_DEPRECATED_ENABLED)
137 // Deprecated since 6.0.0; use start().
138 void
140 {
142 {
144 }
145 }
146
153 // Deprecated since 6.0.0; use stop().
154 void
156 {
157 if (!is_recorded (stop_time_))
158 {
160 }
161 }
162#endif // defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_DEPRECATED_ENABLED)
163
164 // --------------------------------------------------------------------------
165} // namespace micro_os_plus::micro_test_plus::detail
166
167// ----------------------------------------------------------------------------
168
169#endif // defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_ENABLED)
170
171// ----------------------------------------------------------------------------
static bool is_recorded(const timespec &ts) noexcept
Checks whether a clock reading was recorded.
std::chrono::microseconds elapsed(void) const noexcept
Returns the time elapsed between start() and stop().
bool has_elapsed(void) const noexcept
Checks whether the elapsed time is available.
void timestamp_end(void) noexcept
Deprecated recording of the stop time, from the system clock.
timespec start_time_
The clock reading at the start of the measured interval.
Definition stopwatch.h:430
void timestamp_begin(void) noexcept
Deprecated recording of the start time, from the system clock.
void stop(void) noexcept
Records the stop time, from the system clock.
void start(void) noexcept
Records the start time, from the system clock.
Definition stopwatch.cpp:87
timespec stop_time_
The clock reading at the end of the measured interval.
Definition stopwatch.h:439
Internal implementation details for the µTest++ framework.
void read_clock(timespec &ts) noexcept
Reads the system clock.
Definition stopwatch.cpp:66
C++ header file with declarations for the µTest++ stopwatch.