2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <gtest/gtest.h>
18 #include <folly/experimental/AutoTimer.h>
20 using namespace folly;
24 void operator()(StringPiece msg, double sec) {
32 StringPiece StubLogger::m = "";
33 double StubLogger::t = 0;
36 typedef std::chrono::seconds duration;
38 static std::chrono::time_point<StubClock> now() {
39 return std::chrono::time_point<StubClock>(std::chrono::duration<int>(t));
46 TEST(TestAutoTimer, HandleBasic) {
48 AutoTimer<StubLogger, StubClock> timer;
51 ASSERT_EQ("foo", StubLogger::m);
52 ASSERT_EQ(2, StubLogger::t);
53 timer.logFormat("bar {}", 5e-2);
54 ASSERT_EQ("bar 0.05", StubLogger::m);
55 ASSERT_EQ(0, StubLogger::t);
58 TEST(TestAutoTimer, HandleLogOnDestruct) {
61 AutoTimer<StubLogger, StubClock> timer("message");
64 EXPECT_EQ("foo", StubLogger::m);
65 EXPECT_EQ(3, StubLogger::t);
68 ASSERT_EQ("message", StubLogger::m);
69 ASSERT_EQ(2, StubLogger::t);
72 TEST(TestAutoTimer, HandleRealTimer) {
73 AutoTimer<> t("Third message on destruction");
74 t.log("First message");
75 t.log("Second message");
78 TEST(TestAutoTimer, HandleMinLogTime) {
80 AutoTimer<StubLogger, StubClock> timer;
81 timer.setMinTimeToLog(3);
83 // only 2 "seconds" have passed, so this shouldn't log
85 ASSERT_EQ(2.0, timer.log("foo"));
86 ASSERT_EQ(0, StubLogger::t);