Revision f230a1cf deps/v8/test/cctest/test-time.cc
deps/v8/test/cctest/test-time.cc | ||
---|---|---|
133 | 133 |
timer.Start(); |
134 | 134 |
while (!timer.HasExpired(TimeDelta::FromMilliseconds(100))) { |
135 | 135 |
TimeTicks normal_ticks = TimeTicks::Now(); |
136 |
TimeTicks highres_ticks = TimeTicks::HighResNow(); |
|
136 |
TimeTicks highres_ticks = TimeTicks::HighResolutionNow();
|
|
137 | 137 |
CHECK_GE(normal_ticks, previous_normal_ticks); |
138 | 138 |
CHECK_GE((normal_ticks - previous_normal_ticks).InMicroseconds(), 0); |
139 | 139 |
CHECK_GE(highres_ticks, previous_highres_ticks); |
... | ... | |
142 | 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(TimeTicksHighResolutionNowResolution) { |
|
190 |
if (!TimeTicks::IsHighResolutionClockWorking()) return; |
|
191 |
|
|
192 |
// We assume that TimeTicks::HighResolutionNow() has sub-ms resolution. |
|
193 |
static const TimeDelta kTargetGranularity = TimeDelta::FromMilliseconds(1); |
|
194 |
ResolutionTest<TimeTicks>(&TimeTicks::HighResolutionNow, kTargetGranularity); |
|
195 |
} |
Also available in: Unified diff