MCPcopy Create free account
hub / github.com/apache/brpc / HighResClockTest

Function HighResClockTest

test/time_unittest.cc:648–685  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

646}
647
648static void HighResClockTest(TimeTicks (*GetTicks)()) {
649#if defined(OS_WIN)
650 // HighResNow doesn't work on some systems. Since the product still works
651 // even if it doesn't work, it makes this entire test questionable.
652 if (!TimeTicks::IsHighResClockWorking())
653 return;
654#endif
655
656 // Why do we loop here?
657 // We're trying to measure that intervals increment in a VERY small amount
658 // of time -- less than 15ms. Unfortunately, if we happen to have a
659 // context switch in the middle of our test, the context switch could easily
660 // exceed our limit. So, we iterate on this several times. As long as we're
661 // able to detect the fine-granularity timers at least once, then the test
662 // has succeeded.
663
664 const int kTargetGranularityUs = 15000; // 15ms
665
666 bool success = false;
667 int retries = 100; // Arbitrary.
668 TimeDelta delta;
669 while (!success && retries--) {
670 TimeTicks ticks_start = GetTicks();
671 // Loop until we can detect that the clock has changed. Non-HighRes timers
672 // will increment in chunks, e.g. 15ms. By spinning until we see a clock
673 // change, we detect the minimum time between measurements.
674 do {
675 delta = GetTicks() - ticks_start;
676 } while (delta.InMilliseconds() == 0);
677
678 if (delta.InMicroseconds() <= kTargetGranularityUs)
679 success = true;
680 }
681
682 // In high resolution mode, we expect to see the clock increment
683 // in intervals less than 15ms.
684 EXPECT_TRUE(success);
685}
686
687TEST(TimeTicks, HighResNow) {
688 HighResClockTest(&TimeTicks::HighResNow);

Callers 1

TESTFunction · 0.85

Calls 2

InMillisecondsMethod · 0.80
InMicrosecondsMethod · 0.80

Tested by

no test coverage detected