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