Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/green/utils/timing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
7 changes: 6 additions & 1 deletion test/utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}