8000 gh-114911: Add CPUStopwatch test helper by encukou · Pull Request #114912 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-114911: Add CPUStopwatch test helper #114912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Expose the clock info
  • Loading branch information
encukou committed Feb 13, 2024
commit bed30c3d958dc8ebf61d48c2e4895d41aeb98249
7 changes: 6 additions & 1 deletion Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2370,21 +2370,26 @@ class CPUStopwatch:

N.B.:
- This *includes* time spent in other threads.
- Some systems only have a coarse 1/64 second resolution.
- Some systems only have a coarse resolution; check
stopwatch.clock_info.rseolution if.

Usage:

with ProcessStopwatch() as stopwatch:
...
elapsed = stopwatch.seconds
resolution = stopwatch.clock_info.resolution
"""
def __enter__(self):
get_time = time.process_time
clock_info = time.get_clock_info('process_time')
if get_time() <= 0: # some platforms like WASM lack process_time()
get_time = time.monotonic
clock_info = time.get_clock_info('monotonic')
self.context = disable_gc()
self.context.__enter__()
self.get_time = get_time
self.clock_info = clock_info
self.start_time = get_time()
return self

Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ def test_denial_of_service_prevented_int_to_str(self):
# Ensuring that we chose a slow enough conversion to measure.
# It takes 0.1 seconds on a Zen based cloud VM in an opt build.
# Some OSes have a low res 1/64s timer, skip if hard to measure.
if sw_convert.seconds < 1/64:
if sw_convert.seconds < sw_convert.clock_info.resolution * 2:
raise unittest.SkipTest('"slow" conversion took only '
f'{sw_convert.seconds} seconds.')

Expand Down Expand Up @@ -714,7 +714,7 @@ def test_denial_of_service_prevented_str_to_int(self):
# Ensuring that we chose a slow enough conversion to measure.
# It takes 0.1 seconds on a Zen based cloud VM in an opt build.
# Some OSes have a low res 1/64s timer, skip if hard to measure.
if sw_convert.seconds < 1/64:
if sw_convert.seconds < sw_convert.clock_info.resolution * 2:
raise unittest.SkipTest('"slow" conversion took only '
f'{sw_convert.seconds} seconds.')

Expand Down
0