OmniSciDB  72c90bc290
 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 
55  timer_stop<std::chrono::steady_clock::time_point, std::chrono::microseconds>;
56 
57 template <typename Type = std::chrono::steady_clock::time_point,
58  typename TypeR = std::chrono::milliseconds>
59 std::string timer_lap(Type clock_begin, Type& clock_last) {
60  auto now = std::chrono::steady_clock::now();
61  auto overall_duration = (now - clock_begin);
62  auto since_last_duration = (now - clock_last);
63  auto overall = std::chrono::duration_cast<TypeR>(overall_duration);
64  auto since_last = std::chrono::duration_cast<TypeR>(since_last_duration);
65  clock_last = now;
66  // std::string ret(overall.count() + " elapsed " + since_last.count());
67  std::ostringstream oss;
68  oss << overall.count() << " - " << since_last.count();
69  return oss.str();
70 }
71 
72 struct InjectTimer {
73  InjectTimer(std::string const& description, int const& lineNum, std::string const& func)
74  : description_(description), lineNum_(lineNum), func_(func) {
76  start_ = timer_start();
77  LOG(INFO) << "Timer start " << std::setfill(' ') << std::setw(35) << description_
78  << " " << std::setw(35) << func_ << ":" << std::setw(5) << lineNum_;
79  }
80  }
81 
84  LOG(INFO) << "Timer end " << std::setfill(' ') << std::setw(35) << description_
85  << " " << std::setw(35) << func_ << ":" << std::setw(5) << lineNum_
86  << " elapsed " << timer_stop(start_) << " ms";
87  }
88  }
89 
90  std::string description_;
91  int lineNum_;
92  std::string func_;
93 
94  std::chrono::steady_clock::time_point start_;
95 };
96 #define INJECT_TIMER(DESC) InjectTimer DESC(#DESC, __LINE__, __FUNCTION__)
97 
98 template <typename Fn, Fn fn, typename... Args>
99 typename std::result_of<Fn(Args...)>::type time_wrap(Args... args) {
100  InjectTimer t("test", 1, "test");
101  return fn(std::forward<Args>(args)...);
102 }
103 #define TIME_WRAP(FUNC) time_wrap<decltype(&FUNC), &FUNC>
104 
105 #endif // _MEASURE_H_
std::chrono::steady_clock::time_point start_
Definition: measure.h:94
static TimeT::rep execution(F func, Args &&...args)
Definition: measure.h:32
#define LOG(tag)
Definition: Logger.h:285
bool g_enable_debug_timer
Definition: Logger.cpp:17
int lineNum_
Definition: measure.h:91
TypeR::rep timer_stop(Type clock_begin)
Definition: measure.h:48
std::string func_
Definition: measure.h:92
std::string timer_lap(Type clock_begin, Type &clock_last)
Definition: measure.h:59
const auto timer_stop_microseconds
Definition: measure.h:54
~InjectTimer()
Definition: measure.h:82
std::string description_
Definition: measure.h:90
InjectTimer(std::string const &description, int const &lineNum, std::string const &func)
Definition: measure.h:73
std::result_of< Fn(Args...)>::type time_wrap(Args...args)
Definition: measure.h:99
Type timer_start()
Definition: measure.h:42