1 //===- unittests/TimerTest.cpp - Timer tests ------------------------------===// 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 "llvm/Support/Timer.h" 10 #include "gtest/gtest.h" 11 12 #if _WIN32 13 #include <windows.h> 14 #else 15 #include <time.h> 16 #endif 17 18 using namespace llvm; 19 20 namespace { 21 22 // FIXME: Put this somewhere in Support, it's also used in LockFileManager. 23 void SleepMS() { 24 #if _WIN32 25 Sleep(1); 26 #else 27 struct timespec Interval; 28 Interval.tv_sec = 0; 29 Interval.tv_nsec = 1000000; 30 #if defined(__MVS__) 31 long Microseconds = (Interval.tv_nsec + 999) / 1000; 32 usleep(Microseconds); 33 #else 34 nanosleep(&Interval, nullptr); 35 #endif 36 #endif 37 } 38 39 TEST(Timer, Additivity) { 40 Timer T1("T1", "T1"); 41 42 EXPECT_TRUE(T1.isInitialized()); 43 44 T1.startTimer(); 45 T1.stopTimer(); 46 auto TR1 = T1.getTotalTime(); 47 48 T1.startTimer(); 49 SleepMS(); 50 T1.stopTimer(); 51 auto TR2 = T1.getTotalTime(); 52 53 EXPECT_LT(TR1, TR2); 54 } 55 56 TEST(Timer, CheckIfTriggered) { 57 Timer T1("T1", "T1"); 58 59 EXPECT_FALSE(T1.hasTriggered()); 60 T1.startTimer(); 61 EXPECT_TRUE(T1.hasTriggered()); 62 T1.stopTimer(); 63 EXPECT_TRUE(T1.hasTriggered()); 64 65 T1.clear(); 66 EXPECT_FALSE(T1.hasTriggered()); 67 } 68 69 } // end anon namespace 70