Skip to content

Commit 11f457c

Browse files
chris-eiblencukouhugovk
authored
GH-114911: use time.perf_counter in Stopwatch (GH-131469)
Co-authored-by: Petr Viktorin <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent e577439 commit 11f457c

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

Lib/test/support/__init__.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,30 +2586,30 @@ def sleeping_retry(timeout, err_msg=None, /,
25862586
delay = min(delay * 2, max_delay)
25872587

25882588

2589-
class CPUStopwatch:
2589+
class Stopwatch:
25902590
"""Context manager to roughly time a CPU-bound operation.
25912591
2592-
Disables GC. Uses CPU time if it can (i.e. excludes sleeps & time of
2593-
other processes).
2592+
Disables GC. Uses perf_counter, which is a clock with the highest
2593+
available resolution. It is chosen even though it does include
2594+
time elapsed during sleep and is system-wide, because the
2595+
resolution of process_time is too coarse on Windows and
2596+
process_time does not exist everywhere (for example, WASM).
25942597
2595-
N.B.:
2596-
- This *includes* time spent in other threads.
2598+
Note:
2599+
- This *includes* time spent in other threads/processes.
25972600
- Some systems only have a coarse resolution; check
2598-
stopwatch.clock_info.rseolution if.
2601+
stopwatch.clock_info.resolution when using the results.
25992602
26002603
Usage:
26012604
2602-
with ProcessStopwatch() as stopwatch:
2605+
with Stopwatch() as stopwatch:
26032606
...
26042607
elapsed = stopwatch.seconds
26052608
resolution = stopwatch.clock_info.resolution
26062609
"""
26072610
def __enter__(self):
2608-
get_time = time.process_time
2609-
clock_info = time.get_clock_info('process_time')
2610-
if get_time() <= 0: # some platforms like WASM lack process_time()
2611-
get_time = time.monotonic
2612-
clock_info = time.get_clock_info('monotonic')
2611+
get_time = time.perf_counter
2612+
clock_info = time.get_clock_info('perf_counter')
26132613
self.context = disable_gc()
26142614
self.context.__enter__()
26152615
self.get_time = get_time

Lib/test/test_int.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ def test_denial_of_service_prevented_int_to_str(self):
590590
digits = 78_268
591591
with (
592592
support.adjust_int_max_str_digits(digits),
593-
support.CPUStopwatch() as sw_convert):
593+
support.Stopwatch() as sw_convert):
594594
huge_decimal = str(huge_int)
595595
self.assertEqual(len(huge_decimal), digits)
596596
# Ensuring that we chose a slow enough conversion to measure.
@@ -605,7 +605,7 @@ def test_denial_of_service_prevented_int_to_str(self):
605605
with support.adjust_int_max_str_digits(int(.995 * digits)):
606606
with (
607607
self.assertRaises(ValueError) as err,
608-
support.CPUStopwatch() as sw_fail_huge):
608+
support.Stopwatch() as sw_fail_huge):
609609
str(huge_int)
610610
self.assertIn('conversion', str(err.exception))
611611
self.assertLessEqual(sw_fail_huge.seconds, sw_convert.seconds/2)
@@ -615,7 +615,7 @@ def test_denial_of_service_prevented_int_to_str(self):
615615
extra_huge_int = int(f'0x{"c"*500_000}', base=16) # 602060 digits.
616616
with (
617617
self.assertRaises(ValueError) as err,
618-
support.CPUStopwatch() as sw_fail_extra_huge):
618+
support.Stopwatch() as sw_fail_extra_huge):
619619
# If not limited, 8 seconds said Zen based cloud VM.
620620
str(extra_huge_int)
621621
self.assertIn('conversion', str(err.exception))
@@ -630,7 +630,7 @@ def test_denial_of_service_prevented_str_to_int(self):
630630
huge = '8'*digits
631631
with (
632632
support.adjust_int_max_str_digits(digits),
633-
support.CPUStopwatch() as sw_convert):
633+
support.Stopwatch() as sw_convert):
634634
int(huge)
635635
# Ensuring that we chose a slow enough conversion to measure.
636636
# It takes 0.1 seconds on a Zen based cloud VM in an opt build.
@@ -642,7 +642,7 @@ def test_denial_of_service_prevented_str_to_int(self):
642642
with support.adjust_int_max_str_digits(digits - 1):
643643
with (
644644
self.assertRaises(ValueError) as err,
645-
support.CPUStopwatch() as sw_fail_huge):
645+
support.Stopwatch() as sw_fail_huge):
646646
int(huge)
647647
self.assertIn('conversion', str(err.exception))
648648
self.assertLessEqual(sw_fail_huge.seconds, sw_convert.seconds/2)
@@ -652,7 +652,7 @@ def test_denial_of_service_prevented_str_to_int(self):
652652
extra_huge = '7'*1_200_000
653653
with (
654654
self.assertRaises(ValueError) as err,
655-
support.CPUStopwatch() as sw_fail_extra_huge):
655+
support.Stopwatch() as sw_fail_extra_huge):
656656
# If not limited, 8 seconds in the Zen based cloud VM.
657657
int(extra_huge)
658658
self.assertIn('conversion', str(err.exception))

Lib/test/test_re.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from test.support import (gc_collect, bigmemtest, _2G,
22
cpython_only, captured_stdout,
33
check_disallow_instantiation, linked_to_musl,
4-
warnings_helper, SHORT_TIMEOUT, CPUStopwatch, requires_resource)
4+
warnings_helper, SHORT_TIMEOUT, Stopwatch, requires_resource)
55
import locale
66
import re
77
import string
@@ -2467,7 +2467,7 @@ def test_bug_40736(self):
24672467
@requires_resource('cpu')
24682468
def test_search_anchor_at_beginning(self):
24692469
s = 'x'*10**7
2470-
with CPUStopwatch() as stopwatch:
2470+
with Stopwatch() as stopwatch:
24712471
for p in r'\Ay', r'^y':
24722472
self.assertIsNone(re.search(p, s))
24732473
self.assertEqual(re.split(p, s), [s])

0 commit comments

Comments
 (0)