TimeUtilTest.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <thread>
  17. #include <glog/logging.h>
  18. #include <folly/io/async/test/TimeUtil.h>
  19. #include <folly/portability/GTest.h>
  20. #include <folly/portability/Unistd.h>
  21. using folly::TimePoint;
  22. using namespace std::literals::chrono_literals;
  23. using std::chrono::duration_cast;
  24. using std::chrono::milliseconds;
  25. using std::chrono::nanoseconds;
  26. using std::chrono::steady_clock;
  27. // Define a PrintTo() function for std::chrono::nanoseconds so that these
  28. // will be printed nicely on EXPECT*() failures.
  29. // Define this in std::chrono so that argument-dependent lookup works.
  30. namespace std {
  31. namespace chrono {
  32. static inline void PrintTo(nanoseconds ns, ::std::ostream* os) {
  33. *os << ns.count() << "ns";
  34. }
  35. } // namespace chrono
  36. } // namespace std
  37. #ifdef __linux__
  38. void runThread(nanoseconds duration, nanoseconds* timeWaiting) {
  39. TimePoint start;
  40. // Loop consuming CPU until the duration has expired.
  41. while (true) {
  42. TimePoint now;
  43. if (now.getTimeStart() - start.getTimeStart() > duration) {
  44. // Time to quit
  45. // Report how long we spent waiting to be scheduled on the CPU.
  46. *timeWaiting = (now.getTimeWaiting() - start.getTimeWaiting());
  47. VLOG(1) << "thread " << start.getTid() << ": elapsed "
  48. << duration_cast<milliseconds>(
  49. now.getTimeStart() - start.getTimeStart())
  50. .count()
  51. << "ms, time waiting: "
  52. << duration_cast<milliseconds>(*timeWaiting).count() << "ms";
  53. break;
  54. }
  55. }
  56. }
  57. // Test to make sure that TimePoint computes sane values for time
  58. // spent waiting on CPU.
  59. TEST(TimeUtil, getTimeWaiting) {
  60. TimePoint tp;
  61. // Run twice as many threads as CPU cores, to ensure that some of
  62. // them should be waiting sometime.
  63. auto numThreads = sysconf(_SC_NPROCESSORS_CONF) * 2;
  64. std::vector<std::thread> threads;
  65. std::vector<nanoseconds> timeWaiting;
  66. timeWaiting.resize(numThreads, 0ns);
  67. auto start = steady_clock::now();
  68. for (int n = 0; n < numThreads; ++n) {
  69. threads.emplace_back(runThread, 1s, &timeWaiting[n]);
  70. }
  71. for (auto& thread : threads) {
  72. thread.join();
  73. }
  74. auto end = steady_clock::now();
  75. auto timeSpent = end - start;
  76. nanoseconds max{0};
  77. for (int n = 0; n < numThreads; ++n) {
  78. max = std::max(max, timeWaiting[n]);
  79. // No thread could possibly have been waiting for longer than
  80. // the test actually took to run.
  81. EXPECT_LT(timeWaiting[n], timeSpent);
  82. }
  83. // Make sure that at least one thread spent some time waiting
  84. EXPECT_GE(max, 1ns);
  85. }
  86. #endif