proxygen
folly::AutoTimer< Logger, Clock > Class Template Referencefinal

#include <AutoTimer.h>

Public Types

using DoubleSeconds = std::chrono::duration< double >
 

Public Member Functions

 AutoTimer (std::string &&msg="", const DoubleSeconds &minTimetoLog=DoubleSeconds::zero(), Logger &&logger=Logger())
 
 AutoTimer (const AutoTimer &)=delete
 
 AutoTimer (AutoTimer &&)=default
 
AutoTimeroperator= (const AutoTimer &)=delete
 
AutoTimeroperator= (AutoTimer &&)=default
 
 ~AutoTimer ()
 
DoubleSeconds log (StringPiece msg="")
 
template<typename... Args>
DoubleSeconds log (Args &&...args)
 
template<typename... Args>
DoubleSeconds logFormat (Args &&...args)
 

Private Member Functions

DoubleSeconds logImpl (std::chrono::time_point< Clock > now, StringPiece msg)
 

Private Attributes

Optional< std::stringdestructionMessage_
 
std::chrono::time_point< Clock > start_ = Clock::now()
 
DoubleSeconds minTimeToLog_
 
Logger logger_
 

Detailed Description

template<class Logger = GoogleLogger<GoogleLoggerStyle::PRETTY>, class Clock = std::chrono::high_resolution_clock>
class folly::AutoTimer< Logger, Clock >

Automatically times a block of code, printing a specified log message on destruction or whenever the log() method is called. For example:

AutoTimer t("Foo() completed"); doWork(); t.log("Do work finished"); doMoreWork();

This would print something like: "Do work finished in 1.2 seconds" "Foo() completed in 4.3 seconds"

You can customize what you use as the logger and clock. The logger needs to have an operator()(StringPiece, std::chrono::duration<double>) that gets a message and a duration. The clock needs to model Clock from std::chrono.

The default logger logs usings glog. It only logs if the message is non-empty, so you can also just use this class for timing, e.g.:

AutoTimer t; doWork() const auto how_long = t.log();

Definition at line 63 of file AutoTimer.h.

Member Typedef Documentation

template<class Logger = GoogleLogger<GoogleLoggerStyle::PRETTY>, class Clock = std::chrono::high_resolution_clock>
using folly::AutoTimer< Logger, Clock >::DoubleSeconds = std::chrono::duration<double>

Definition at line 65 of file AutoTimer.h.

Constructor & Destructor Documentation

template<class Logger = GoogleLogger<GoogleLoggerStyle::PRETTY>, class Clock = std::chrono::high_resolution_clock>
folly::AutoTimer< Logger, Clock >::AutoTimer ( std::string &&  msg = "",
const DoubleSeconds minTimetoLog = DoubleSeconds::zero(),
Logger &&  logger = Logger() 
)
inlineexplicit

Definition at line 67 of file AutoTimer.h.

72  minTimeToLog_(minTimetoLog),
73  logger_(std::move(logger)) {}
Optional< std::string > destructionMessage_
Definition: AutoTimer.h:116
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
DoubleSeconds minTimeToLog_
Definition: AutoTimer.h:118
template<class Logger = GoogleLogger<GoogleLoggerStyle::PRETTY>, class Clock = std::chrono::high_resolution_clock>
folly::AutoTimer< Logger, Clock >::AutoTimer ( const AutoTimer< Logger, Clock > &  )
delete
template<class Logger = GoogleLogger<GoogleLoggerStyle::PRETTY>, class Clock = std::chrono::high_resolution_clock>
folly::AutoTimer< Logger, Clock >::AutoTimer ( AutoTimer< Logger, Clock > &&  )
default
template<class Logger = GoogleLogger<GoogleLoggerStyle::PRETTY>, class Clock = std::chrono::high_resolution_clock>
folly::AutoTimer< Logger, Clock >::~AutoTimer ( )
inline

Definition at line 82 of file AutoTimer.h.

82  {
83  if (destructionMessage_) {
85  }
86  }
Optional< std::string > destructionMessage_
Definition: AutoTimer.h:116
DoubleSeconds log(StringPiece msg="")
Definition: AutoTimer.h:88
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268

Member Function Documentation

