From 4f18889bbe8b13c7129b663581676009dd3a9965 Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Wed, 22 Oct 2025 17:27:47 +0200 Subject: [PATCH 1/3] gh-140381: Handle slower machines in test_profiling While looking at #140028 I found some test failures that are caused by new tests (from #138122) running too slowly. This adds an arbitrary heuristic to 10x the sampling run time (to the default value of 10 seconds). Doubling the 1-second duration was sufficient for my HP PA tests, but Fedora reported one of the 2-second durations being too slow for a freethreaded build. This heuristic could move into test_support. Thoughts? --- .../test_profiling/test_sampling_profiler.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_profiling/test_sampling_profiler.py b/Lib/test/test_profiling/test_sampling_profiler.py index 59bc18b9bcf14d..ae6e37d98df626 100644 --- a/Lib/test/test_profiling/test_sampling_profiler.py +++ b/Lib/test/test_profiling/test_sampling_profiler.py @@ -10,6 +10,7 @@ import subprocess import sys import tempfile +import timeit import unittest from collections import namedtuple from unittest import mock @@ -29,6 +30,8 @@ PROCESS_VM_READV_SUPPORTED = False +SLOW_MACHINE = timeit.timeit("2*2", number=10_000) > 0.0001 + try: from _remote_debugging import PROCESS_VM_READV_SUPPORTED import _remote_debugging @@ -1754,6 +1757,7 @@ def main_loop(): ''' def test_sampling_basic_functionality(self): + duration_sec = 10 if SLOW_MACHINE else 2 with ( test_subprocess(self.test_script) as subproc, io.StringIO() as captured_output, @@ -1762,7 +1766,7 @@ def test_sampling_basic_functionality(self): try: profiling.sampling.sample.sample( subproc.process.pid, - duration_sec=2, + duration_sec=duration_sec, sample_interval_usec=1000, # 1ms show_summary=False, ) @@ -1904,7 +1908,10 @@ def test_sample_target_script(self): script_file.flush() self.addCleanup(close_and_unlink, script_file) - test_args = ["profiling.sampling.sample", "-d", "1", script_file.name] + duration = 10 if SLOW_MACHINE else 1 + test_args = [ + "profiling.sampling.sample", "-d", str(duration), script_file.name + ] with ( mock.patch("sys.argv", test_args), @@ -1936,7 +1943,12 @@ def test_sample_target_module(self): with open(module_path, "w") as f: f.write(self.test_script) - test_args = ["profiling.sampling.sample", "-d", "1", "-m", "test_module"] + duration = 10 if SLOW_MACHINE else 1 + test_args = [ + "profiling.sampling.sample", + "-d", str(duration), + "-m", "test_module" + ] with ( mock.patch("sys.argv", test_args), From 4425705688aceb38d64b8d2fdb67bcd045e0810e Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Wed, 22 Oct 2025 18:02:54 +0200 Subject: [PATCH 2/3] Move is_slow_machine to test_support --- Lib/test/support/__init__.py | 4 ++++ Lib/test/test_profiling/test_sampling_profiler.py | 11 ++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 098bdcc0542b90..2531607a9633b9 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -16,6 +16,7 @@ import sysconfig import textwrap import time +import timeit import types import unittest import warnings @@ -2694,6 +2695,9 @@ def exceeds_recursion_limit(): return 150_000 +# Is the host running the tests significantly slower than a typical PC? +is_slow_machine = timeit.timeit("2*2", number=10_000) > 0.0001 + # Windows doesn't have os.uname() but it doesn't support s390x. is_s390x = hasattr(os, 'uname') and os.uname().machine == 's390x' skip_on_s390x = unittest.skipIf(is_s390x, 'skipped on s390x') diff --git a/Lib/test/test_profiling/test_sampling_profiler.py b/Lib/test/test_profiling/test_sampling_profiler.py index ae6e37d98df626..078d0b22ee7609 100644 --- a/Lib/test/test_profiling/test_sampling_profiler.py +++ b/Lib/test/test_profiling/test_sampling_profiler.py @@ -10,7 +10,6 @@ import subprocess import sys import tempfile -import timeit import unittest from collections import namedtuple from unittest import mock @@ -26,12 +25,10 @@ from test.support import force_not_colorized_test_class, SHORT_TIMEOUT from test.support.socket_helper import find_unused_port from test.support import requires_subprocess, is_emscripten -from test.support import captured_stdout, captured_stderr +from test.support import captured_stdout, captured_stderr, is_slow_machine PROCESS_VM_READV_SUPPORTED = False -SLOW_MACHINE = timeit.timeit("2*2", number=10_000) > 0.0001 - try: from _remote_debugging import PROCESS_VM_READV_SUPPORTED import _remote_debugging @@ -1757,7 +1754,7 @@ def main_loop(): ''' def test_sampling_basic_functionality(self): - duration_sec = 10 if SLOW_MACHINE else 2 + duration_sec = 10 if is_slow_machine else 2 with ( test_subprocess(self.test_script) as subproc, io.StringIO() as captured_output, @@ -1908,7 +1905,7 @@ def test_sample_target_script(self): script_file.flush() self.addCleanup(close_and_unlink, script_file) - duration = 10 if SLOW_MACHINE else 1 + duration = 10 if is_slow_machine else 1 test_args = [ "profiling.sampling.sample", "-d", str(duration), script_file.name ] @@ -1943,7 +1940,7 @@ def test_sample_target_module(self): with open(module_path, "w") as f: f.write(self.test_script) - duration = 10 if SLOW_MACHINE else 1 + duration = 10 if is_slow_machine else 1 test_args = [ "profiling.sampling.sample", "-d", str(duration), From 4779da2cf6a23918ccfa24d6a3548f0b5a5d8b3d Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Wed, 22 Oct 2025 18:05:39 +0200 Subject: [PATCH 3/3] Tidy imports --- Lib/test/test_profiling/test_sampling_profiler.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_profiling/test_sampling_profiler.py b/Lib/test/test_profiling/test_sampling_profiler.py index 078d0b22ee7609..0eafd4d77aae1b 100644 --- a/Lib/test/test_profiling/test_sampling_profiler.py +++ b/Lib/test/test_profiling/test_sampling_profiler.py @@ -21,11 +21,17 @@ ) from profiling.sampling.gecko_collector import GeckoCollector +from test.support import ( + SHORT_TIMEOUT, + captured_stderr, + captured_stdout, + force_not_colorized_test_class, + is_emscripten, + is_slow_machine, + requires_subprocess, +) from test.support.os_helper import unlink -from test.support import force_not_colorized_test_class, SHORT_TIMEOUT from test.support.socket_helper import find_unused_port -from test.support import requires_subprocess, is_emscripten -from test.support import captured_stdout, captured_stderr, is_slow_machine PROCESS_VM_READV_SUPPORTED = False