1 //===-- TimerTest.cpp -----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/StreamString.h"
10 #include "lldb/Utility/Timer.h"
11 #include "gtest/gtest.h"
14 using namespace lldb_private
;
16 TEST(TimerTest
, CategoryTimes
) {
17 Timer::ResetCategoryTimes();
19 static Timer::Category
tcat("CAT1");
21 std::this_thread::sleep_for(std::chrono::milliseconds(10));
24 Timer::DumpCategoryTimes(ss
);
26 ASSERT_EQ(1, sscanf(ss
.GetData(), "%lf sec for CAT1", &seconds
));
27 EXPECT_LT(0.001, seconds
);
28 EXPECT_GT(0.1, seconds
);
31 TEST(TimerTest
, CategoryTimesNested
) {
32 Timer::ResetCategoryTimes();
34 static Timer::Category
tcat1("CAT1");
36 std::this_thread::sleep_for(std::chrono::milliseconds(10));
37 // Explicitly testing the same category as above.
39 std::this_thread::sleep_for(std::chrono::milliseconds(10));
42 Timer::DumpCategoryTimes(ss
);
44 // It should only appear once.
45 ASSERT_EQ(ss
.GetString().count("CAT1"), 1U);
46 ASSERT_EQ(1, sscanf(ss
.GetData(), "%lf sec for CAT1", &seconds
));
47 EXPECT_LT(0.002, seconds
);
48 EXPECT_GT(0.2, seconds
);
51 TEST(TimerTest
, CategoryTimes2
) {
52 Timer::ResetCategoryTimes();
54 static Timer::Category
tcat1("CAT1");
56 std::this_thread::sleep_for(std::chrono::milliseconds(100));
57 static Timer::Category
tcat2("CAT2");
59 std::this_thread::sleep_for(std::chrono::milliseconds(10));
62 Timer::DumpCategoryTimes(ss
);
63 double seconds1
, seconds2
;
64 ASSERT_EQ(2, sscanf(ss
.GetData(),
65 "%lf sec (total: %*fs; child: %*fs; count: %*d) for "
66 "CAT1%*[\n ]%lf sec for CAT2",
67 &seconds1
, &seconds2
))
68 << "String: " << ss
.GetData();
69 EXPECT_LT(0.01, seconds1
);
70 EXPECT_GT(1, seconds1
);
71 EXPECT_LT(0.001, seconds2
);
72 EXPECT_GT(0.1, seconds2
);
75 TEST(TimerTest
, CategoryTimesStats
) {
76 Timer::ResetCategoryTimes();
78 static Timer::Category
tcat1("CAT1");
80 std::this_thread::sleep_for(std::chrono::milliseconds(100));
81 static Timer::Category
tcat2("CAT2");
84 std::this_thread::sleep_for(std::chrono::milliseconds(10));
88 std::this_thread::sleep_for(std::chrono::milliseconds(10));
92 // 0.105202764 sec (total: 0.132s; child: 0.027s; count: 1) for CAT1
93 // 0.026772798 sec (total: 0.027s; child: 0.000s; count: 2) for CAT2
95 Timer::DumpCategoryTimes(ss
);
96 double seconds1
, total1
, child1
, seconds2
;
99 6, sscanf(ss
.GetData(),
100 "%lf sec (total: %lfs; child: %lfs; count: %d) for CAT1%*[\n\r ]"
101 "%lf sec (total: %*fs; child: %*fs; count: %d) for CAT2",
102 &seconds1
, &total1
, &child1
, &count1
, &seconds2
, &count2
))
103 << "String: " << ss
.GetData();
104 EXPECT_NEAR(total1
- child1
, seconds1
, 0.002);
105 EXPECT_EQ(1, count1
);
106 EXPECT_NEAR(child1
, seconds2
, 0.002);
107 EXPECT_EQ(2, count2
);