1 //===-- TimerTest.cpp -------------------------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Utility/StreamString.h" 10 #include "lldb/Utility/Timer.h" 11 #include "gtest/gtest.h" 12 #include <thread> 13 14 using namespace lldb_private; 15 16 TEST(TimerTest, CategoryTimes) { 17 Timer::ResetCategoryTimes(); 18 { 19 static Timer::Category tcat("CAT1"); 20 Timer t(tcat, "."); 21 std::this_thread::sleep_for(std::chrono::milliseconds(10)); 22 } 23 StreamString ss; 24 Timer::DumpCategoryTimes(&ss); 25 double seconds; 26 ASSERT_EQ(1, sscanf(ss.GetData(), "%lf sec for CAT1", &seconds)); 27 EXPECT_LT(0.001, seconds); 28 EXPECT_GT(0.1, seconds); 29 } 30 31 TEST(TimerTest, CategoryTimesNested) { 32 Timer::ResetCategoryTimes(); 33 { 34 static Timer::Category tcat1("CAT1"); 35 Timer t1(tcat1, "."); 36 std::this_thread::sleep_for(std::chrono::milliseconds(10)); 37 // Explicitly testing the same category as above. 38 Timer t2(tcat1, "."); 39 std::this_thread::sleep_for(std::chrono::milliseconds(10)); 40 } 41 StreamString ss; 42 Timer::DumpCategoryTimes(&ss); 43 double seconds; 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); 49 } 50 51 TEST(TimerTest, CategoryTimes2) { 52 Timer::ResetCategoryTimes(); 53 { 54 static Timer::Category tcat1("CAT1"); 55 Timer t1(tcat1, "."); 56 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 57 static Timer::Category tcat2("CAT2"); 58 Timer t2(tcat2, "."); 59 std::this_thread::sleep_for(std::chrono::milliseconds(10)); 60 } 61 StreamString ss; 62 Timer::DumpCategoryTimes(&ss); 63 double seconds1, seconds2; 64 ASSERT_EQ(2, sscanf(ss.GetData(), "%lf sec for CAT1%*[\n ]%lf sec for CAT2", 65 &seconds1, &seconds2)) 66 << "String: " << ss.GetData(); 67 EXPECT_LT(0.01, seconds1); 68 EXPECT_GT(1, seconds1); 69 EXPECT_LT(0.001, seconds2); 70 EXPECT_GT(0.1, seconds2); 71 } 72