gh-114911: Add CPUStopwatch test helper (GH-114912)

A few of our tests measure the time of CPU-bound operation, mainly
to avoid quadratic or worse behaviour.
Add a helper to ignore GC and time spent in other processes.
This commit is contained in:
Petr Viktorin 2024-02-28 12:53:48 +01:00 committed by GitHub
parent 3b63d0769f
commit 7acf1fb5a7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 75 additions and 42 deletions

View file

@ -1,7 +1,7 @@
from test.support import (gc_collect, bigmemtest, _2G,
cpython_only, captured_stdout,
check_disallow_instantiation, is_emscripten, is_wasi,
warnings_helper, SHORT_TIMEOUT)
warnings_helper, SHORT_TIMEOUT, CPUStopwatch)
import locale
import re
import string
@ -2284,17 +2284,16 @@ class ReTests(unittest.TestCase):
def test_search_anchor_at_beginning(self):
s = 'x'*10**7
start = time.perf_counter()
for p in r'\Ay', r'^y':
self.assertIsNone(re.search(p, s))
self.assertEqual(re.split(p, s), [s])
self.assertEqual(re.findall(p, s), [])
self.assertEqual(list(re.finditer(p, s)), [])
self.assertEqual(re.sub(p, '', s), s)
t = time.perf_counter() - start
with CPUStopwatch() as stopwatch:
for p in r'\Ay', r'^y':
self.assertIsNone(re.search(p, s))
self.assertEqual(re.split(p, s), [s])
self.assertEqual(re.findall(p, s), [])
self.assertEqual(list(re.finditer(p, s)), [])
self.assertEqual(re.sub(p, '', s), s)
# Without optimization it takes 1 second on my computer.
# With optimization -- 0.0003 seconds.
self.assertLess(t, 0.1)
self.assertLess(stopwatch.seconds, 0.1)
def test_possessive_quantifiers(self):
"""Test Possessive Quantifiers