1 // Copyright (c) 2018 Google LLC.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <unistd.h>
16 #include <sstream>
17
18 #include "gtest/gtest.h"
19 #include "source/util/timer.h"
20
21 namespace spvtools {
22 namespace utils {
23 namespace {
24
25 // A mock class to mimic Timer class for a testing purpose. It has fixed
26 // CPU/WALL/USR/SYS time, RSS delta, and the delta of the number of page faults.
27 class MockTimer : public Timer {
28 public:
MockTimer(std::ostream * out,bool measure_mem_usage=false)29 MockTimer(std::ostream* out, bool measure_mem_usage = false)
30 : Timer(out, measure_mem_usage) {}
CPUTime()31 double CPUTime() override { return 0.019123; }
WallTime()32 double WallTime() override { return 0.019723; }
UserTime()33 double UserTime() override { return 0.012723; }
SystemTime()34 double SystemTime() override { return 0.002723; }
RSS() const35 long RSS() const override { return 360L; }
PageFault() const36 long PageFault() const override { return 3600L; }
37 };
38
39 // This unit test checks whether the actual output of MockTimer::Report() is the
40 // same as fixed CPU/WALL/USR/SYS time, RSS delta, and the delta of the number
41 // of page faults that are returned by MockTimer.
TEST(MockTimer,DoNothing)42 TEST(MockTimer, DoNothing) {
43 std::ostringstream buf;
44
45 PrintTimerDescription(&buf);
46 MockTimer timer(&buf);
47 timer.Start();
48
49 // Do nothing.
50
51 timer.Stop();
52 timer.Report("TimerTest");
53
54 EXPECT_EQ(0.019123, timer.CPUTime());
55 EXPECT_EQ(0.019723, timer.WallTime());
56 EXPECT_EQ(0.012723, timer.UserTime());
57 EXPECT_EQ(0.002723, timer.SystemTime());
58 EXPECT_EQ(
59 " PASS name CPU time WALL time USR time"
60 " SYS time\n TimerTest 0.02 0.02"
61 " 0.01 0.00\n",
62 buf.str());
63 }
64
65 // This unit test checks whether the ScopedTimer<MockTimer> correctly reports
66 // the fixed CPU/WALL/USR/SYS time, RSS delta, and the delta of the number of
67 // page faults that are returned by MockTimer.
TEST(MockTimer,TestScopedTimer)68 TEST(MockTimer, TestScopedTimer) {
69 std::ostringstream buf;
70
71 {
72 ScopedTimer<MockTimer> scopedtimer(&buf, "ScopedTimerTest");
73 // Do nothing.
74 }
75
76 EXPECT_EQ(
77 " ScopedTimerTest 0.02 0.02 0.01"
78 " 0.00\n",
79 buf.str());
80 }
81
82 // A mock class to mimic CumulativeTimer class for a testing purpose. It has
83 // fixed CPU/WALL/USR/SYS time, RSS delta, and the delta of the number of page
84 // faults for each measurement (i.e., a pair of Start() and Stop()). If the
85 // number of measurements increases, it increases |count_stop_| by the number of
86 // calling Stop() and the amount of each resource usage is proportional to
87 // |count_stop_|.
88 class MockCumulativeTimer : public CumulativeTimer {
89 public:
MockCumulativeTimer(std::ostream * out,bool measure_mem_usage=false)90 MockCumulativeTimer(std::ostream* out, bool measure_mem_usage = false)
91 : CumulativeTimer(out, measure_mem_usage), count_stop_(0) {}
CPUTime()92 double CPUTime() override { return count_stop_ * 0.019123; }
WallTime()93 double WallTime() override { return count_stop_ * 0.019723; }
UserTime()94 double UserTime() override { return count_stop_ * 0.012723; }
SystemTime()95 double SystemTime() override { return count_stop_ * 0.002723; }
RSS() const96 long RSS() const override { return count_stop_ * 360L; }
PageFault() const97 long PageFault() const override { return count_stop_ * 3600L; }
98
99 // Calling Stop() does nothing but just increases |count_stop_| by 1.
Stop()100 void Stop() override { ++count_stop_; }
101
102 private:
103 unsigned int count_stop_;
104 };
105
106 // This unit test checks whether the MockCumulativeTimer correctly reports the
107 // cumulative CPU/WALL/USR/SYS time, RSS delta, and the delta of the number of
108 // page faults whose values are fixed for each measurement (i.e., a pair of
109 // Start() and Stop()).
TEST(MockCumulativeTimer,DoNothing)110 TEST(MockCumulativeTimer, DoNothing) {
111 CumulativeTimer* ctimer;
112 std::ostringstream buf;
113
114 {
115 ctimer = new MockCumulativeTimer(&buf);
116 ctimer->Start();
117
118 // Do nothing.
119
120 ctimer->Stop();
121 }
122
123 {
124 ctimer->Start();
125
126 // Do nothing.
127
128 ctimer->Stop();
129 ctimer->Report("CumulativeTimerTest");
130 }
131
132 EXPECT_EQ(
133 " CumulativeTimerTest 0.04 0.04 0.03"
134 " 0.01\n",
135 buf.str());
136
137 if (ctimer) delete ctimer;
138 }
139
140 } // namespace
141 } // namespace utils
142 } // namespace spvtools
143