OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
measure.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _MEASURE_H_
18 #define _MEASURE_H_
19 
20 #include <chrono>
21 #include <iomanip>
22 #include <sstream>
23 #include <type_traits>
24 
25 #include "Logger/Logger.h"
26 
27 extern bool g_enable_debug_timer;
28 
29 template <typename TimeT = std::chrono::milliseconds>
30 struct measure {
31  template <typename F, typename... Args>
32  static typename TimeT::rep execution(F func, Args&&... args) {
33  auto start = std::chrono::steady_clock::now();
34  func(std::forward<Args>(args)...);
35  auto duration =
36  std::chrono::duration_cast<TimeT>(std::chrono::steady_clock::now() - start);
37  return duration.count();
38  }
39 };
40 
41 template <typename Type = std::chrono::steady_clock::time_point>
43  return std::chrono::steady_clock::now();
44 }
45 
46 template <typename Type = std::chrono::steady_clock::time_point,
47  typename TypeR = std::chrono::milliseconds>
48 typename TypeR::rep timer_stop(Type clock_begin) {
49  auto duration =
50  std::chrono::duration_cast<TypeR>(std::chrono::steady_clock::now() - clock_begin);
51  return duration.count();
52 }
53 
54 template <typename TimeT = std::chrono::milliseconds>
55 class Timer {
56  public:
57  Timer() : duration{0}, timer_started_(false) {}
58 
59  void start() {
61  timer_started_ = true;
62  }
63 
64  void stop() {
65  if (!timer_started_) {
66  LOG(WARNING) << " unexpected call to stop on a timer that has not started";
67  }
69  timer_started_ = false;
70  }
71 
72  typename TimeT::rep elapsed() { return duration; }
73 
74  private:
75  std::chrono::steady_clock::time_point start_time_;
76  typename TimeT::rep duration;
78 };
79 
81  timer_stop<std::chrono::steady_clock::time_point, std::chrono::microseconds>;
82 
83 template <typename Type = std::chrono::steady_clock::time_point,
84  typename TypeR = std::chrono::milliseconds>
85 std::string timer_lap(Type clock_begin, Type& clock_last) {
86  auto now = std::chrono::steady_clock::now();
87  auto overall_duration = (now - clock_begin);
88  auto since_last_duration = (now - clock_last);
89  auto overall = std::chrono::duration_cast<TypeR>(overall_duration);
90  auto since_last = std::chrono::duration_cast<TypeR>(since_last_duration);
91  clock_last = now;
92  // std::string ret(overall.count() + " elapsed " + since_last.count());
93  std::ostringstream oss;
94  oss << overall.count() << " - " << since_last.count();
95  return oss.str();
96 }
97 
98 struct InjectTimer {
99  InjectTimer(std::string const& description, int const& lineNum, std::string const& func)
100  : description_(description), lineNum_(lineNum), func_(func) {
101  if (g_enable_debug_timer) {
102  start_ = timer_start();
103  LOG(INFO) << "Timer start " << std::setfill(' ') << std::setw(35) << description_
104  << " " << std::setw(35) << func_ << ":" << std::setw(5) << lineNum_;
105  }
106  }
107 
109  if (g_enable_debug_timer) {
110  LOG(INFO) << "Timer end " << std::setfill(' ') << std::setw(35) << description_
111  << " " << std::setw(35) << func_ << ":" << std::setw(5) << lineNum_
112  << " elapsed " << timer_stop(start_) << " ms";
113  }
114  }
115 
116  std::string description_;
117  int lineNum_;
118  std::string func_;
119 
120  std::chrono::steady_clock::time_point start_;
121 };
122 #define INJECT_TIMER(DESC) InjectTimer DESC(#DESC, __LINE__, __FUNCTION__)
123 
124 template <typename Fn, Fn fn, typename... Args>
125 typename std::result_of<Fn(Args...)>::type time_wrap(Args... args) {
126  InjectTimer t("test", 1, "test");
127  return fn(std::forward<Args>(args)...);
128 }
129 #define TIME_WRAP(FUNC) time_wrap<decltype(&FUNC), &FUNC>
130 
131 #endif // _MEASURE_H_
std::chrono::steady_clock::time_point start_
Definition: measure.h:120
Definition: measure.h:55
static TimeT::rep execution(F func, Args &&...args)
Definition: measure.h:32
Timer()
Definition: measure.h:57
#define LOG(tag)
Definition: Logger.h:285
bool g_enable_debug_timer
Definition: Logger.cpp:17
int lineNum_
Definition: measure.h:117
void stop()
Definition: measure.h:64
TypeR::rep timer_stop(Type clock_begin)
Definition: measure.h:48
std::string func_
Definition: measure.h:118
std::string timer_lap(Type clock_begin, Type &clock_last)
Definition: measure.h:85
const auto timer_stop_microseconds
Definition: measure.h:80
~InjectTimer()
Definition: measure.h:108
std::string description_
Definition: measure.h:116
InjectTimer(std::string const &description, int const &lineNum, std::string const &func)
Definition: measure.h:99
TimeT::rep duration
Definition: measure.h:76
std::result_of< Fn(Args...)>::type time_wrap(Args...args)
Definition: measure.h:125
TimeT::rep elapsed()
Definition: measure.h:72
void start()
Definition: measure.h:59
std::chrono::steady_clock::time_point start_time_
Definition: measure.h:75
Type timer_start()
Definition: measure.h:42
bool timer_started_
Definition: measure.h:77