diff --git a/src/green/utils/timing.h b/src/green/utils/timing.h index b5e5a5a..2932b1f 100644 --- a/src/green/utils/timing.h +++ b/src/green/utils/timing.h @@ -202,17 +202,34 @@ namespace green::utils { } + /** + * @brief reset the `duration` and `active` status of the given event and all its children + * + * @param event The event to reset along with all its children. + */ + void reset_event_and_children(event_t& event) { + event.duration = 0.0; + event.active = false; + for (auto& child_kv : event.children) { + reset_event_and_children(*child_kv.second); + } + } /** - * @brief reset the `duration` attribute of all child events of the current event + * @brief reset the `duration` and `active` status of all the children events + * of the current event * If there is no current event (i.e., at the root level when `_current_event` is nullptr), - * this method returns silently and does nothing. + * this method resets all root events and their children. */ void reset() { - if (!_current_event) return; + if (!_current_event) { + for (auto& kv : _root_events) { + reset_event_and_children(*kv.second); + } + return; + } for (auto& kv : _current_event->children) { - kv.second->duration = 0.0; - kv.second->active = false; + reset_event_and_children(*kv.second); } } diff --git a/test/utils_test.cpp b/test/utils_test.cpp index 278a573..78392ad 100644 --- a/test/utils_test.cpp +++ b/test/utils_test.cpp @@ -135,7 +135,7 @@ TEST_CASE("Timing") { statistic.start("CHILD2"); std::this_thread::sleep_for(std::chrono::milliseconds(500)); statistic.end(); - statistic.reset(); + statistic.reset(); // reset during active ROOT event only resets its children statistic.end(); double duration_root = statistic.event("ROOT").duration; @@ -144,5 +144,10 @@ TEST_CASE("Timing") { REQUIRE(duration_child1 == 0.0); REQUIRE(duration_child2 == 0.0); + REQUIRE(duration_root > 0.0); + + statistic.reset(); // reset at root level resets all events + duration_root = statistic.event("ROOT").duration; + REQUIRE(duration_root == 0.0); } }