src/workflow/Timer.hpp
| 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 | #ifndef WORKFLOW_UTIL_TIMER_HPP | ||
| 7 | #define WORKFLOW_UTIL_TIMER_HPP | ||
| 8 | |||
| 9 | #include <chrono> | ||
| 10 | #include <string> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | namespace openstudio::workflow::util { | ||
| 14 | |||
| 15 | class Timer | ||
| 16 | { | ||
| 17 | public: | ||
| 18 | // start is initialized to now() when ctor is called, end is not initialized | ||
| 19 | Timer(std::string message, unsigned level = 0); | ||
| 20 | |||
| 21 | auto start() const; | ||
| 22 | auto end() const; | ||
| 23 | |||
| 24 | unsigned level() const; | ||
| 25 | |||
| 26 | // Reset start to now, end to none | ||
| 27 | void tick(); | ||
| 28 | |||
| 29 | // Capture end time | ||
| 30 | void tock(); | ||
| 31 | |||
| 32 | std::string message() const; | ||
| 33 | |||
| 34 | auto duration() const; | ||
| 35 | |||
| 36 | std::string format() const; | ||
| 37 | |||
| 38 | std::string formatRow(size_t message_len, size_t timepoint_len, size_t duration_len) const; | ||
| 39 | |||
| 40 | private: | ||
| 41 | using DurationType = std::chrono::milliseconds; | ||
| 42 | using ClockType = std::chrono::system_clock; | ||
| 43 | using TimePointType = decltype(ClockType::now()); | ||
| 44 | |||
| 45 | std::string m_message; | ||
| 46 | TimePointType m_start = ClockType::now(); | ||
| 47 | TimePointType m_end; | ||
| 48 | unsigned m_level; | ||
| 49 | }; | ||
| 50 | |||
| 51 | class TimerCollection | ||
| 52 | { | ||
| 53 | public: | ||
| 54 | ✗ | TimerCollection() = default; | |
| 55 | |||
| 56 | // TODO: maybe it's a bad idea to return the Timer reference... code writer is responsible for ensuring they call tockCurrentTimer | ||
| 57 | // We could just return a size_t (currentTimerIndex) and have tockTimer(size_t index); | ||
| 58 | Timer& newTimer(std::string message, unsigned level = 0); | ||
| 59 | void tockCurrentTimer(); | ||
| 60 | |||
| 61 | // 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 | ||
| 62 | std::string timeReport(int line_length = 80, bool fit = true) const; | ||
| 63 | |||
| 64 | private: | ||
| 65 | std::vector<Timer> m_timers; | ||
| 66 | mutable Timer m_totalTimer{"Total"}; | ||
| 67 | |||
| 68 | // this is so we can nest timings and detailed (sub) timings | ||
| 69 | std::vector<size_t> m_timerIndices; | ||
| 70 | }; | ||
| 71 | |||
| 72 | } // namespace openstudio::workflow::util | ||
| 73 | |||
| 74 | #endif // WORKFLOW_UTIL_TIMER_HPP | ||
| 75 |