timer_test.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include <unistd.h>
  15. #include <sstream>
  16. #include "gtest/gtest.h"
  17. #include "source/util/timer.h"
  18. namespace spvtools {
  19. namespace utils {
  20. namespace {
  21. // A mock class to mimic Timer class for a testing purpose. It has fixed
  22. // CPU/WALL/USR/SYS time, RSS delta, and the delta of the number of page faults.
  23. class MockTimer : public Timer {
  24. public:
  25. MockTimer(std::ostream* out, bool measure_mem_usage = false)
  26. : Timer(out, measure_mem_usage) {}
  27. double CPUTime() override { return 0.019123; }
  28. double WallTime() override { return 0.019723; }
  29. double UserTime() override { return 0.012723; }
  30. double SystemTime() override { return 0.002723; }
  31. long RSS() const override { return 360L; }
  32. long PageFault() const override { return 3600L; }
  33. };
  34. // This unit test checks whether the actual output of MockTimer::Report() is the
  35. // same as fixed CPU/WALL/USR/SYS time, RSS delta, and the delta of the number
  36. // of page faults that are returned by MockTimer.
  37. TEST(MockTimer, DoNothing) {
  38. std::ostringstream buf;
  39. PrintTimerDescription(&buf);
  40. MockTimer timer(&buf);
  41. timer.Start();
  42. // Do nothing.
  43. timer.Stop();
  44. timer.Report("TimerTest");
  45. EXPECT_EQ(0.019123, timer.CPUTime());
  46. EXPECT_EQ(0.019723, timer.WallTime());
  47. EXPECT_EQ(0.012723, timer.UserTime());
  48. EXPECT_EQ(0.002723, timer.SystemTime());
  49. EXPECT_EQ(
  50. " PASS name CPU time WALL time USR time"
  51. " SYS time\n TimerTest 0.02 0.02"
  52. " 0.01 0.00\n",
  53. buf.str());
  54. }
  55. // This unit test checks whether the ScopedTimer<MockTimer> correctly reports
  56. // the fixed CPU/WALL/USR/SYS time, RSS delta, and the delta of the number of
  57. // page faults that are returned by MockTimer.
  58. TEST(MockTimer, TestScopedTimer) {
  59. std::ostringstream buf;
  60. {
  61. ScopedTimer<MockTimer> scopedtimer(&buf, "ScopedTimerTest");
  62. // Do nothing.
  63. }
  64. EXPECT_EQ(
  65. " ScopedTimerTest 0.02 0.02 0.01"
  66. " 0.00\n",
  67. buf.str());
  68. }
  69. // A mock class to mimic CumulativeTimer class for a testing purpose. It has
  70. // fixed CPU/WALL/USR/SYS time, RSS delta, and the delta of the number of page
  71. // faults for each measurement (i.e., a pair of Start() and Stop()). If the
  72. // number of measurements increases, it increases |count_stop_| by the number of
  73. // calling Stop() and the amount of each resource usage is proportional to
  74. // |count_stop_|.
  75. class MockCumulativeTimer : public CumulativeTimer {
  76. public:
  77. MockCumulativeTimer(std::ostream* out, bool measure_mem_usage = false)
  78. : CumulativeTimer(out, measure_mem_usage), count_stop_(0) {}
  79. double CPUTime() override { return count_stop_ * 0.019123; }
  80. double WallTime() override { return count_stop_ * 0.019723; }
  81. double UserTime() override { return count_stop_ * 0.012723; }
  82. double SystemTime() override { return count_stop_ * 0.002723; }
  83. long RSS() const override { return count_stop_ * 360L; }
  84. long PageFault() const override { return count_stop_ * 3600L; }
  85. // Calling Stop() does nothing but just increases |count_stop_| by 1.
  86. void Stop() override { ++count_stop_; };
  87. private:
  88. unsigned int count_stop_;
  89. };
  90. // This unit test checks whether the MockCumulativeTimer correctly reports the
  91. // cumulative CPU/WALL/USR/SYS time, RSS delta, and the delta of the number of
  92. // page faults whose values are fixed for each measurement (i.e., a pair of
  93. // Start() and Stop()).
  94. TEST(MockCumulativeTimer, DoNothing) {
  95. CumulativeTimer* ctimer;
  96. std::ostringstream buf;
  97. {
  98. ctimer = new MockCumulativeTimer(&buf);
  99. ctimer->Start();
  100. // Do nothing.
  101. ctimer->Stop();
  102. }
  103. {
  104. ctimer->Start();
  105. // Do nothing.
  106. ctimer->Stop();
  107. ctimer->Report("CumulativeTimerTest");
  108. }
  109. EXPECT_EQ(
  110. " CumulativeTimerTest 0.04 0.04 0.03"
  111. " 0.01\n",
  112. buf.str());
  113. if (ctimer) delete ctimer;
  114. }
  115. } // namespace
  116. } // namespace utils
  117. } // namespace spvtools