Repository navigation
fix(scripts): check_test_timing counts a sleep on a module loaded by name and scans the tests tree faster (#1110, #1177) - #1202
Conversation
…oaded by name (intel#1110) The checker followed only `import` statements, so `__import__("time").sleep(0.05)` and `importlib.import_module("time").sleep(1)` were invisible to it: `python scripts/check_test_timing.py` reported 0 calls while tests/test_container_agent.py polled a proxy log with `__import__("time").sleep(0.05)`. A call on `__import__("<module>")` (the builtin, not rebound by an assignment, parameter or `def`) or on `importlib.import_module("<module>")` / `import_module("<module>")` (resolved through the file's imports) now counts as `module.attr` when the module and attribute are in the targets. The argument must be a single string literal; a computed name is not followed, as before. A `# timing-ok: <reason>` waives it like any other call. test_proxy_logs_plain_http_failures_with_host_and_normalised_names waits for the proxy log with `poll_until` instead of a fixed-sleep loop. It was the only such call in tests/, so the baseline does not grow. Its growth baseline entry shrinks with the file. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
…or waiver and does not walk it (intel#1177) tests/test_check_test_timing.py::test_the_repo_tree_holds_its_own_baseline scans all of tests/ (637 files) and took 5.5 s of CPU on an idle host. Pinned to one CPU, with two copies of it running and `--timeout 10`, it fails on main (`1 failed` after 10.38 s), and the 60 s limit is reached on a runner that shares its CPUs among enough workers. No test writes into the real tests directory (every writer found uses `tmp_path`), so the scan is not racing a file that appears or goes away: the cause is the time of the scan. Most of that time is the scope walk and the tokenizer run over files that cannot hold a call. A call is spelt with `sleep` or `Timer` and a waiver with `timing-ok`, so an ASCII file with none of these words is now parsed, to keep reporting a syntax error, and not walked or tokenized. A file with a non-ASCII character is still scanned in full, since Python folds identifiers to NFKC (`time.sleep(1)` is `time.sleep(1)`). The whole-tree test takes 3.7 s, and two copies on one CPU pass with `--timeout 10`. tests/test_check_test_timing.py pins that the shortcut gives the same result as the full scan and does not walk a file without a marker. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 4 selected item(s). CI at Review details
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1202 +/- ##
==========================================
- Coverage 96.07% 96.07% -0.01%
==========================================
Files 496 495 -1
Lines 65276 65206 -70
Branches 11330 11314 -16
==========================================
- Hits 62717 62644 -73
+ Misses 1585 1584 -1
- Partials 974 978 +4 🚀 New features to boost your workflow:
|
vchamarthi
left a comment
There was a problem hiding this comment.
@antonwolfy, could you answer my 3 questions below?
I'll approve once that's done.
My questions before I approve:
mod = __import__("time")followed bymod.sleep(1)is not counted, and neither ist = importlib.import_module("time")followed byt.sleep(1). Decide whether that should count.importlib.import_module(name="time").sleep(1)is not counted, because a keyword argument is not read. State in the module docstring that only a positional literal is followed, or count the keyword form.- Issue #1177 says a bound on wall-clock time fails on a busy runner, and that a test which counts the work passes there. If the 60 s bound stays, say in the description why a count does not suit this test, since the description says the issue lists no fix options.
Also holding my approval:
- A check of the change fails on this head.
Optional, for later (2)
dokimi/scripts/check_test_timing.py:201: If I changeWATCHED[node.module]on line 201 toTARGETS[node.module],from importlib import *raises KeyError and every changed test still passes.dokimi/scripts/check_test_timing.py:338: If I change== 1on line 338 to>= 1,__import__("time", globals()).sleep(1)is counted and every changed test still passes.
What I checked on 22afbc5:
- Not passing: The full test suite fails with main merged in, at
tests/test_repo_batch_lifecycle.py::test_ctrl_c_starts_no_queued_cell,ci.yml:test / Run tests. - The 265 changed tests pass, and fail on main as expected.
- It merges cleanly onto main.
- When I broke the changed code on purpose, the tests caught all 12 breakages.
…he star import of importlib Settles the review questions on intel#1202. A module bound by `mod = __import__("time")` or `t = importlib.import_module("time")`, a keyword `name=`, and a load with a second argument are not counted: the module docstring now says so, and a test pins each. A test also pins `from importlib import *`, whose `WATCHED[...]` lookup raised KeyError when it read `TARGETS[...]`. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
napetrov
left a comment
There was a problem hiding this comment.
Change class: LOCAL (head 22afbc5)
- Contract: none
- Responsibility: none
- Dependency: none
- Review depth: skim: local change, check diff + tests
- Coverage gaps: none
The direct dynamic-import forms requested by #1110 now count. The scanner parses before taking the ASCII fast path, so it preserves syntax errors and handles Unicode identifiers. The previously reported Ctrl-C lifecycle test passed locally on a temporary merge with current main (1b24ab379). GitHub CI is green. I reviewed the timing checker from source. Its module-binding and positional-literal limits are consistent with the described scope.
@vchamarthi (#1202 (review)): The docstring and tests are in 0f812f4. The three questions, as the branch answers them:
The two optional points are already pinned: the description's mutants list reading The failing check: |
@napetrov (#1202 (review)): Approval that recaps its checks; it asks for no change, and the branch has no new commit this round. |
Summary
#1110:
scripts/check_test_timing.pyfollowed onlyimportstatements, so a sleep reached through__import__orimportlibwas invisible to it. Onmain,scan('__import__("time").sleep(1)\n', "x.py")returns({}, []),python scripts/check_test_timing.pyprintstest timing ratchet ok: 0 sleep/Timer call(s) in 0 file(s), andtests/test_container_agent.py:1106still polls the egress proxy's log with__import__("time").sleep(0.05)and has no baseline entry. The checker now counts it, and that test waits withpoll_until.#1177:
test_the_repo_tree_holds_its_own_baselinescans all 637 files oftests/and took 5.5 s on an idle host. The cause is the time: no test writes into the realtests/directory (every writer found usestmp_path), so the scan is not racing a file that appears or goes away. Pinned to one CPU with two copies of the test running and--timeout 10, it fails onmain(1 failed in 10.38 s, both copies). The scan now parses every file but walks and tokenizes only the ones that can hold a call, and the test takes 3.7 s; two copies on one CPU pass with--timeout 10(7.7 s).The timeout stays at 60 s. The issue lists no fix options, so the one taken is the cheaper scan, since a test of the whole tree has to scan it. A count does not suit this test: the 60 s is the suite's
pytest-timeout(pyproject.toml), not a bound the test asserts, and the test's work is fixed (parse every file oftests/and compare the result with the baseline), so there is no smaller amount of work to count instead; only the time per file can fall.Closes #1110
Closes #1177
Changes
scripts/check_test_timing.py: a call on__import__("<module>")(the builtin, not rebound by an assignment, a parameter or adef) or onimportlib.import_module("<module>")/import_module("<module>")(resolved through the file's imports) counts asmodule.attrwhen the module and attribute are targets. The argument must be one string literal; a computed name is not followed.# timing-ok: <reason>waives it. The module docstring lists the new forms._resolvekeeps its complexity cap by moving the loader check into_loaded.tests/test_container_agent.py:test_proxy_logs_plain_http_failures_with_host_and_normalised_nameswaits withpoll_until(lambda: len(proxy.log) >= 4, 2.5, interval=0.05). It was the only such call intests/, sotest_timing_baseline.jsondoes not change; the file's entry inscripts/growth_baseline.jsonshrinks by its two lines.tests/test_check_test_timing.py: a parametrized test for the loaded-by-name forms (counted, waived, non-literal, other module, shadowed__import__andimport_module).scripts/check_test_timing.py(Flaky on CI: tests/test_check_test_timing.py::test_the_repo_tree_holds_its_own_baseline #1177):scanparses every file, so a syntax error is still reported, and returns at once for an ASCII file with nosleep,Timerortiming-okin it. A non-ASCII file is scanned in full, because Python folds identifiers to NFKC (time.sleep(1)istime.sleep(1)).scripts/check_test_timing.py,tests/test_check_test_timing.py: the module docstring says a module bound by assignment (mod = __import__("time")), a keyword name (import_module(name="time")) and a load with a second argument (__import__("time", globals())) are not followed, and tests pin each, withfrom importlib import *.tests/test_check_test_timing.py(Flaky on CI: tests/test_check_test_timing.py::test_the_repo_tree_holds_its_own_baseline #1177): the shortcut gives the result of the full scan (count) for files with and without a marker, does not walk a file without one, and still raisesSyntaxErrorfor it.scripts/andtests/change.Risk
The checker counts more: a
__import__("time").sleeporimportlib.import_module("time").sleepadded totests/now fails the ratchet unless waived. None exists in the tree today (python scripts/check_test_timing.pyexits 0 with no baseline change). The #1177 shortcut leaves the result of every file as it was; only the time differs. Open PRs #1197, #1185, #1045 and #1011 also editscripts/growth_baseline.json, and #1011 editstests/test_container_agent.py: each touches other lines than this PR's (thetests/test_container_agent.pyentry here, line 1103 of that file), so they merge without a conflict.Testing
On Linux, at
acab195b(onmain316a3413):make ci-local: 38 passed, 0 failed, 15 skipped (matrix legs of other jobs, micromamba, semgrep, fuzz, deploy). 28440 tests passed, 23 skipped; the diff has no covered lines to measure. The run is on the tree withmainmerged in, andtests/test_repo_batch_lifecycle.py::test_ctrl_c_starts_no_queued_cellis among the passing tests.__import__("time").sleep(...)is not counted, so test_container_agent.py still polls with a fixed sleep #1110 onmain's code:tests/test_check_test_timing.pyfails 9 tests (the 7 loaded-by-name counts and 2 of the shortcut cases), the first on__import__("time").sleep(1)returning({}, [])instead of({"time.sleep": 1}, []); the file has 172 tests, 163 pass. Here all pass.main's code: two copies oftest_the_repo_tree_holds_its_own_baselineon one CPU with--timeout 10fail (1 failedin 10.38 s each); four copies fail on this branch too (10.7 s each, 3.7 s of CPU apiece), two pass.check_test_timing.py(builtin check, keywords,import_modulevia from-import, attribute check, counted names, literal-argument check, the ASCII guard, thetiming-okmarker, no shortcut, parse after the shortcut) fails a test of this PR or of the file. Two more: readingTARGETS[node.module]forfrom importlib import *raisesKeyErrorand fails the star-import case;len(load.args) >= 1counts__import__("time", globals()).sleep(1)and fails its case.Open questions
None.
Revert
A plain revert restores the old checker (it ignores a sleep on a module loaded by name) and the fixed-sleep wait in
test_container_agent.py. Nothing is stored.Checklist
pytestpassesruff,mypy, and schema checks passAGENTS.mdraise/exit and test names the doc, contract or issue it traces to.github/.licenserc.yaml)