proxygen
AutoTimerTest.cpp File Reference

Go to the source code of this file.

Classes

struct  StubLogger
 
struct  StubClock
 

Functions

 TEST (TestAutoTimer, HandleBasicClosure)
 
 TEST (TestAutoTimer, HandleBasic)
 
 TEST (TestAutoTimer, HandleLogOnDestruct)
 
 TEST (TestAutoTimer, HandleRealTimerClosure)
 
 TEST (TestAutoTimer, HandleRealTimer)
 
 TEST (TestAutoTimer, HandleMinLogTime)
 
 TEST (TestAutoTimer, MovedObjectDestructionDoesntLog)
 

Function Documentation

TEST ( TestAutoTimer  ,
HandleBasicClosure   
)

Definition at line 47 of file AutoTimerTest.cpp.

References ASSERT_EQ, StubLogger::m, folly::gen::move, StubLogger::t, and StubClock::t.

47  {
48  auto logger = [](StringPiece mesg, auto sec) {
49  return StubLogger()(mesg, sec);
50  };
51  StubClock::t = 1;
52  // Here decltype is needed. But since most users are expected to use this
53  // method with the default clock, template specification won't be needed even
54  // when they use a closure. See test case HandleRealTimerClosure
55  auto timer = makeAutoTimer<decltype(logger), StubClock>(
56  "", std::chrono::duration<double>::zero(), std::move(logger));
57  StubClock::t = 3;
58  timer.log("foo");
59  ASSERT_EQ("foo", StubLogger::m);
61  timer.logFormat("bar {}", 5e-2);
62  ASSERT_EQ("bar 0.05", StubLogger::m);
64 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
static int t
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
static double t
static std::string m
TEST ( TestAutoTimer  ,
HandleBasic   
)

Definition at line 66 of file AutoTimerTest.cpp.

References ASSERT_EQ, folly::AutoTimer< Logger, Clock >::log(), folly::AutoTimer< Logger, Clock >::logFormat(), StubLogger::m, StubLogger::t, and StubClock::t.

66  {
67  StubClock::t = 1;
69  StubClock::t = 3;
70  timer.log("foo");
71  ASSERT_EQ("foo", StubLogger::m);
73  timer.logFormat("bar {}", 5e-2);
74  ASSERT_EQ("bar 0.05", StubLogger::m);
76 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
static int t
DoubleSeconds logFormat(Args &&...args)
Definition: AutoTimer.h:99
static double t
DoubleSeconds log(StringPiece msg="")
Definition: AutoTimer.h:88
static std::string m
TEST ( TestAutoTimer  ,
HandleLogOnDestruct   
)

Definition at line 78 of file AutoTimerTest.cpp.

References ASSERT_EQ, EXPECT_EQ, folly::AutoTimer< Logger, Clock >::log(), StubLogger::m, StubLogger::t, and StubClock::t.

78  {
79  {
80  StubClock::t = 0;
81  AutoTimer<StubLogger, StubClock> timer("message");
82  StubClock::t = 3;
83  timer.log("foo");
84  EXPECT_EQ("foo", StubLogger::m);
86  StubClock::t = 5;
87  }
88  ASSERT_EQ("message", StubLogger::m);
90 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
static int t
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static double t
static std::string m
TEST ( TestAutoTimer  ,
HandleRealTimerClosure   
)

Definition at line 92 of file AutoTimerTest.cpp.

References folly::makeAutoTimer(), and folly::pushmi::detail::t.

92  {
93  auto t = makeAutoTimer(
94  "Third message on destruction",
95  std::chrono::duration<double>::zero(),
96  [](StringPiece mesg, auto sec) {
98  });
99  t.log("First message");
100  t.log("Second message");
101 }
auto makeAutoTimer(std::string &&msg="", const std::chrono::duration< double > &minTimeToLog=std::chrono::duration< double >::zero(), Logger &&logger=Logger())
Definition: AutoTimer.h:125
TEST ( TestAutoTimer  ,
HandleRealTimer   
)

Definition at line 103 of file AutoTimerTest.cpp.

References folly::AutoTimer< Logger, Clock >::log(), and folly::pushmi::detail::t.

103  {
104  AutoTimer<> t("Third message on destruction");
105  t.log("First message");
106  t.log("Second message");
107 }
TEST ( TestAutoTimer  ,
HandleMinLogTime   
)

Definition at line 109 of file AutoTimerTest.cpp.

References ASSERT_EQ, count, folly::AutoTimer< Logger, Clock >::log(), StubLogger::t, and StubClock::t.

109  {
110  StubClock::t = 1;
111  AutoTimer<StubLogger, StubClock> timer("", std::chrono::duration<double>(3));
112  StubClock::t = 3;
113  // only 2 "seconds" have passed, so this shouldn't log
114  StubLogger::t = 0;
115  ASSERT_EQ(std::chrono::duration<double>(2), timer.log("foo"));
116  ASSERT_EQ(std::chrono::duration<double>::zero().count(), StubLogger::t);
117 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
static int t
static double t
int * count
TEST ( TestAutoTimer  ,
MovedObjectDestructionDoesntLog   
)

Definition at line 119 of file AutoTimerTest.cpp.

References current, EXPECT_EQ, int32_t, folly::makeAutoTimer(), folly::gen::move, and SCOPE_EXIT.

119  {
120  const std::vector<std::string> expectedMsgs = {
121  "BEFORE_MOVE", "AFTER_MOVE", "END"};
122  int32_t current = 0;
123  SCOPE_EXIT {
124  EXPECT_EQ(3, current);
125  };
126 
127  auto timer = [&expectedMsgs, &current] {
128  auto oldTimer = folly::makeAutoTimer(
129  "END",
130  std::chrono::duration<double>::zero(),
131  [&expectedMsgs, &current](
132  StringPiece msg, const std::chrono::duration<double>&) {
133  EXPECT_EQ(expectedMsgs.at(current), msg);
134  current++;
135  });
136  oldTimer.log("BEFORE_MOVE");
137  auto newTimer = std::move(oldTimer); // force the move-ctor
138  return newTimer;
139  }();
140  timer.log("AFTER_MOVE");
141 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
int current
auto makeAutoTimer(std::string &&msg="", const std::chrono::duration< double > &minTimeToLog=std::chrono::duration< double >::zero(), Logger &&logger=Logger())
Definition: AutoTimer.h:125