Skip to main content

timestamps Class

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

Declaration

class micro_os_plus::micro_test_plus::detail::timestamps { ... }

Included Headers

Public Constructors Index

timestamps ()=default

Default constructor. Both timestamps are uninitialised. More...

timestamps (const timestamps &)=delete

Deleted copy constructor to prevent copying. More...

timestamps (timestamps &&)=delete

Deleted move constructor to prevent moving. More...

Public Destructor Index

~timestamps ()=default

Defaulted destructor. More...

Public Operators Index

timestamps &operator= (const timestamps &)=delete

Deleted copy assignment operator to prevent copying. More...

timestamps &operator= (timestamps &&)=delete

Deleted move assignment operator to prevent moving. More...

Public Member Functions Index

voidcompute_elapsed_time (uint32_t &milliseconds, uint32_t &microseconds) const

Computes the elapsed time between begin and end timestamps. More...

boolhas_begin () const noexcept

Returns true if the begin timestamp has been recorded. More...

boolhas_end () const noexcept

Returns true if the end timestamp has been recorded. More...

boolhas_timestamps (void) const noexcept

Returns true if both begin and end timestamps are available. More...

voidtimestamp_begin (const timespec &ts) noexcept

Records the begin timestamp from a caller-supplied value. More...

voidtimestamp_begin (void) noexcept

Records the begin timestamp using the current system clock. More...

voidtimestamp_end (const timespec &ts) noexcept

Records the end timestamp from a caller-supplied value. More...

voidtimestamp_end (void) noexcept

Records the end timestamp using the current system clock. More...

Protected Member Attributes Index

std::optional< timestamp >begin_time_

The timestamp recorded at the beginning of the test suite. More...

std::optional< timestamp >end_time_

The timestamp recorded at the end of the test suite. More...

Description

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

timestamps stores an optional begin timestamp and an optional end timestamp. When both are available, compute_elapsed_time() derives the elapsed interval in milliseconds and microseconds.

Typical usage in the framework:

  1. Call timestamp_begin() just before the test suite or session body executes.
  2. Call timestamp_end() immediately after it completes.
  3. Pass the elapsed values to the reporter.

If the platform does not provide a monotonic clock, the std::optional members remain empty and has_timestamps() returns false, so that the reporter can skip timing output.

The class is non-copyable and non-movable to prevent accidental sharing of live timing state.

Definition at line 204 of file timings.h.

Public Constructors

timestamps()

micro_os_plus::micro_test_plus::detail::timestamps::timestamps ()
default

Default constructor. Both timestamps are uninitialised.

Definition at line 210 of file timings.h.

Referenced by timestamps, timestamps, operator= and operator=.

timestamps()

micro_os_plus::micro_test_plus::detail::timestamps::timestamps (const timestamps &)
delete

Deleted copy constructor to prevent copying.

Definition at line 215 of file timings.h.

Reference timestamps.

timestamps()

micro_os_plus::micro_test_plus::detail::timestamps::timestamps (timestamps &&)
delete

Deleted move constructor to prevent moving.

Definition at line 220 of file timings.h.

Reference timestamps.

Public Destructor

~timestamps()

micro_os_plus::micro_test_plus::detail::timestamps::~timestamps ()
default

Defaulted destructor.

Definition at line 239 of file timings.h.

References compute_elapsed_time, has_end and has_timestamps.

Public Operators

operator=()

timestamps & micro_os_plus::micro_test_plus::detail::timestamps::operator= (const timestamps &)
delete

Deleted copy assignment operator to prevent copying.

Definition at line 226 of file timings.h.

Reference timestamps.

operator=()

timestamps & micro_os_plus::micro_test_plus::detail::timestamps::operator= (timestamps &&)
delete

Deleted move assignment operator to prevent moving.

Definition at line 233 of file timings.h.

Reference timestamps.

Public Member Functions

compute_elapsed_time()

void micro_os_plus::micro_test_plus::detail::timestamps::compute_elapsed_time (uint32_t & milliseconds, uint32_t & microseconds)

Computes the elapsed time between begin and end timestamps.

Parameters
[out] milliseconds

The elapsed time in whole milliseconds.

[out] microseconds

The sub-millisecond remainder in microseconds.

Subtracts the begin timestamp from the end timestamp in nanoseconds. If the nanosecond difference is negative, one second is borrowed from the seconds delta to normalise the result. The total elapsed duration in microseconds is then split into whole milliseconds (written to milliseconds) and the remainder microseconds (written to microseconds). Requires has_timestamps() to be true; behaviour is undefined otherwise.

Declaration at line 326 of file timings.h, definition at line 192 of file timings.cpp.

192 timestamps::compute_elapsed_time (uint32_t& milliseconds,
193 uint32_t& microseconds) const
194 {
195 assert (has_timestamps ());
196
197 // Precondition: has_timestamps() must be true before calling this method.
198 // Invoking it with disengaged optionals is undefined behaviour.
199 long long delta_ns
200 = end_time_->value ().tv_nsec - begin_time_->value ().tv_nsec;
201 long long delta_s
202 = end_time_->value ().tv_sec - begin_time_->value ().tv_sec;
203 if (delta_ns < 0)
204 {
205 delta_ns += 1000000000LL;
206 --delta_s;
207 }
208
209 // Split into milliseconds and microseconds.
210 const long long total_us = delta_s * 1000000LL + delta_ns / 1000LL;
211 milliseconds = static_cast<uint32_t> (total_us / 1000LL);
212 microseconds = static_cast<uint32_t> (total_us % 1000LL);
213 }

References begin_time_, end_time_ and has_timestamps.

Referenced by ~timestamps, micro_os_plus::micro_test_plus::reporter_human::end_session, micro_os_plus::micro_test_plus::reporter_tap::end_session, micro_os_plus::micro_test_plus::reporter_human::end_suite and micro_os_plus::micro_test_plus::reporter_tap::end_suite.

has_begin()

bool micro_os_plus::micro_test_plus::detail::timestamps::has_begin ()
inline noexcept

Returns true if the begin timestamp has been recorded.

Parameters

None.

Return Values
true

timestamp_begin() has been called.

false

timestamp_begin() has not been called.

Returns true when begin_time_ holds a value, i.e. when timestamp_begin() has previously been called.

Declaration at line 292 of file timings.h, definition at line 89 of file timings-inlines.h.

89 timestamps::has_begin () const noexcept
90 {
91 return begin_time_.has_value ();
92 }

Reference begin_time_.

has_end()

bool micro_os_plus::micro_test_plus::detail::timestamps::has_end ()
inline noexcept

Returns true if the end timestamp has been recorded.

Parameters

None.

Return Values
true

timestamp_end() has been called.

false

timestamp_end() has not been called.

Returns true when end_time_ holds a value, i.e. when timestamp_end() has previously been called.

Declaration at line 303 of file timings.h, definition at line 100 of file timings-inlines.h.

100 timestamps::has_end () const noexcept
101 {
102 return end_time_.has_value ();
103 }

Reference end_time_.

Referenced by ~timestamps.

has_timestamps()

bool micro_os_plus::micro_test_plus::detail::timestamps::has_timestamps (void)
noexcept

Returns true if both begin and end timestamps are available.

Parameters

None.

Return Values
true

Both timestamps are present and elapsed time can be computed.

false

At least one timestamp is absent; elapsed time is not available.

Returns true only when both the begin and end optional timestamps are engaged and each contains a valid (non-zero) clock reading, as determined by timestamp::has_clock().

Declaration at line 316 of file timings.h, definition at line 175 of file timings.cpp.

175 timestamps::has_timestamps (void) const noexcept
176 {
177 return begin_time_.has_value () && begin_time_->has_clock ()
178 && end_time_.has_value () && end_time_->has_clock ();
179 }

References begin_time_ and end_time_.

