micro-test-plus
4.1.0
µTest++ Testing Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
timings.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
* Major parts of the code are inspired from v1.1.8 of the Boost UT project,
12
* released under the terms of the Boost Version 1.0 Software License,
13
* which can be obtained from https://www.boost.org/LICENSE_1_0.txt.
14
*/
15
16
// ----------------------------------------------------------------------------
17
35
36
// ----------------------------------------------------------------------------
37
38
#include <cassert>
39
40
#if defined(MICRO_OS_PLUS_INCLUDE_CONFIG_H)
41
#include <micro-os-plus/config.h>
42
#endif
// MICRO_OS_PLUS_INCLUDE_CONFIG_H
43
44
#include "
micro-os-plus/micro-test-plus/timings.h
"
45
46
// ----------------------------------------------------------------------------
47
48
#if defined(__GNUC__)
49
#pragma GCC diagnostic ignored "-Waggregate-return"
50
#if defined(__clang__)
51
#pragma clang diagnostic ignored "-Wpre-c++17-compat"
52
#pragma clang diagnostic ignored "-Wc++98-compat"
53
#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
54
#endif
55
#endif
56
57
// ============================================================================
58
59
namespace
micro_os_plus::micro_test_plus::detail
60
{
61
// --------------------------------------------------------------------------
62
71
timestamp::timestamp
() noexcept
72
{
73
#if defined(_WIN32)
74
timespec_get (&
value_
, TIME_UTC);
75
#elif defined(CLOCK_MONOTONIC)
76
clock_gettime (CLOCK_MONOTONIC, &
value_
);
77
#endif
78
}
79
86
bool
87
timestamp::has_clock
(
void
)
const
noexcept
88
{
89
return
value_
.tv_sec != 0 ||
value_
.tv_nsec != 0;
90
}
91
92
// --------------------------------------------------------------------------
93
101
void
102
timestamps::timestamp_begin
(
void
)
noexcept
103
{
104
// Ensure it is timestamped only once.
105
if
(!
begin_time_
.has_value ())
106
{
107
begin_time_
.emplace ();
108
}
109
}
110
117
void
118
timestamps::timestamp_begin
(
const
timespec& ts)
noexcept
119
{
120
// Ensure it is timestamped only once.
121
if
(!
begin_time_
.has_value ())
122
{
123
begin_time_
.emplace (ts);
124
}
125
}
126
134
void
135
timestamps::timestamp_end
(
void
)
noexcept
136
{
137
// Ensure it is timestamped only once.
138
if
(!
end_time_
.has_value ())
139
{
140
end_time_
.emplace ();
141
}
142
}
143
150
void
151
timestamps::timestamp_end
(
const
timespec& ts)
noexcept
152
{
153
// Ensure it is timestamped only once.
154
if
(!
end_time_
.has_value ())
155
{
156
end_time_
.emplace (ts);
157
}
158
}
159
166
bool
167
timestamps::has_timestamps
(
void
)
const
noexcept
168
{
169
return
begin_time_
.has_value () &&
begin_time_
->has_clock ()
170
&&
end_time_
.has_value () &&
end_time_
->has_clock ();
171
}
172
183
void
184
timestamps::compute_elapsed_time
(uint32_t& milliseconds,
185
uint32_t& microseconds)
const
186
{
187
assert (
has_timestamps
());
188
189
// Precondition: has_timestamps() must be true before calling this method.
190
// Invoking it with disengaged optionals is undefined behaviour.
191
long
long
delta_ns
192
=
end_time_
->value ().tv_nsec -
begin_time_
->value ().tv_nsec;
193
long
long
delta_s
194
=
end_time_
->value ().tv_sec -
begin_time_
->value ().tv_sec;
195
if
(delta_ns < 0)
196
{
197
delta_ns += 1000000000LL;
198
--delta_s;
199
}
200
201
// Split into milliseconds and microseconds.
202
const
long
long
total_us = delta_s * 1000000LL + delta_ns / 1000LL;
203
milliseconds =
static_cast<
uint32_t
>
(total_us / 1000LL);
204
microseconds =
static_cast<
uint32_t
>
(total_us % 1000LL);
205
}
206
207
// --------------------------------------------------------------------------
208
}
// namespace micro_os_plus::micro_test_plus::detail
209
210
// ----------------------------------------------------------------------------
micro_os_plus::micro_test_plus::detail::timestamp::timestamp
timestamp() noexcept
Default constructor. Zero-initialises the internal timespec.
Definition
timings.cpp:71
micro_os_plus::micro_test_plus::detail::timestamp::value_
timespec value_
The underlying timespec value.
Definition
timings.h:176
micro_os_plus::micro_test_plus::detail::timestamp::has_clock
bool has_clock(void) const noexcept
Returns true if a monotonic clock is available on this target.
Definition
timings.cpp:87
micro_os_plus::micro_test_plus::detail::timestamps::has_timestamps
bool has_timestamps(void) const noexcept
Returns true if both begin and end timestamps are available.
Definition
timings.cpp:167
micro_os_plus::micro_test_plus::detail::timestamps::timestamp_begin
void timestamp_begin(void) noexcept
Records the begin timestamp using the current system clock.
Definition
timings.cpp:102
micro_os_plus::micro_test_plus::detail::timestamps::compute_elapsed_time
void compute_elapsed_time(uint32_t &milliseconds, uint32_t µseconds) const
Computes the elapsed time between begin and end timestamps.
Definition
timings.cpp:184
micro_os_plus::micro_test_plus::detail::timestamps::end_time_
std::optional< timestamp > end_time_
The timestamp recorded at the end of the test suite.
Definition
timings.h:338
micro_os_plus::micro_test_plus::detail::timestamps::begin_time_
std::optional< timestamp > begin_time_
The timestamp recorded at the beginning of the test suite.
Definition
timings.h:333
micro_os_plus::micro_test_plus::detail::timestamps::timestamp_end
void timestamp_end(void) noexcept
Records the end timestamp using the current system clock.
Definition
timings.cpp:135
micro_os_plus::micro_test_plus::detail
Internal implementation details for the µTest++ framework.
Definition
deferred-reporter.h:79
timings.h
C++ header file with declarations for the µTest++ timing utilities.
src
timings.cpp
Generated by
1.17.0