Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 TimeTicks normal_ticks = TimeTicks::Now(); | 135 TimeTicks normal_ticks = TimeTicks::Now(); |
| 136 TimeTicks highres_ticks = TimeTicks::HighResNow(); | 136 TimeTicks highres_ticks = TimeTicks::HighResNow(); |
| 137 CHECK_GE(normal_ticks, previous_normal_ticks); | 137 CHECK_GE(normal_ticks, previous_normal_ticks); |
| 138 CHECK_GE((normal_ticks - previous_normal_ticks).InMicroseconds(), 0); | 138 CHECK_GE((normal_ticks - previous_normal_ticks).InMicroseconds(), 0); |
| 139 CHECK_GE(highres_ticks, previous_highres_ticks); | 139 CHECK_GE(highres_ticks, previous_highres_ticks); |
| 140 CHECK_GE((highres_ticks - previous_highres_ticks).InMicroseconds(), 0); | 140 CHECK_GE((highres_ticks - previous_highres_ticks).InMicroseconds(), 0); |
| 141 previous_normal_ticks = normal_ticks; | 141 previous_normal_ticks = normal_ticks; |
| 142 previous_highres_ticks = highres_ticks; | 142 previous_highres_ticks = highres_ticks; |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 | |
| 146 | |
| 147 template <typename T> | |
| 148 static void ResolutionTest(T (*Now)(), TimeDelta target_granularity) { | |
| 149 // We're trying to measure that intervals increment in a VERY small amount | |
| 150 // of time -- according to the specified target granularity. Unfortunately, | |
| 151 // if we happen to have a context switch in the middle of our test, the | |
| 152 // context switch could easily exceed our limit. So, we iterate on this | |
| 153 // several times. As long as we're able to detect the fine-granularity | |
| 154 // timers at least once, then the test has succeeded. | |
| 155 static const TimeDelta kExpirationTimeout = TimeDelta::FromSeconds(1); | |
| 156 ElapsedTimer timer; | |
| 157 timer.Start(); | |
| 158 TimeDelta delta; | |
| 159 do { | |
| 160 T start = Now(); | |
| 161 T now = start; | |
| 162 // Loop until we can detect that the clock has changed. Non-HighRes timers | |
| 163 // will increment in chunks, i.e. 15ms. By spinning until we see a clock | |
| 164 // change, we detect the minimum time between measurements. | |
| 165 do { | |
| 166 now = Now(); | |
| 167 delta = now - start; | |
| 168 } while (now <= start); | |
| 169 CHECK_NE(static_cast<int64_t>(0), delta.InMicroseconds()); | |
| 170 } while (delta > target_granularity && !timer.HasExpired(kExpirationTimeout)); | |
| 171 CHECK_LE(delta, target_granularity); | |
| 172 } | |
| 173 | |
| 174 | |
| 175 TEST(TimeNowResolution) { | |
| 176 // We assume that Time::Now() has at least 16ms resolution. | |
| 177 static const TimeDelta kTargetGranularity = TimeDelta::FromMilliseconds(16); | |
| 178 ResolutionTest<Time>(&Time::Now, kTargetGranularity); | |
| 179 } | |
| 180 | |
| 181 | |
| 182 TEST(TimeTicksNowResolution) { | |
| 183 // We assume that TimeTicks::Now() has at least 16ms resolution. | |
| 184 static const TimeDelta kTargetGranularity = TimeDelta::FromMilliseconds(16); | |
| 185 ResolutionTest<TimeTicks>(&TimeTicks::Now, kTargetGranularity); | |
| 186 } | |
| 187 | |
| 188 | |
| 189 TEST(TimeTicksHighResNowResolution) { | |
|
Hannes Payer (out of office)
2013/10/02 11:27:25
What about merging the last two test cases and usi
Benedikt Meurer
2013/10/02 12:27:46
TimeTicks::Now() is different from TimeTicks::High
| |
| 190 if (!TimeTicks::IsHighResClockWorking()) return; | |
| 191 | |
| 192 // We assume that TimeTicks::HighResNow() has sub-millisecond resolution. | |
| 193 static const TimeDelta kTargetGranularity = TimeDelta::FromMilliseconds(1); | |
| 194 ResolutionTest<TimeTicks>(&TimeTicks::HighResNow, kTargetGranularity); | |
| 195 } | |
| OLD | NEW |