[email protected] | d1811bc | 2012-03-31 07:08:53 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 5f6eee53 | 2008-09-02 08:28:37 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <windows.h> |
[email protected] | de592d3 | 2008-09-26 03:00:00 | [diff] [blame] | 6 | #include <mmsystem.h> |
[email protected] | 5f6eee53 | 2008-09-02 08:28:37 | [diff] [blame] | 7 | #include <process.h> |
| 8 | |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 9 | #include "base/threading/platform_thread.h" |
[email protected] | 5f6eee53 | 2008-09-02 08:28:37 | [diff] [blame] | 10 | #include "base/time.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 13 | using base::Time; |
| 14 | using base::TimeDelta; |
| 15 | using base::TimeTicks; |
| 16 | |
[email protected] | 5f6eee53 | 2008-09-02 08:28:37 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | class MockTimeTicks : public TimeTicks { |
| 20 | public: |
[email protected] | c5324208 | 2008-09-09 08:58:51 | [diff] [blame] | 21 | static DWORD Ticker() { |
[email protected] | 5f6eee53 | 2008-09-02 08:28:37 | [diff] [blame] | 22 | return static_cast<int>(InterlockedIncrement(&ticker_)); |
| 23 | } |
| 24 | |
| 25 | static void InstallTicker() { |
[email protected] | c5324208 | 2008-09-09 08:58:51 | [diff] [blame] | 26 | old_tick_function_ = SetMockTickFunction(&Ticker); |
[email protected] | 5f6eee53 | 2008-09-02 08:28:37 | [diff] [blame] | 27 | ticker_ = -5; |
| 28 | } |
| 29 | |
| 30 | static void UninstallTicker() { |
[email protected] | c5324208 | 2008-09-09 08:58:51 | [diff] [blame] | 31 | SetMockTickFunction(old_tick_function_); |
[email protected] | 5f6eee53 | 2008-09-02 08:28:37 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | private: |
| 35 | static volatile LONG ticker_; |
[email protected] | c5324208 | 2008-09-09 08:58:51 | [diff] [blame] | 36 | static TickFunctionType old_tick_function_; |
[email protected] | 5f6eee53 | 2008-09-02 08:28:37 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | volatile LONG MockTimeTicks::ticker_; |
[email protected] | c5324208 | 2008-09-09 08:58:51 | [diff] [blame] | 40 | MockTimeTicks::TickFunctionType MockTimeTicks::old_tick_function_; |
[email protected] | 5f6eee53 | 2008-09-02 08:28:37 | [diff] [blame] | 41 | |
| 42 | HANDLE g_rollover_test_start; |
| 43 | |
| 44 | unsigned __stdcall RolloverTestThreadMain(void* param) { |
| 45 | int64 counter = reinterpret_cast<int64>(param); |
| 46 | DWORD rv = WaitForSingleObject(g_rollover_test_start, INFINITE); |
| 47 | EXPECT_EQ(rv, WAIT_OBJECT_0); |
| 48 | |
| 49 | TimeTicks last = TimeTicks::Now(); |
| 50 | for (int index = 0; index < counter; index++) { |
| 51 | TimeTicks now = TimeTicks::Now(); |
| 52 | int64 milliseconds = (now - last).InMilliseconds(); |
[email protected] | de592d3 | 2008-09-26 03:00:00 | [diff] [blame] | 53 | // This is a tight loop; we could have looped faster than our |
| 54 | // measurements, so the time might be 0 millis. |
| 55 | EXPECT_GE(milliseconds, 0); |
[email protected] | 5f6eee53 | 2008-09-02 08:28:37 | [diff] [blame] | 56 | EXPECT_LT(milliseconds, 250); |
| 57 | last = now; |
| 58 | } |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | } // namespace |
| 63 | |
| 64 | TEST(TimeTicks, WinRollover) { |
| 65 | // The internal counter rolls over at ~49days. We'll use a mock |
| 66 | // timer to test this case. |
| 67 | // Basic test algorithm: |
| 68 | // 1) Set clock to rollover - N |
| 69 | // 2) Create N threads |
| 70 | // 3) Start the threads |
| 71 | // 4) Each thread loops through TimeTicks() N times |
| 72 | // 5) Each thread verifies integrity of result. |
| 73 | |
| 74 | const int kThreads = 8; |
| 75 | // Use int64 so we can cast into a void* without a compiler warning. |
| 76 | const int64 kChecks = 10; |
| 77 | |
| 78 | // It takes a lot of iterations to reproduce the bug! |
| 79 | // (See bug 1081395) |
| 80 | for (int loop = 0; loop < 4096; loop++) { |
| 81 | // Setup |
| 82 | MockTimeTicks::InstallTicker(); |
| 83 | g_rollover_test_start = CreateEvent(0, TRUE, FALSE, 0); |
| 84 | HANDLE threads[kThreads]; |
| 85 | |
| 86 | for (int index = 0; index < kThreads; index++) { |
| 87 | void* argument = reinterpret_cast<void*>(kChecks); |
| 88 | unsigned thread_id; |
| 89 | threads[index] = reinterpret_cast<HANDLE>( |
| 90 | _beginthreadex(NULL, 0, RolloverTestThreadMain, argument, 0, |
| 91 | &thread_id)); |
| 92 | EXPECT_NE((HANDLE)NULL, threads[index]); |
| 93 | } |
| 94 | |
| 95 | // Start! |
| 96 | SetEvent(g_rollover_test_start); |
| 97 | |
| 98 | // Wait for threads to finish |
| 99 | for (int index = 0; index < kThreads; index++) { |
| 100 | DWORD rv = WaitForSingleObject(threads[index], INFINITE); |
| 101 | EXPECT_EQ(rv, WAIT_OBJECT_0); |
| 102 | } |
| 103 | |
| 104 | CloseHandle(g_rollover_test_start); |
| 105 | |
| 106 | // Teardown |
| 107 | MockTimeTicks::UninstallTicker(); |
| 108 | } |
| 109 | } |
[email protected] | de592d3 | 2008-09-26 03:00:00 | [diff] [blame] | 110 | |
[email protected] | 8af6f334 | 2010-09-23 15:54:48 | [diff] [blame] | 111 | TEST(TimeTicks, SubMillisecondTimers) { |
| 112 | // HighResNow doesn't work on some systems. Since the product still works |
| 113 | // even if it doesn't work, it makes this entire test questionable. |
| 114 | if (!TimeTicks::IsHighResClockWorking()) |
| 115 | return; |
| 116 | |
| 117 | const int kRetries = 1000; |
[email protected] | de592d3 | 2008-09-26 03:00:00 | [diff] [blame] | 118 | bool saw_submillisecond_timer = false; |
[email protected] | 8af6f334 | 2010-09-23 15:54:48 | [diff] [blame] | 119 | |
| 120 | // Run kRetries attempts to see a sub-millisecond timer. |
[email protected] | de592d3 | 2008-09-26 03:00:00 | [diff] [blame] | 121 | for (int index = 0; index < 1000; index++) { |
[email protected] | 8af6f334 | 2010-09-23 15:54:48 | [diff] [blame] | 122 | TimeTicks last_time = TimeTicks::HighResNow(); |
| 123 | TimeDelta delta; |
| 124 | // Spin until the clock has detected a change. |
| 125 | do { |
| 126 | delta = TimeTicks::HighResNow() - last_time; |
| 127 | } while (delta.InMicroseconds() == 0); |
| 128 | if (delta.InMicroseconds() < 1000) { |
[email protected] | de592d3 | 2008-09-26 03:00:00 | [diff] [blame] | 129 | saw_submillisecond_timer = true; |
[email protected] | 8af6f334 | 2010-09-23 15:54:48 | [diff] [blame] | 130 | break; |
[email protected] | de592d3 | 2008-09-26 03:00:00 | [diff] [blame] | 131 | } |
[email protected] | de592d3 | 2008-09-26 03:00:00 | [diff] [blame] | 132 | } |
| 133 | EXPECT_TRUE(saw_submillisecond_timer); |
[email protected] | de592d3 | 2008-09-26 03:00:00 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | TEST(TimeTicks, TimeGetTimeCaps) { |
| 137 | // Test some basic assumptions that we expect about how timeGetDevCaps works. |
| 138 | |
| 139 | TIMECAPS caps; |
| 140 | MMRESULT status = timeGetDevCaps(&caps, sizeof(caps)); |
| 141 | EXPECT_EQ(TIMERR_NOERROR, status); |
| 142 | if (status != TIMERR_NOERROR) { |
| 143 | printf("Could not get timeGetDevCaps\n"); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | EXPECT_GE(static_cast<int>(caps.wPeriodMin), 1); |
| 148 | EXPECT_GT(static_cast<int>(caps.wPeriodMax), 1); |
| 149 | EXPECT_GE(static_cast<int>(caps.wPeriodMin), 1); |
| 150 | EXPECT_GT(static_cast<int>(caps.wPeriodMax), 1); |
| 151 | printf("timeGetTime range is %d to %dms\n", caps.wPeriodMin, |
| 152 | caps.wPeriodMax); |
| 153 | } |
| 154 | |
| 155 | TEST(TimeTicks, QueryPerformanceFrequency) { |
| 156 | // Test some basic assumptions that we expect about QPC. |
| 157 | |
| 158 | LARGE_INTEGER frequency; |
| 159 | BOOL rv = QueryPerformanceFrequency(&frequency); |
| 160 | EXPECT_EQ(TRUE, rv); |
| 161 | EXPECT_GT(frequency.QuadPart, 1000000); // Expect at least 1MHz |
| 162 | printf("QueryPerformanceFrequency is %5.2fMHz\n", |
| 163 | frequency.QuadPart / 1000000.0); |
| 164 | } |
| 165 | |
| 166 | TEST(TimeTicks, TimerPerformance) { |
| 167 | // Verify that various timer mechanisms can always complete quickly. |
| 168 | // Note: This is a somewhat arbitrary test. |
| 169 | const int kLoops = 10000; |
[email protected] | 32202ea | 2008-09-26 15:21:42 | [diff] [blame] | 170 | // Due to the fact that these run on bbots, which are horribly slow, |
| 171 | // we can't really make any guarantees about minimum runtime. |
| 172 | // Really, we want these to finish in ~10ms, and that is generous. |
| 173 | const int kMaxTime = 35; // Maximum acceptible milliseconds for test. |
[email protected] | de592d3 | 2008-09-26 03:00:00 | [diff] [blame] | 174 | |
| 175 | typedef TimeTicks (*TestFunc)(); |
| 176 | struct TestCase { |
| 177 | TestFunc func; |
| 178 | char *description; |
| 179 | }; |
| 180 | // Cheating a bit here: assumes sizeof(TimeTicks) == sizeof(Time) |
| 181 | // in order to create a single test case list. |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 182 | COMPILE_ASSERT(sizeof(TimeTicks) == sizeof(Time), |
[email protected] | de592d3 | 2008-09-26 03:00:00 | [diff] [blame] | 183 | test_only_works_with_same_sizes); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 184 | TestCase cases[] = { |
[email protected] | de592d3 | 2008-09-26 03:00:00 | [diff] [blame] | 185 | { reinterpret_cast<TestFunc>(Time::Now), "Time::Now" }, |
| 186 | { TimeTicks::Now, "TimeTicks::Now" }, |
| 187 | { TimeTicks::HighResNow, "TimeTicks::HighResNow" }, |
| 188 | { NULL, "" } |
| 189 | }; |
| 190 | |
| 191 | int test_case = 0; |
| 192 | while (cases[test_case].func) { |
| 193 | TimeTicks start = TimeTicks::HighResNow(); |
| 194 | for (int index = 0; index < kLoops; index++) |
| 195 | cases[test_case].func(); |
| 196 | TimeTicks stop = TimeTicks::HighResNow(); |
[email protected] | 4681149 | 2008-12-18 19:17:31 | [diff] [blame] | 197 | // Turning off the check for acceptible delays. Without this check, |
| 198 | // the test really doesn't do much other than measure. But the |
| 199 | // measurements are still useful for testing timers on various platforms. |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 200 | // The reason to remove the check is because the tests run on many |
[email protected] | 4681149 | 2008-12-18 19:17:31 | [diff] [blame] | 201 | // buildbots, some of which are VMs. These machines can run horribly |
| 202 | // slow, and there is really no value for checking against a max timer. |
| 203 | //EXPECT_LT((stop - start).InMilliseconds(), kMaxTime); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 204 | printf("%s: %1.2fus per call\n", cases[test_case].description, |
[email protected] | de592d3 | 2008-09-26 03:00:00 | [diff] [blame] | 205 | (stop - start).InMillisecondsF() * 1000 / kLoops); |
| 206 | test_case++; |
| 207 | } |
| 208 | } |
[email protected] | 0b2a2f4 | 2010-08-04 22:17:18 | [diff] [blame] | 209 | |
| 210 | TEST(TimeTicks, Drift) { |
[email protected] | d1811bc | 2012-03-31 07:08:53 | [diff] [blame] | 211 | // If QPC is disabled, this isn't measuring anything. |
| 212 | if (!TimeTicks::IsHighResClockWorking()) |
| 213 | return; |
| 214 | |
[email protected] | 0b2a2f4 | 2010-08-04 22:17:18 | [diff] [blame] | 215 | const int kIterations = 100; |
| 216 | int64 total_drift = 0; |
| 217 | |
| 218 | for (int i = 0; i < kIterations; ++i) { |
| 219 | int64 drift_microseconds = TimeTicks::GetQPCDriftMicroseconds(); |
| 220 | |
| 221 | // Make sure the drift never exceeds our limit. |
| 222 | EXPECT_LT(drift_microseconds, 50000); |
| 223 | |
| 224 | // Sleep for a few milliseconds (note that it means 1000 microseconds). |
| 225 | // If we check the drift too frequently, it's going to increase |
| 226 | // monotonically, making our measurement less realistic. |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 227 | base::PlatformThread::Sleep( |
| 228 | base::TimeDelta::FromMilliseconds((i % 2 == 0) ? 1 : 2)); |
[email protected] | 0b2a2f4 | 2010-08-04 22:17:18 | [diff] [blame] | 229 | |
| 230 | total_drift += drift_microseconds; |
| 231 | } |
| 232 | |
| 233 | // Sanity check. We expect some time drift to occur, especially across |
[email protected] | d1811bc | 2012-03-31 07:08:53 | [diff] [blame] | 234 | // the number of iterations we do. |
[email protected] | 0b2a2f4 | 2010-08-04 22:17:18 | [diff] [blame] | 235 | EXPECT_LT(0, total_drift); |
| 236 | |
| 237 | printf("average time drift in microseconds: %lld\n", |
| 238 | total_drift / kIterations); |
[email protected] | 8af6f334 | 2010-09-23 15:54:48 | [diff] [blame] | 239 | } |