Skip to content

Commit 0cd4097

Browse files
committed
gh-140381: Make test_profiling tests deterministic to fix flakiness
The test_sampling_basic_functionality, test_sample_target_script, and test_sample_target_module tests were failing intermittently on slow architectures (i686/s390x) because they relied on a time-based workload with an infinite loop (while True). On slow machines, the profiler's sampling window would end before enough iterations executed, causing slow_fibonacci to not appear in the profiling output.
1 parent 17636ba commit 0cd4097

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Lib/test/test_profiling/test_sampling_profiler/test_integration.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
requires_subprocess,
2828
captured_stdout,
2929
captured_stderr,
30+
SHORT_TIMEOUT,
3031
)
3132

3233
from .helpers import (
@@ -37,6 +38,9 @@
3738
)
3839
from .mocks import MockFrameInfo, MockThreadInfo, MockInterpreterInfo
3940

41+
# Duration for profiling tests - long enough for process to complete naturally
42+
PROFILING_TIMEOUT = str(int(SHORT_TIMEOUT))
43+
4044

4145
@skip_if_not_supported
4246
@unittest.skipIf(
@@ -405,9 +409,11 @@ def level2():
405409
406410
def main_loop():
407411
"""Main test loop with different execution paths."""
412+
max_iterations = 1000
413+
408414
iteration = 0
409415
410-
while True:
416+
while iteration < max_iterations:
411417
iteration += 1
412418
413419
# Different execution paths - focus on CPU intensive work
@@ -434,9 +440,10 @@ def test_sampling_basic_functionality(self):
434440
mock.patch("sys.stdout", captured_output),
435441
):
436442
try:
443+
# Sample for up to SHORT_TIMEOUT seconds, but process exits after fixed iterations
437444
profiling.sampling.sample.sample(
438445
subproc.process.pid,
439-
duration_sec=2,
446+
duration_sec=SHORT_TIMEOUT,
440447
sample_interval_usec=1000, # 1ms
441448
show_summary=False,
442449
)
@@ -578,7 +585,8 @@ def test_sample_target_script(self):
578585
script_file.flush()
579586
self.addCleanup(close_and_unlink, script_file)
580587

581-
test_args = ["profiling.sampling.sample", "-d", "1", script_file.name]
588+
# Sample for up to SHORT_TIMEOUT seconds, but process exits after fixed iterations
589+
test_args = ["profiling.sampling.sample", "-d", PROFILING_TIMEOUT, script_file.name]
582590

583591
with (
584592
mock.patch("sys.argv", test_args),
@@ -612,7 +620,7 @@ def test_sample_target_module(self):
612620
test_args = [
613621
"profiling.sampling.sample",
614622
"-d",
615-
"1",
623+
PROFILING_TIMEOUT,
616624
"-m",
617625
"test_module",
618626
]

0 commit comments

Comments
 (0)