template<class Logger = GoogleLogger<GoogleLoggerStyle::PRETTY>, class Clock = std::chrono::high_resolution_clock>
DoubleSeconds folly::AutoTimer< Logger, Clock >::log ( StringPiece  msg = "")
inline

Definition at line 88 of file AutoTimer.h.

References testing::Args(), and now().

Referenced by TEST().

88  {
89  return logImpl(Clock::now(), msg);
90  }
std::chrono::steady_clock::time_point now()
DoubleSeconds logImpl(std::chrono::time_point< Clock > now, StringPiece msg)
Definition: AutoTimer.h:107
template<class Logger = GoogleLogger<GoogleLoggerStyle::PRETTY>, class Clock = std::chrono::high_resolution_clock>
template<typename... Args>
DoubleSeconds folly::AutoTimer< Logger, Clock >::log ( Args &&...  args)
inline

Definition at line 93 of file AutoTimer.h.

References testing::Args(), and now().

93  {
94  auto now = Clock::now();
95  return logImpl(now, to<std::string>(std::forward<Args>(args)...));
96  }
std::chrono::steady_clock::time_point now()
DoubleSeconds logImpl(std::chrono::time_point< Clock > now, StringPiece msg)
Definition: AutoTimer.h:107
template<class Logger = GoogleLogger<GoogleLoggerStyle::PRETTY>, class Clock = std::chrono::high_resolution_clock>
template<typename... Args>
DoubleSeconds folly::AutoTimer< Logger, Clock >::logFormat ( Args &&...  args)
inline

Definition at line 99 of file AutoTimer.h.

References folly::format(), and now().

Referenced by TEST().

99  {
100  auto now = Clock::now();
101  return logImpl(now, format(std::forward<Args>(args)...).str());
102  }
std::chrono::steady_clock::time_point now()
DoubleSeconds logImpl(std::chrono::time_point< Clock > now, StringPiece msg)
Definition: AutoTimer.h:107
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)
Definition: Format.h:271
template<class Logger = GoogleLogger<GoogleLoggerStyle::PRETTY>, class Clock = std::chrono::high_resolution_clock>
DoubleSeconds folly::AutoTimer< Logger, Clock >::logImpl ( std::chrono::time_point< Clock >  now,
StringPiece  msg 
)
inlineprivate

Definition at line 107 of file AutoTimer.h.

References now(), and start_.

107  {
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  }
std::chrono::steady_clock::time_point now()
DoubleSeconds minTimeToLog_
Definition: AutoTimer.h:118
std::chrono::time_point< Clock > start_
Definition: AutoTimer.h:117
template<class Logger = GoogleLogger<GoogleLoggerStyle::PRETTY>, class Clock = std::chrono::high_resolution_clock>
AutoTimer& folly::AutoTimer< Logger, Clock >::operator= ( const AutoTimer< Logger, Clock > &  )
delete
template<class Logger = GoogleLogger<GoogleLoggerStyle::PRETTY>, class Clock = std::chrono::high_resolution_clock>
AutoTimer& folly::AutoTimer< Logger, Clock >::operator= ( AutoTimer< Logger, Clock > &&  )
default

Member Data Documentation

template<class Logger = GoogleLogger<GoogleLoggerStyle::PRETTY>, class Clock = std::chrono::high_resolution_clock>
Optional<std::string> folly::AutoTimer< Logger, Clock >::destructionMessage_
private

Definition at line 116 of file AutoTimer.h.

template<class Logger = GoogleLogger<GoogleLoggerStyle::PRETTY>, class Clock = std::chrono::high_resolution_clock>
Logger folly::AutoTimer< Logger, Clock >::logger_
private

Definition at line 119 of file AutoTimer.h.

template<class Logger = GoogleLogger<GoogleLoggerStyle::PRETTY>, class Clock = std::chrono::high_resolution_clock>
DoubleSeconds folly::AutoTimer< Logger, Clock >::minTimeToLog_
private

Definition at line 118 of file AutoTimer.h.

template<class Logger = GoogleLogger<GoogleLoggerStyle::PRETTY>, class Clock = std::chrono::high_resolution_clock>
std::chrono::time_point<Clock> folly::AutoTimer< Logger, Clock >::start_ = Clock::now()
private

Definition at line 117 of file AutoTimer.h.


The documentation for this class was generated from the following file: