From 20a631ea708734d4f8ea33cf84e93661fd0cbb68 Mon Sep 17 00:00:00 2001 From: safishamsi Date: Mon, 7 Sep 2026 20:30:19 +0100 Subject: [PATCH] test(ts): measure CPU time in the normalizer scaling test to de-flake it perf_counter counts wall-clock, which includes time the process is preempted off-CPU during a busy full-suite run, inflating the larger measurement and flaking the ratio. process_time counts only CPU work, isolating the algorithmic scaling. Follow-up to #3402. --- tests/test_ts_import_type_arguments.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/test_ts_import_type_arguments.py b/tests/test_ts_import_type_arguments.py index 097caf1ca..1bf6db2a8 100644 --- a/tests/test_ts_import_type_arguments.py +++ b/tests/test_ts_import_type_arguments.py @@ -295,8 +295,13 @@ def test_ts_normalizer_scales_linearly_on_large_files(): each match by scanning every range made extraction quadratic, so `extract` spun at 100% CPU in the regex/scan path and never finished. - Assert on scaling, not wall-clock, so the test is not machine-dependent: - doubling the input must not roughly quadruple the time. + Assert on scaling, not absolute wall-clock, so the test is not + machine-dependent: doubling the input must not roughly quadruple the time. + Measure CPU time (``process_time``), not wall-clock (``perf_counter``): under + a busy full-suite run the process is preempted, and wall-clock would count + time slices spent scheduled off-CPU — inflating the larger measurement and + flaking the ratio. CPU time counts only work actually done, so it isolates + the algorithmic scaling regardless of load. """ import time @@ -309,13 +314,13 @@ def test_ts_normalizer_scales_linearly_on_large_files(): def timed(n: int) -> float: source = build(n) - start = time.perf_counter() + start = time.process_time() _normalize_ts_import_types(source) - return time.perf_counter() - start + return time.process_time() - start timed(200) # warm the grammar/parser import off the measured path - small = min(timed(1000) for _ in range(3)) - large = min(timed(2000) for _ in range(3)) + small = min(timed(1000) for _ in range(5)) + large = min(timed(2000) for _ in range(5)) # Linear work doubles (~2x). Quadratic work quadruples (~4x). A generous # 3x ceiling separates the two without being flaky under load.