1d167586aSVedant Kumar //===- unittests/TimerTest.cpp - Timer tests ------------------------------===//
2d167586aSVedant Kumar //
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
6d167586aSVedant Kumar //
7d167586aSVedant Kumar //===----------------------------------------------------------------------===//
8d167586aSVedant Kumar
9d167586aSVedant Kumar #include "llvm/Support/Timer.h"
10d167586aSVedant Kumar #include "gtest/gtest.h"
1107e602e6SNico Weber
12712e8d29SNico Weber #if _WIN32
1307e602e6SNico Weber #include <windows.h>
1407e602e6SNico Weber #else
1591d3cfedSDuncan P. N. Exon Smith #include <time.h>
1607e602e6SNico Weber #endif
17d167586aSVedant Kumar
18d167586aSVedant Kumar using namespace llvm;
19d167586aSVedant Kumar
20d167586aSVedant Kumar namespace {
21d167586aSVedant Kumar
2207e602e6SNico Weber // FIXME: Put this somewhere in Support, it's also used in LockFileManager.
SleepMS()2307e602e6SNico Weber void SleepMS() {
24712e8d29SNico Weber #if _WIN32
2507e602e6SNico Weber Sleep(1);
2607e602e6SNico Weber #else
2707e602e6SNico Weber struct timespec Interval;
2807e602e6SNico Weber Interval.tv_sec = 0;
2907e602e6SNico Weber Interval.tv_nsec = 1000000;
3007e602e6SNico Weber nanosleep(&Interval, nullptr);
3107e602e6SNico Weber #endif
3207e602e6SNico Weber }
3307e602e6SNico Weber
TEST(Timer,Additivity)34d167586aSVedant Kumar TEST(Timer, Additivity) {
359f15a79eSMatthias Braun Timer T1("T1", "T1");
36d167586aSVedant Kumar
37d167586aSVedant Kumar EXPECT_TRUE(T1.isInitialized());
38d167586aSVedant Kumar
39d167586aSVedant Kumar T1.startTimer();
40d167586aSVedant Kumar T1.stopTimer();
41d167586aSVedant Kumar auto TR1 = T1.getTotalTime();
42d167586aSVedant Kumar
43d167586aSVedant Kumar T1.startTimer();
4407e602e6SNico Weber SleepMS();
45d167586aSVedant Kumar T1.stopTimer();
46d167586aSVedant Kumar auto TR2 = T1.getTotalTime();
47d167586aSVedant Kumar
48*38ac4093SArchibald Elliott EXPECT_LT(TR1, TR2);
49d167586aSVedant Kumar }
50d167586aSVedant Kumar
TEST(Timer,CheckIfTriggered)51d167586aSVedant Kumar TEST(Timer, CheckIfTriggered) {
529f15a79eSMatthias Braun Timer T1("T1", "T1");
53d167586aSVedant Kumar
54d167586aSVedant Kumar EXPECT_FALSE(T1.hasTriggered());
55d167586aSVedant Kumar T1.startTimer();
56d167586aSVedant Kumar EXPECT_TRUE(T1.hasTriggered());
57d167586aSVedant Kumar T1.stopTimer();
58d167586aSVedant Kumar EXPECT_TRUE(T1.hasTriggered());
59d167586aSVedant Kumar
60d167586aSVedant Kumar T1.clear();
61d167586aSVedant Kumar EXPECT_FALSE(T1.hasTriggered());
62d167586aSVedant Kumar }
63d167586aSVedant Kumar
6491d3cfedSDuncan P. N. Exon Smith } // end anon namespace
65