OmniSciDB  04ee39c94c
measure.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, 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 "Logger.h"
24 
25 extern bool g_enable_debug_timer;
26 
27 template <typename TimeT = std::chrono::milliseconds>
28 struct measure {
29  template <typename F, typename... Args>
30  static typename TimeT::rep execution(F func, Args&&... args) {
31  auto start = std::chrono::steady_clock::now();
32  func(std::forward<Args>(args)...);
33  auto duration =
34  std::chrono::duration_cast<TimeT>(std::chrono::steady_clock::now() - start);
35  return duration.count();
36  }
37 };
38 
39 template <typename Type = std::chrono::steady_clock::time_point>
40 Type timer_start() {
41  return std::chrono::steady_clock::now();
42 }
43 
44 template <typename Type = std::chrono::steady_clock::time_point,
45  typename TypeR = std::chrono::milliseconds>
46 typename TypeR::rep timer_stop(Type clock_begin) {
47  auto duration =
48  std::chrono::duration_cast<TypeR>(std::chrono::steady_clock::now() - clock_begin);
49  return duration.count();
50 }
51 
52 template <typename Type = std::chrono::steady_clock::time_point,
53  typename TypeR = std::chrono::milliseconds>
54 std::string timer_lap(Type clock_begin, Type& clock_last) {
55  auto now = std::chrono::steady_clock::now();
56  auto overall_duration = (now - clock_begin);
57  auto since_last_duration = (now - clock_last);
58  auto overall = std::chrono::duration_cast<TypeR>(overall_duration);
59  auto since_last = std::chrono::duration_cast<TypeR>(since_last_duration);
60  clock_last = now;
61  // std::string ret(overall.count() + " elapsed " + since_last.count());
62  std::ostringstream oss;
63  oss << overall.count() << " - " << since_last.count();
64  return oss.str();
65 }
66 
67 struct InjectTimer {
68  InjectTimer(std::string const& description, int const& lineNum, std::string const& func)
69  : description_(description), lineNum_(lineNum), func_(func) {
71  start_ = timer_start();
72  LOG(INFO) << "Timer start " << std::setfill(' ') << std::setw(35) << description_
73  << " " << std::setw(35) << func_ << ":" << std::setw(5) << lineNum_;
74  }
75  }
76 
79  LOG(INFO) << "Timer end " << std::setfill(' ') << std::setw(35) << description_
80  << " " << std::setw(35) << func_ << ":" << std::setw(5) << lineNum_
81  << " elapsed " << timer_stop(start_) << " ms";
82  }
83  }
84 
85  std::string description_;
86  int lineNum_;
87  std::string func_;
88 
89  std::chrono::steady_clock::time_point start_;
90 };
91 #define INJECT_TIMER(DESC) InjectTimer DESC(#DESC, __LINE__, __FUNCTION__)
92 
93 template <typename Fn, Fn fn, typename... Args>
94 typename std::result_of<Fn(Args...)>::type time_wrap(Args... args) {
95  InjectTimer t("test", 1, "test");
96  return fn(std::forward<Args>(args)...);
97 }
98 #define TIME_WRAP(FUNC) time_wrap<decltype(&FUNC), &FUNC>
99 
100 #endif // _MEASURE_H_
std::result_of< Fn(Args...)>::type time_wrap(Args... args)
Definition: measure.h:94
bool g_enable_debug_timer
Definition: Execute.cpp:68
std::chrono::steady_clock::time_point start_
Definition: measure.h:89
#define LOG(tag)
Definition: Logger.h:182
int lineNum_
Definition: measure.h:86
TypeR::rep timer_stop(Type clock_begin)
Definition: measure.h:46
std::string func_
Definition: measure.h:87
std::string timer_lap(Type clock_begin, Type &clock_last)
Definition: measure.h:54
~InjectTimer()
Definition: measure.h:77
std::string description_
Definition: measure.h:85
InjectTimer(std::string const &description, int const &lineNum, std::string const &func)
Definition: measure.h:68
static TimeT::rep execution(F func, Args &&... args)
Definition: measure.h:30
Type timer_start()
Definition: measure.h:40