1 //===-- TimerTest.cpp -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #if defined(_MSC_VER) && (_HAS_EXCEPTIONS == 0)
11 // Workaround for MSVC standard library bug, which fails to include <thread>
12 // when exceptions are disabled.
16 #include "lldb/Core/Timer.h"
17 #include "gtest/gtest.h"
19 #include "lldb/Core/StreamString.h"
22 using namespace lldb_private;
24 TEST(TimerTest, CategoryTimes) {
25 Timer::ResetCategoryTimes();
28 std::this_thread::sleep_for(std::chrono::milliseconds(10));
31 Timer::DumpCategoryTimes(&ss);
33 ASSERT_EQ(1, sscanf(ss.GetData(), "%lf sec for CAT1", &seconds));
34 EXPECT_LT(0.001, seconds);
35 EXPECT_GT(0.1, seconds);
38 TEST(TimerTest, CategoryTimesNested) {
39 Timer::ResetCategoryTimes();
42 std::this_thread::sleep_for(std::chrono::milliseconds(10));
44 std::this_thread::sleep_for(std::chrono::milliseconds(10));
47 Timer::DumpCategoryTimes(&ss);
49 ASSERT_EQ(1, sscanf(ss.GetData(), "%lf sec for CAT1", &seconds));
50 EXPECT_LT(0.002, seconds);
51 EXPECT_GT(0.2, seconds);
54 TEST(TimerTest, CategoryTimes2) {
55 Timer::ResetCategoryTimes();
58 std::this_thread::sleep_for(std::chrono::milliseconds(100));
60 std::this_thread::sleep_for(std::chrono::milliseconds(10));
63 Timer::DumpCategoryTimes(&ss);
64 double seconds1, seconds2;
65 ASSERT_EQ(2, sscanf(ss.GetData(), "%lf sec for CAT1%*[\n ]%lf sec for CAT2",
66 &seconds1, &seconds2))
67 << "String: " << ss.GetData();
68 EXPECT_LT(0.01, seconds1);
69 EXPECT_GT(1, seconds1);
70 EXPECT_LT(0.001, seconds2);
71 EXPECT_GT(0.1, seconds2);