Referenced by ~timestamps, compute_elapsed_time, micro_os_plus::micro_test_plus::reporter_human::end_session, micro_os_plus::micro_test_plus::reporter_tap::end_session, micro_os_plus::micro_test_plus::reporter_human::end_suite and micro_os_plus::micro_test_plus::reporter_tap::end_suite.

timestamp_begin()

void micro_os_plus::micro_test_plus::detail::timestamps::timestamp_begin (const timespec & ts)
noexcept

Records the begin timestamp from a caller-supplied value.

Parameters
ts

The timespec value to use as the begin timestamp.

Returns

Nothing.

If the begin timestamp has not yet been set, a timestamp is constructed in-place from the supplied timespec value. Subsequent calls are silently ignored, ensuring idempotent behaviour.

Declaration at line 260 of file timings.h, definition at line 126 of file timings.cpp.

126 timestamps::timestamp_begin (const timespec& ts) noexcept
127 {
128 // Ensure it is timestamped only once.
129 if (!begin_time_.has_value ())
130 {
131 begin_time_.emplace (ts);
132 }
133 }

Reference begin_time_.

timestamp_begin()

void micro_os_plus::micro_test_plus::detail::timestamps::timestamp_begin (void)
noexcept

Records the begin timestamp using the current system clock.

Parameters

None.

Returns

Nothing.

If the begin timestamp has not yet been set, a timestamp is constructed in-place using the default constructor, which captures the current monotonic time. Subsequent calls are silently ignored, ensuring idempotent behaviour.

Declaration at line 250 of file timings.h, definition at line 110 of file timings.cpp.

111 {
112 // Ensure it is timestamped only once.
113 if (!begin_time_.has_value ())
114 {
115 begin_time_.emplace ();
116 }
117 }

Reference begin_time_.

timestamp_end()

void micro_os_plus::micro_test_plus::detail::timestamps::timestamp_end (const timespec & ts)
noexcept

Records the end timestamp from a caller-supplied value.

Parameters
ts

The timespec value to use as the end timestamp.

Returns

Nothing.

If the end timestamp has not yet been set, a timestamp is constructed in-place from the supplied timespec value. Subsequent calls are silently ignored, ensuring idempotent behaviour.

Declaration at line 281 of file timings.h, definition at line 159 of file timings.cpp.

159 timestamps::timestamp_end (const timespec& ts) noexcept
160 {
161 // Ensure it is timestamped only once.
162 if (!end_time_.has_value ())
163 {
164 end_time_.emplace (ts);
165 }
166 }

Reference end_time_.

timestamp_end()

void micro_os_plus::micro_test_plus::detail::timestamps::timestamp_end (void)
noexcept

Records the end timestamp using the current system clock.

Parameters

None.

Returns

Nothing.

If the end timestamp has not yet been set, a timestamp is constructed in-place using the default constructor, which captures the current monotonic time. Subsequent calls are silently ignored, ensuring idempotent behaviour.

Declaration at line 271 of file timings.h, definition at line 143 of file timings.cpp.

144 {
145 // Ensure it is timestamped only once.
146 if (!end_time_.has_value ())
147 {
148 end_time_.emplace ();
149 }
150 }

Reference end_time_.

Protected Member Attributes

begin_time_

std::optional<timestamp> micro_os_plus::micro_test_plus::detail::timestamps::begin_time_
protected

The timestamp recorded at the beginning of the test suite.

Definition at line 333 of file timings.h.

333 std::optional<timestamp> begin_time_;

Referenced by compute_elapsed_time, has_begin, has_timestamps, timestamp_begin and timestamp_begin.

end_time_

std::optional<timestamp> micro_os_plus::micro_test_plus::detail::timestamps::end_time_
protected

The timestamp recorded at the end of the test suite.

Definition at line 338 of file timings.h.

338 std::optional<timestamp> end_time_;

Referenced by compute_elapsed_time, has_end, has_timestamps, timestamp_end and timestamp_end.


The documentation for this class was generated from the following files:


Generated via doxygen2docusaurus 2.2.1 by Doxygen 1.17.0.