180814287SRaphael Isemann //===-- TimerTest.cpp -----------------------------------------------------===//
238d0632eSPavel Labath //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
638d0632eSPavel Labath //
738d0632eSPavel Labath //===----------------------------------------------------------------------===//
838d0632eSPavel Labath
938d0632eSPavel Labath #include "lldb/Utility/StreamString.h"
1038d0632eSPavel Labath #include "lldb/Utility/Timer.h"
1138d0632eSPavel Labath #include "gtest/gtest.h"
1238d0632eSPavel Labath #include <thread>
1338d0632eSPavel Labath
1438d0632eSPavel Labath using namespace lldb_private;
1538d0632eSPavel Labath
TEST(TimerTest,CategoryTimes)1638d0632eSPavel Labath TEST(TimerTest, CategoryTimes) {
1738d0632eSPavel Labath Timer::ResetCategoryTimes();
1838d0632eSPavel Labath {
1938d0632eSPavel Labath static Timer::Category tcat("CAT1");
2042d9cd2dSMichal Gorny Timer t(tcat, ".");
2138d0632eSPavel Labath std::this_thread::sleep_for(std::chrono::milliseconds(10));
2238d0632eSPavel Labath }
2338d0632eSPavel Labath StreamString ss;
24*5f69e668SAlex Langford Timer::DumpCategoryTimes(ss);
2538d0632eSPavel Labath double seconds;
2638d0632eSPavel Labath ASSERT_EQ(1, sscanf(ss.GetData(), "%lf sec for CAT1", &seconds));
2738d0632eSPavel Labath EXPECT_LT(0.001, seconds);
2838d0632eSPavel Labath EXPECT_GT(0.1, seconds);
2938d0632eSPavel Labath }
3038d0632eSPavel Labath
TEST(TimerTest,CategoryTimesNested)3138d0632eSPavel Labath TEST(TimerTest, CategoryTimesNested) {
3238d0632eSPavel Labath Timer::ResetCategoryTimes();
3338d0632eSPavel Labath {
3438d0632eSPavel Labath static Timer::Category tcat1("CAT1");
3542d9cd2dSMichal Gorny Timer t1(tcat1, ".");
3638d0632eSPavel Labath std::this_thread::sleep_for(std::chrono::milliseconds(10));
3738d0632eSPavel Labath // Explicitly testing the same category as above.
3842d9cd2dSMichal Gorny Timer t2(tcat1, ".");
3938d0632eSPavel Labath std::this_thread::sleep_for(std::chrono::milliseconds(10));
4038d0632eSPavel Labath }
4138d0632eSPavel Labath StreamString ss;
42*5f69e668SAlex Langford Timer::DumpCategoryTimes(ss);
4338d0632eSPavel Labath double seconds;
4438d0632eSPavel Labath // It should only appear once.
4538d0632eSPavel Labath ASSERT_EQ(ss.GetString().count("CAT1"), 1U);
4638d0632eSPavel Labath ASSERT_EQ(1, sscanf(ss.GetData(), "%lf sec for CAT1", &seconds));
4738d0632eSPavel Labath EXPECT_LT(0.002, seconds);
4838d0632eSPavel Labath EXPECT_GT(0.2, seconds);
4938d0632eSPavel Labath }
5038d0632eSPavel Labath
TEST(TimerTest,CategoryTimes2)5138d0632eSPavel Labath TEST(TimerTest, CategoryTimes2) {
5238d0632eSPavel Labath Timer::ResetCategoryTimes();
5338d0632eSPavel Labath {
5438d0632eSPavel Labath static Timer::Category tcat1("CAT1");
5542d9cd2dSMichal Gorny Timer t1(tcat1, ".");
5638d0632eSPavel Labath std::this_thread::sleep_for(std::chrono::milliseconds(100));
5738d0632eSPavel Labath static Timer::Category tcat2("CAT2");
5842d9cd2dSMichal Gorny Timer t2(tcat2, ".");
5938d0632eSPavel Labath std::this_thread::sleep_for(std::chrono::milliseconds(10));
6038d0632eSPavel Labath }
6138d0632eSPavel Labath StreamString ss;
62*5f69e668SAlex Langford Timer::DumpCategoryTimes(ss);
6338d0632eSPavel Labath double seconds1, seconds2;
6478337420SAntonio Afonso ASSERT_EQ(2, sscanf(ss.GetData(),
657316670eSAntonio Afonso "%lf sec (total: %*fs; child: %*fs; count: %*d) for "
6678337420SAntonio Afonso "CAT1%*[\n ]%lf sec for CAT2",
6738d0632eSPavel Labath &seconds1, &seconds2))
6838d0632eSPavel Labath << "String: " << ss.GetData();
6938d0632eSPavel Labath EXPECT_LT(0.01, seconds1);
7038d0632eSPavel Labath EXPECT_GT(1, seconds1);
7138d0632eSPavel Labath EXPECT_LT(0.001, seconds2);
7238d0632eSPavel Labath EXPECT_GT(0.1, seconds2);
7338d0632eSPavel Labath }
7478337420SAntonio Afonso
TEST(TimerTest,CategoryTimesStats)7578337420SAntonio Afonso TEST(TimerTest, CategoryTimesStats) {
7678337420SAntonio Afonso Timer::ResetCategoryTimes();
7778337420SAntonio Afonso {
7878337420SAntonio Afonso static Timer::Category tcat1("CAT1");
7978337420SAntonio Afonso Timer t1(tcat1, ".");
8078337420SAntonio Afonso std::this_thread::sleep_for(std::chrono::milliseconds(100));
8178337420SAntonio Afonso static Timer::Category tcat2("CAT2");
8278337420SAntonio Afonso {
8378337420SAntonio Afonso Timer t2(tcat2, ".");
8478337420SAntonio Afonso std::this_thread::sleep_for(std::chrono::milliseconds(10));
8578337420SAntonio Afonso }
8678337420SAntonio Afonso {
8778337420SAntonio Afonso Timer t3(tcat2, ".");
8878337420SAntonio Afonso std::this_thread::sleep_for(std::chrono::milliseconds(10));
8978337420SAntonio Afonso }
9078337420SAntonio Afonso }
9178337420SAntonio Afonso // Example output:
9278337420SAntonio Afonso // 0.105202764 sec (total: 0.132s; child: 0.027s; count: 1) for CAT1
9378337420SAntonio Afonso // 0.026772798 sec (total: 0.027s; child: 0.000s; count: 2) for CAT2
9478337420SAntonio Afonso StreamString ss;
95*5f69e668SAlex Langford Timer::DumpCategoryTimes(ss);
9678337420SAntonio Afonso double seconds1, total1, child1, seconds2;
9778337420SAntonio Afonso int count1, count2;
9878337420SAntonio Afonso ASSERT_EQ(
9978337420SAntonio Afonso 6, sscanf(ss.GetData(),
1007ddfb956SAdrian McCarthy "%lf sec (total: %lfs; child: %lfs; count: %d) for CAT1%*[\n\r ]"
1017316670eSAntonio Afonso "%lf sec (total: %*fs; child: %*fs; count: %d) for CAT2",
10278337420SAntonio Afonso &seconds1, &total1, &child1, &count1, &seconds2, &count2))
10378337420SAntonio Afonso << "String: " << ss.GetData();
10478337420SAntonio Afonso EXPECT_NEAR(total1 - child1, seconds1, 0.002);
10578337420SAntonio Afonso EXPECT_EQ(1, count1);
10678337420SAntonio Afonso EXPECT_NEAR(child1, seconds2, 0.002);
10778337420SAntonio Afonso EXPECT_EQ(2, count2);
10878337420SAntonio Afonso }
109