micro-test-plus
6.0.0
µTest++ Testing Framework
Toggle main menu visibility
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
34
#include "
micro-os-plus/micro-test-plus/stopwatch.h
"
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
53
namespace
micro_os_plus::micro_test_plus::detail
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_
= {};
91
read_clock
(
start_time_
);
92
}
93
100
void
101
stopwatch::stop
(
void
)
noexcept
102
{
103
stop_time_
= {};
104
read_clock
(
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
139
stopwatch::timestamp_begin
(
void
)
noexcept
140
{
141
if
(!
is_recorded
(
start_time_
))
142
{
143
read_clock
(
start_time_
);
144
}
145
}
146
153
// Deprecated since 6.0.0; use stop().
154
void
155
stopwatch::timestamp_end
(
void
)
noexcept
156
{
157
if
(!
is_recorded
(
stop_time_
))
158
{
159
read_clock
(
stop_time_
);
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
// ----------------------------------------------------------------------------
micro_os_plus::micro_test_plus::detail::stopwatch::is_recorded
static bool is_recorded(const timespec &ts) noexcept
Checks whether a clock reading was recorded.
Definition
stopwatch-inlines.h:75
micro_os_plus::micro_test_plus::detail::stopwatch::elapsed
std::chrono::microseconds elapsed(void) const noexcept
Returns the time elapsed between start() and stop().
Definition
stopwatch.cpp:116
micro_os_plus::micro_test_plus::detail::stopwatch::has_elapsed
bool has_elapsed(void) const noexcept
Checks whether the elapsed time is available.
Definition
stopwatch-inlines.h:64
micro_os_plus::micro_test_plus::detail::stopwatch::timestamp_end
void timestamp_end(void) noexcept
Deprecated recording of the stop time, from the system clock.
Definition
stopwatch.cpp:155
micro_os_plus::micro_test_plus::detail::stopwatch::start_time_
timespec start_time_
The clock reading at the start of the measured interval.
Definition
stopwatch.h:430
micro_os_plus::micro_test_plus::detail::stopwatch::timestamp_begin
void timestamp_begin(void) noexcept
Deprecated recording of the start time, from the system clock.
Definition
stopwatch.cpp:139
micro_os_plus::micro_test_plus::detail::stopwatch::stop
void stop(void) noexcept
Records the stop time, from the system clock.
Definition
stopwatch.cpp:101
micro_os_plus::micro_test_plus::detail::stopwatch::start
void start(void) noexcept
Records the start time, from the system clock.
Definition
stopwatch.cpp:87
micro_os_plus::micro_test_plus::detail::stopwatch::stop_time_
timespec stop_time_
The clock reading at the end of the measured interval.
Definition
stopwatch.h:439
micro_os_plus::micro_test_plus::detail
Internal implementation details for the µTest++ framework.
Definition
check-result.h:82
micro_os_plus::micro_test_plus::detail::read_clock
void read_clock(timespec &ts) noexcept
Reads the system clock.
Definition
stopwatch.cpp:66
stopwatch.h
C++ header file with declarations for the µTest++ stopwatch.
src
stopwatch.cpp
Generated by
1.18.0