You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/cpu_time.h"
  11. #include "rtc_base/platform_thread.h"
  12. #include "rtc_base/time_utils.h"
  13. #include "system_wrappers/include/sleep.h"
  14. #include "test/gtest.h"
  15. // Only run these tests on non-instrumented builds, because timing on
  16. // instrumented builds is unreliable, causing the test to be flaky.
  17. #if defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \
  18. defined(ADDRESS_SANITIZER)
  19. #define MAYBE_TEST(test_name) DISABLED_##test_name
  20. #else
  21. #define MAYBE_TEST(test_name) test_name
  22. #endif
  23. namespace {
  24. const int kAllowedErrorMillisecs = 30;
  25. const int kProcessingTimeMillisecs = 300;
  26. const int kWorkingThreads = 2;
  27. // Consumes approximately kProcessingTimeMillisecs of CPU time in single thread.
  28. void WorkingFunction(void* counter_pointer) {
  29. int64_t* counter = reinterpret_cast<int64_t*>(counter_pointer);
  30. *counter = 0;
  31. int64_t stop_cpu_time =
  32. rtc::GetThreadCpuTimeNanos() +
  33. kProcessingTimeMillisecs * rtc::kNumNanosecsPerMillisec;
  34. while (rtc::GetThreadCpuTimeNanos() < stop_cpu_time) {
  35. (*counter)++;
  36. }
  37. }
  38. } // namespace
  39. namespace rtc {
  40. // A minimal test which can be run on instrumented builds, so that they're at
  41. // least exercising the code to check for memory leaks/etc.
  42. TEST(CpuTimeTest, BasicTest) {
  43. int64_t process_start_time_nanos = GetProcessCpuTimeNanos();
  44. int64_t thread_start_time_nanos = GetThreadCpuTimeNanos();
  45. int64_t process_duration_nanos =
  46. GetProcessCpuTimeNanos() - process_start_time_nanos;
  47. int64_t thread_duration_nanos =
  48. GetThreadCpuTimeNanos() - thread_start_time_nanos;
  49. EXPECT_GE(process_duration_nanos, 0);
  50. EXPECT_GE(thread_duration_nanos, 0);
  51. }
  52. TEST(CpuTimeTest, MAYBE_TEST(TwoThreads)) {
  53. int64_t process_start_time_nanos = GetProcessCpuTimeNanos();
  54. int64_t thread_start_time_nanos = GetThreadCpuTimeNanos();
  55. int64_t counter1;
  56. int64_t counter2;
  57. PlatformThread thread1(WorkingFunction, reinterpret_cast<void*>(&counter1),
  58. "Thread1");
  59. PlatformThread thread2(WorkingFunction, reinterpret_cast<void*>(&counter2),
  60. "Thread2");
  61. thread1.Start();
  62. thread2.Start();
  63. thread1.Stop();
  64. thread2.Stop();
  65. EXPECT_GE(counter1, 0);
  66. EXPECT_GE(counter2, 0);
  67. int64_t process_duration_nanos =
  68. GetProcessCpuTimeNanos() - process_start_time_nanos;
  69. int64_t thread_duration_nanos =
  70. GetThreadCpuTimeNanos() - thread_start_time_nanos;
  71. // This thread did almost nothing.
  72. // Therefore GetThreadCpuTime is not a wall clock.
  73. EXPECT_LE(thread_duration_nanos,
  74. kAllowedErrorMillisecs * kNumNanosecsPerMillisec);
  75. // Total process time is at least twice working threads' CPU time.
  76. // Therefore process and thread times are correctly related.
  77. EXPECT_GE(process_duration_nanos,
  78. kWorkingThreads *
  79. (kProcessingTimeMillisecs - kAllowedErrorMillisecs) *
  80. kNumNanosecsPerMillisec);
  81. }
  82. TEST(CpuTimeTest, MAYBE_TEST(Sleeping)) {
  83. int64_t process_start_time_nanos = GetProcessCpuTimeNanos();
  84. webrtc::SleepMs(kProcessingTimeMillisecs);
  85. int64_t process_duration_nanos =
  86. GetProcessCpuTimeNanos() - process_start_time_nanos;
  87. // Sleeping should not introduce any additional CPU time.
  88. // Therefore GetProcessCpuTime is not a wall clock.
  89. EXPECT_LE(process_duration_nanos,
  90. kWorkingThreads * kAllowedErrorMillisecs * kNumNanosecsPerMillisec);
  91. }
  92. } // namespace rtc