src/workflow/Timer.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "Timer.hpp" | ||
| 7 | |||
| 8 | #include "../utilities/core/Assert.hpp" | ||
| 9 | |||
| 10 | #include <fmt/format.h> | ||
| 11 | #include <fmt/chrono.h> // Formatting for std::chrono | ||
| 12 | #include <fmt/color.h> // For fmt::fg | ||
| 13 | |||
| 14 | namespace openstudio::workflow::util { | ||
| 15 | |||
| 16 | ✗ | Timer::Timer(std::string message, unsigned level) : m_message(std::move(message)), m_level(level) {} | |
| 17 | |||
| 18 | ✗ | unsigned Timer::level() const { | |
| 19 | ✗ | return m_level; | |
| 20 | } | ||
| 21 | |||
| 22 | ✗ | auto Timer::start() const { | |
| 23 | ✗ | return m_start; | |
| 24 | } | ||
| 25 | |||
| 26 | ✗ | auto Timer::end() const { | |
| 27 | ✗ | return m_end; | |
| 28 | } | ||
| 29 | |||
| 30 | ✗ | void Timer::tick() { | |
| 31 | ✗ | m_end = TimePointType{}; | |
| 32 | ✗ | m_start = ClockType::now(); | |
| 33 | ✗ | } | |
| 34 | |||
| 35 | ✗ | void Timer::tock() { | |
| 36 | ✗ | m_end = ClockType::now(); | |
| 37 | ✗ | } | |
| 38 | |||
| 39 | ✗ | std::string Timer::message() const { | |
| 40 | ✗ | return m_message; | |
| 41 | } | ||
| 42 | |||
| 43 | ✗ | auto Timer::duration() const { | |
| 44 | ✗ | if (m_end == TimePointType{}) { | |
| 45 | ✗ | throw std::runtime_error(fmt::format("Timer {} with level={} was not stopped", m_message, m_level)); | |
| 46 | } | ||
| 47 | ✗ | return std::chrono::duration_cast<DurationType>(m_end - m_start); | |
| 48 | } | ||
| 49 | |||
| 50 | ✗ | std::string Timer::format() const { | |
| 51 | ✗ | if (m_end == TimePointType{}) { | |
| 52 | ✗ | return fmt::format("Timer {} started at {}", m_message, m_start); | |
| 53 | } else { | ||
| 54 | ✗ | return fmt::format("Timer {} took {} ms", m_message, duration()); | |
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | ✗ | std::string Timer::formatRow(size_t message_len, size_t timepoint_len, size_t duration_len) const { | |
| 59 | ✗ | if (m_level == 1) { | |
| 60 | ✗ | return fmt::format(fmt::fg(fmt::color::light_gray), "| o {3:<{0}.{0}s} | {4:%T} | {5:%T} | {6:^{2}} |\n", message_len - 2, timepoint_len, | |
| 61 | ✗ | duration_len, m_message, m_start, m_end, duration()); | |
| 62 | ✗ | } else if (m_level == 2) { | |
| 63 | ✗ | return fmt::format(fmt::fg(fmt::color::light_gray), "| o {3:<{0}.{0}s} | {4:%T} | {5:%T} | {6:^{2}} |\n", message_len - 4, timepoint_len, | |
| 64 | ✗ | duration_len, m_message, m_start, m_end, duration()); | |
| 65 | } else { | ||
| 66 | ✗ | return fmt::format("| {3:<{0}.{0}s} | {4:%T} | {5:%T} | {6:^{2}} |\n", message_len, timepoint_len, duration_len, m_message, m_start, m_end, | |
| 67 | ✗ | duration()); | |
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | ✗ | Timer& TimerCollection::newTimer(std::string message, unsigned level) { | |
| 72 | ✗ | m_timerIndices.push_back(m_timers.size()); | |
| 73 | ✗ | return m_timers.emplace_back(std::move(message), level); | |
| 74 | } | ||
| 75 | |||
| 76 | ✗ | void TimerCollection::tockCurrentTimer() { | |
| 77 | ✗ | m_timers[m_timerIndices.back()].tock(); | |
| 78 | ✗ | m_timerIndices.pop_back(); | |
| 79 | ✗ | } | |
| 80 | |||
| 81 | // line_length is the maximum terminal width, fit = true will cause the table to be resized down as much as possible, fit = false means the table will take exactly line_length | ||
| 82 | ✗ | std::string TimerCollection::timeReport(int line_length, bool fit) const { | |
| 83 | // Timer | start | end | duration | ||
| 84 | ✗ | std::string result; | |
| 85 | |||
| 86 | ✗ | constexpr size_t timepoint_len = 8; // 19 if not specifying %T | |
| 87 | ✗ | constexpr size_t duration_len = 10; | |
| 88 | ✗ | constexpr size_t ncols = 4; | |
| 89 | ✗ | constexpr size_t extra_chars = 3 * (ncols - 1) + 2 * (ncols - 2); | |
| 90 | ✗ | size_t message_len = line_length - (2 * timepoint_len + duration_len + extra_chars); | |
| 91 | ✗ | if (fit) { | |
| 92 | ✗ | size_t max_message_len = 0; | |
| 93 | ✗ | for (const auto& timer : m_timers) { | |
| 94 | ✗ | max_message_len = std::max(max_message_len, timer.message().size()); | |
| 95 | } | ||
| 96 | ✗ | message_len = std::min(message_len, max_message_len + 4); | |
| 97 | // fmt::print("line_length={}, message_len={}, max_message_len={}\n", line_length, message_len, max_message_len); | ||
| 98 | } | ||
| 99 | |||
| 100 | // Use │ or | ? | ||
| 101 | // result += fmt::format("┌{0:─^{1}}┐\n", "", total_len - 2); | ||
| 102 | result += | ||
| 103 | ✗ | fmt::format("| {3:^{0}} | {4:^{1}} | {5:^{1}} | {6:^{2}} |\n", message_len, timepoint_len, duration_len, "Timer", "start", "end", "duration"); | |
| 104 | // result += fmt::format("|{0:─^{1}}|\n", "", total_len - 2); | ||
| 105 | ✗ | result += fmt::format("|:{3:-^{0}}|:{3:-^{1}}:|:{3:-^{1}}:|:{3:-^{2}}:|\n", message_len + 1, timepoint_len, duration_len, ""); | |
| 106 | |||
| 107 | ✗ | for (const auto& timer : m_timers) { | |
| 108 | ✗ | result += timer.formatRow(message_len, timepoint_len, duration_len); | |
| 109 | } | ||
| 110 | |||
| 111 | ✗ | m_totalTimer.tock(); | |
| 112 | ✗ | result += fmt::format(fmt::fg(fmt::color::red), "| {3:^{0}.{0}s} | {4:%T} | {5:%T} | {6:^{2}} |\n", message_len, timepoint_len, duration_len, | |
| 113 | ✗ | m_totalTimer.message(), m_totalTimer.start(), m_totalTimer.end(), m_totalTimer.duration()); | |
| 114 | |||
| 115 | ✗ | return result; | |
| 116 | ✗ | }; | |
| 117 | |||
| 118 | } // namespace openstudio::workflow::util | ||
| 119 |