diff --git a/dune/stuff/common/profiler.cc b/dune/stuff/common/profiler.cc index b24a0fc75f18e182018d5834e270788efd6dca59..529739cb859b0e23bdd1c2117b5cab14a5f64f09 100644 --- a/dune/stuff/common/profiler.cc +++ b/dune/stuff/common/profiler.cc @@ -97,6 +97,10 @@ long Profiler::getTimingIdx(const std::string section_name, const int run_number const Datamap& data = datamaps_[run_number]; Datamap::const_iterator section = data.find(section_name); if (section == data.end()) { + // timer might still be running + const auto& timer_it = known_timers_map_.find(section_name); + if (timer_it != known_timers_map_.end()) + return timer_it->second.second.delta(); ASSERT_EXCEPTION(false, "no timer found: " + section_name); return -1; } @@ -120,6 +124,9 @@ void Profiler::addCount(const int num) void Profiler::nextRun() { + // set all known timers to "stopped" + for (auto& timer_it : known_timers_map_) + timer_it.second.first = false; current_run_number_++; } diff --git a/dune/stuff/test/common_profiler.cc b/dune/stuff/test/common_profiler.cc index 537f25448861b640190cd9f21d5043a42d0b26bb..5935eb69487cb939822e1b660b556757e572e588 100644 --- a/dune/stuff/test/common_profiler.cc +++ b/dune/stuff/test/common_profiler.cc @@ -34,13 +34,15 @@ TEST(ProfilerTest, ScopedTiming) TEST(ProfilerTest, MultiRuns) { const auto range = Math::range(1, 3); - DSC_PROFILER.reset(range.size()); + // needs to be range.size() + 1 since we're calling nextRun() range.size() times + DSC_PROFILER.reset(range.size() + 1); for (auto i : range) { scoped_busywait("ProfilerTest.MultiRuns", i * wait_ms); DSC_PROFILER.nextRun(); } for (auto i : range) { - EXPECT_GE(DSC_PROFILER.getTiming("ProfilerTest.MultiRuns", i - 1), i * wait_ms); + // i-1 cause runs have 0-based index + EXPECT_GE(DSC_PROFILER.getTimingIdx("ProfilerTest.MultiRuns", i - 1), i * wait_ms); } } @@ -63,6 +65,19 @@ TEST(ProfilerTest, ExpectedFailures) EXPECT_THROW(DSC_PROFILER.stopTiming("This_section_was_never_start"), Dune::RangeError); } +TEST(ProfilerTest, NestedTiming) +{ + auto& prof = DSC_PROFILER; + prof.reset(1); + prof.startTiming("NestedTiming.Outer"); + busywait(100); + prof.startTiming("NestedTiming.Inner"); + busywait(100); + auto inner = prof.getTiming("NestedTiming.Inner"); + auto outer = prof.getTiming("NestedTiming.Outer"); + EXPECT_GT(outer, inner); +} + int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv);