gecko-dev/testing/web-platform/tests/common/sleep.py
Erik Anderson 323dc181fd Bug 1535636 [wpt PR 15776] - Update sleep.py to use high resolution timer, a=testonly
Automatic update from web-platform-tests
Update sleep.py to use high resolution timer

In a previous change to this file (https://chromium-review.googlesource.com/c/chromium/src/+/1469655), I added a check after the sleep completes to check if we slept long enough. After submitting this to Microsoft's test environment which runs on Hyper-V VMs, we found that the test was still flaky because the timer we're using in the Python web server doesn't have sufficient precision.

In the previous change, I had considered using time.clock() which uses QPC on Windows. However, on Unix, it maps to CPU time which means it doesn't advance while we're switched out for the sleep. This change uses time.default_timer() which is the best of both worlds: with Python 2 it maps to time.clock() on Windows and to time.time() on non-Windows platforms while on Python 3.3+ it maps to time.perf_counter(); this ensures it is measuring wall time with the most accurate clock available for the platform.

This change has been run for over two weeks in Microsoft's test environment without any issues.

Change-Id: I30fc5f72de24fafe0766ca9b607b63290e47fae5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1516914
Reviewed-by: Robert Ma <robertma@chromium.org>
Commit-Queue: Robert Ma <robertma@chromium.org>
Cr-Commit-Position: refs/heads/master@{#640122}

--

wpt-commits: 7d3ab930ab623a4bcbb2af6e5d87ad8a851bbee0
wpt-pr: 15776
2019-04-01 14:43:29 +01:00

13 lines
491 B
Python

import time
import timeit
# sleep can be lower than requested value in some platforms: https://bugs.python.org/issue31539
# We add padding here to compensate for that.
sleep_padding = 15.0
def sleep_at_least(sleep_in_ms):
sleep_until = timeit.default_timer() + (sleep_in_ms / 1E3)
time.sleep((sleep_in_ms + sleep_padding) / 1E3)
# Check if the padding was sufficient; if not, sleep again.
while timeit.default_timer() < sleep_until:
time.sleep(sleep_padding / 1E3)