Skip to main content

timings.h File

C++ header file with declarations for the µTest++ timing utilities. More...

Included Headers

#include <cstdint> #include <optional> #include <ctime> #include "inlines/timings-inlines.h"

Namespaces Index

namespacemicro_os_plus

The primary namespace for the µOS++ framework. More...

namespacemicro_test_plus

Primary namespace for the µTest++ testing framework. More...

namespacedetail

Internal implementation details for the µTest++ framework. More...

Classes Index

classtimestamp

A single point-in-time measurement, wrapping a timespec value. More...

classtimestamps

A begin/end timestamp pair used to measure elapsed time. More...

Description

C++ header file with declarations for the µTest++ timing utilities.

This header provides two lightweight classes used to measure the elapsed time of test suites and test sessions within the µTest++ framework:

  • timestamp wraps a single timespec value and records one point in time. It is default-constructible and trivially copyable.
  • timestamps pairs a begin and an end timestamp (both stored as std::optional<timestamp>) and exposes methods for recording the start and finish of an operation, querying whether both timestamps are available, and computing the elapsed time in milliseconds and microseconds.

Timing support relies on clock_gettime(CLOCK_MONOTONIC_RAW, ...) when available; if the clock is not present (e.g. on bare-metal targets without an RTC) the timestamps are simply omitted and no timing information is reported.

All definitions reside within the micro_os_plus::micro_test_plus namespace, ensuring clear separation from user code and minimising the risk of naming conflicts.

This file is intended solely for internal use within the framework and should not be included directly by user code.

File Listing

The file content with the documentation metadata removed is:

1
2// ----------------------------------------------------------------------------
3/*
4 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
5 * Copyright (c) 2021-2026 Liviu Ionescu. All rights reserved.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose is hereby granted, under the terms of the MIT license.
9 *
10 * If a copy of the license was not distributed with this file, it can be
11 * obtained from https://opensource.org/licenses/mit.
12 */
13
14// ----------------------------------------------------------------------------
15
44
45#ifndef MICRO_OS_PLUS_MICRO_TEST_PLUS_TIMINGS_H_
46#define MICRO_OS_PLUS_MICRO_TEST_PLUS_TIMINGS_H_
47
48// ----------------------------------------------------------------------------
49
50#if defined(__cplusplus)
51
52// ----------------------------------------------------------------------------
53
54#if __has_include("micro-os-plus/project-config.h")
55#include "micro-os-plus/project-config.h"
56#endif // __has_include("micro-os-plus/project-config.h")
57
58#if __has_include("micro-os-plus/micro-test-plus-defines.h")
59#include "micro-os-plus/micro-test-plus-defines.h"
60#endif // __has_include("micro-os-plus/micro-test-plus-defines.h")
61
62#include <cstdint>
63#include <optional>
64#include <ctime>
65
66// ----------------------------------------------------------------------------
67
68#if defined(__GNUC__)
69#pragma GCC diagnostic push
70
71#if defined(__clang__)
72#pragma clang diagnostic ignored "-Wc++98-compat"
73#pragma clang diagnostic ignored "-Wpre-c++17-compat"
74#endif // defined(__clang__)
75#endif // defined(__GNUC__)
76
77// ============================================================================
78
80{
81 // --------------------------------------------------------------------------
82
83 namespace detail
84 {
85 // ========================================================================
86
103 {
104 public:
108 timestamp () noexcept;
109
115 timestamp (const timespec& ts) noexcept;
116
117 // `timespec` is trivially copyable, so copy and move are safe to
118 // default. Defaulting these operations allows `timestamp` to be used in
119 // contexts that require copyability or movability (e.g. containers,
120 // algorithms).
124 timestamp (const timestamp&) = default;
125
129 timestamp (timestamp&&) = default;
130
136 = default;
137
143 = default;
144
148 ~timestamp () = default;
149
158 bool
159 has_clock (void) const noexcept;
160
168 [[nodiscard]] timespec&
169 value () noexcept;
170
178 [[nodiscard]] const timespec&
179 value () const noexcept;
180
181 protected:
185 timespec value_{};
186 };
187
188 // ========================================================================
189
214 {
215 public:
219 timestamps () = default;
220
224 timestamps (const timestamps&) = delete;
225
229 timestamps (timestamps&&) = delete;
230
236 = delete;
237
243 = delete;
244
248 ~timestamps () = default;
249
258 void
259 timestamp_begin (void) noexcept;
260
268 void
269 timestamp_begin (const timespec& ts) noexcept;
270
279 void
280 timestamp_end (void) noexcept;
281
289 void
290 timestamp_end (const timespec& ts) noexcept;
291
300 bool
301 has_begin () const noexcept;
302
311 bool
312 has_end () const noexcept;
313
324 bool
325 has_timestamps (void) const noexcept;
326
334 void
335 compute_elapsed_time (uint32_t& milliseconds,
336 uint32_t& microseconds) const;
337
338 protected:
342 std::optional<timestamp> begin_time_;
343
347 std::optional<timestamp> end_time_;
348 };
349
350 // ------------------------------------------------------------------------
351 } // namespace detail
352 // ==========================================================================
353} // namespace micro_os_plus::micro_test_plus
354
355#if defined(__GNUC__)
356#pragma GCC diagnostic pop
357#endif // defined(__GNUC__)
358
359// ----------------------------------------------------------------------------
360
361#endif // defined(__cplusplus)
362
363// ============================================================================
364// Templates, inlines & constexpr implementations.
365
367
368// ----------------------------------------------------------------------------
369
370#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TIMINGS_H_
371
372// ----------------------------------------------------------------------------

Generated via doxygen2docusaurus 2.2.2 by Doxygen 1.17.0.