]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - unittests/Core/TimerTest.cpp
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / unittests / Core / TimerTest.cpp
1 //===-- TimerTest.cpp -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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.
13 #include <eh.h>
14 #endif
15
16 #include "lldb/Core/Timer.h"
17 #include "gtest/gtest.h"
18
19 #include "lldb/Core/StreamString.h"
20 #include <thread>
21
22 using namespace lldb_private;
23
24 TEST(TimerTest, CategoryTimes) {
25   Timer::ResetCategoryTimes();
26   {
27     Timer t("CAT1", "");
28     std::this_thread::sleep_for(std::chrono::milliseconds(10));
29   }
30   StreamString ss;
31   Timer::DumpCategoryTimes(&ss);
32   double seconds;
33   ASSERT_EQ(1, sscanf(ss.GetData(), "%lf sec for CAT1", &seconds));
34   EXPECT_LT(0.001, seconds);
35   EXPECT_GT(0.1, seconds);
36 }
37
38 TEST(TimerTest, CategoryTimesNested) {
39   Timer::ResetCategoryTimes();
40   {
41     Timer t1("CAT1", "");
42     std::this_thread::sleep_for(std::chrono::milliseconds(10));
43     Timer t2("CAT1", "");
44     std::this_thread::sleep_for(std::chrono::milliseconds(10));
45   }
46   StreamString ss;
47   Timer::DumpCategoryTimes(&ss);
48   double seconds;
49   ASSERT_EQ(1, sscanf(ss.GetData(), "%lf sec for CAT1", &seconds));
50   EXPECT_LT(0.002, seconds);
51   EXPECT_GT(0.2, seconds);
52 }
53
54 TEST(TimerTest, CategoryTimes2) {
55   Timer::ResetCategoryTimes();
56   {
57     Timer t1("CAT1", "");
58     std::this_thread::sleep_for(std::chrono::milliseconds(100));
59     Timer t2("CAT2", "");
60     std::this_thread::sleep_for(std::chrono::milliseconds(10));
61   }
62   StreamString ss;
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);
72 }