proxygen
AutoTimer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-present Facebook, 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 #pragma once
17 
18 #include <chrono>
19 #include <string>
20 #include <type_traits>
21 
22 #include <folly/Conv.h>
23 #include <folly/Format.h>
24 #include <folly/Optional.h>
25 #include <folly/String.h>
26 #include <glog/logging.h>
27 
28 namespace folly {
29 
30 // Default logger
32 template <GoogleLoggerStyle>
33 struct GoogleLogger;
34 
60 template <
62  class Clock = std::chrono::high_resolution_clock>
63 class AutoTimer final {
64  public:
65  using DoubleSeconds = std::chrono::duration<double>;
66 
67  explicit AutoTimer(
68  std::string&& msg = "",
69  const DoubleSeconds& minTimetoLog = DoubleSeconds::zero(),
70  Logger&& logger = Logger())
71  : destructionMessage_(std::move(msg)),
72  minTimeToLog_(minTimetoLog),
73  logger_(std::move(logger)) {}
74 
75  // It doesn't really make sense to copy AutoTimer
76  // Movable to make sure the helper method for creating an AutoTimer works.
77  AutoTimer(const AutoTimer&) = delete;
78  AutoTimer(AutoTimer&&) = default;
79  AutoTimer& operator=(const AutoTimer&) = delete;
80  AutoTimer& operator=(AutoTimer&&) = default;
81 
83  if (destructionMessage_) {
84  log(destructionMessage_.value());
85  }
86  }
87 
89  return logImpl(Clock::now(), msg);
90  }
91 
92  template <typename... Args>
93  DoubleSeconds log(Args&&... args) {
94  auto now = Clock::now();
95  return logImpl(now, to<std::string>(std::forward<Args>(args)...));
96  }
97 
98  template <typename... Args>
100  auto now = Clock::now();
101  return logImpl(now, format(std::forward<Args>(args)...).str());
102  }
103 
104  private:
105  // We take in the current time so that we don't measure time to call
106  // to<std::string> or format() in the duration.
107  DoubleSeconds logImpl(std::chrono::time_point<Clock> now, StringPiece msg) {
108  auto duration = now - start_;
109  if (duration >= minTimeToLog_) {
110  logger_(msg, duration);
111  }
112  start_ = Clock::now(); // Don't measure logging time
113  return duration;
114  }
115 
117  std::chrono::time_point<Clock> start_ = Clock::now();
120 };
121 
122 template <
124  class Clock = std::chrono::high_resolution_clock>
126  std::string&& msg = "",
127  const std::chrono::duration<double>& minTimeToLog =
128  std::chrono::duration<double>::zero(),
129  Logger&& logger = Logger()) {
131  std::move(msg), minTimeToLog, std::move(logger));
132 }
133 
134 template <GoogleLoggerStyle Style>
135 struct GoogleLogger final {
136  void operator()(StringPiece msg, const std::chrono::duration<double>& sec)
137  const {
138  if (msg.empty()) {
139  return;
140  }
141  if (Style == GoogleLoggerStyle::PRETTY) {
142  LOG(INFO) << msg << " in "
143  << prettyPrint(sec.count(), PrettyType::PRETTY_TIME);
144  } else {
145  LOG(INFO) << msg << " in " << sec.count() << " seconds";
146  }
147  }
148 };
149 } // namespace folly
Optional< std::string > destructionMessage_
Definition: AutoTimer.h:116
std::chrono::steady_clock::time_point now()
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
AutoTimer(std::string &&msg="", const DoubleSeconds &minTimetoLog=DoubleSeconds::zero(), Logger &&logger=Logger())
Definition: AutoTimer.h:67
DoubleSeconds logImpl(std::chrono::time_point< Clock > now, StringPiece msg)
Definition: AutoTimer.h:107
DoubleSeconds logFormat(Args &&...args)
Definition: AutoTimer.h:99
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::string prettyPrint(double val, PrettyType type, bool addSpace)
Definition: String.cpp:386
GoogleLoggerStyle
Definition: AutoTimer.h:31
constexpr bool empty() const
Definition: Range.h:443
std::chrono::duration< double > DoubleSeconds
Definition: AutoTimer.h:65
auto makeAutoTimer(std::string &&msg="", const std::chrono::duration< double > &minTimeToLog=std::chrono::duration< double >::zero(), Logger &&logger=Logger())
Definition: AutoTimer.h:125
DoubleSeconds log(StringPiece msg="")
Definition: AutoTimer.h:88
void operator()(StringPiece msg, const std::chrono::duration< double > &sec) const
Definition: AutoTimer.h:136
DoubleSeconds log(Args &&...args)
Definition: AutoTimer.h:93
const char * string
Definition: Conv.cpp:212
uintptr_t start_
DoubleSeconds minTimeToLog_
Definition: AutoTimer.h:118
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)
Definition: Format.h:271