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 59bc18b9bcf14d..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 PROCESS_VM_READV_SUPPORTED = False @@ -1754,6 +1760,7 @@ def main_loop(): ''' def test_sampling_basic_functionality(self): + duration_sec = 10 if is_slow_machine else 2 with ( test_subprocess(self.test_script) as subproc, io.StringIO() as captured_output, @@ -1762,7 +1769,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 +1911,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 is_slow_machine else 1 + test_args = [ + "profiling.sampling.sample", "-d", str(duration), script_file.name + ] with ( mock.patch("sys.argv", test_args), @@ -1936,7 +1946,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 is_slow_machine else 1 + test_args = [ + "profiling.sampling.sample", + "-d", str(duration), + "-m", "test_module" + ] with ( mock.patch("sys.argv", test_args),