Skip to content

perf(tests): parse the source trees once per worker - #4850

Merged
dan-petty merged 3 commits into
mainfrom
perf/4700-parse-source-trees-once
Nov 11, 2026
Merged

dan-petty merged 3 commits into
mainfrom
perf/4700-parse-source-trees-once

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

Test modules that walk src/ or tests/ now read the per-process parse cache of tests/support/trees.py: the parsed_source_trees fixture for a walk of all of src/, and parse_python_file for a smaller walk. Before, each test parsed its files again, and three telemetry checks parsed the 799 files of src/ each time. The telemetry gauge check also makes its name tables once, and the emit check stops at a module that imports no emit.

The check that the project and tree fixtures each have one definition reads every module of tests/, so it moves to the test-tree row of devops ci, which parses those files once. The roadmap import check, which parsed 2,127 files in 10.65 s in CI, is deleted: mypy refuses a broken import in src/, and a broken import in a test fails that test.

Closes #4700

Change: openspec/changes/archive/2026-11-11-4700-parse-the-source-trees-once-per/

@dan-petty dan-petty added type/infra Type infra: the build, CI, images or cluster scope/tests The test suite, its fixtures, fakes and floor labels Nov 11, 2026
@dan-petty

Copy link
Copy Markdown
Owner Author

Measurements

All local numbers come from the devcontainer while other agent workflows ran. The load average was 25 to 80 on 16 CPUs, so wall times are not stable. The "before" checkout is origin/main, and the "after" checkout is this branch. The runs alternate in ABBA order.

Census of the parse sites (acceptance 1)

An ast-grep rule finds an ast.parse call inside a for loop or a comprehension over glob/rglob, under tests/ but not tests/support/:

Sites
Before (origin/main) 10 sites in 7 files
After 1 site: tests/gate/tree_checks/facades.py:102, which parses the Python blocks of Markdown files, not Python files

I read the files where a helper does the parse (test_github_session.py, test_ai_spend_pricing.py, test_source_tree.py, test_test_selection.py, tests/gate/swallowed_failures.py) by hand. Each now calls parse_python_file or reads parsed_source_trees.

The 4 heaviest files (acceptance 2)

Command: uv run pytest -n 4 -p no:cov -q tests/invariants/test_source_tree.py tests/telemetry/test_telemetry_instruments.py tests/invariants/test_hermetic_floor.py tests/invariants/test_architectural_invariants.py. The time is the pytest passed in value.

Round Before After
1 (before the telemetry walk commit) 221.6 s, 261.0 s 197.2 s, 195.5 s
2 (final code) 259.6 s, 198.4 s 162.1 s, 167.1 s

Round 2: the mean goes from 229.0 s to 164.6 s (-28%).

One process (-n 0), with a plugin that counts the ast.parse calls that do not come from pytest itself:

ast.parse calls CPU in ast.parse CPU of the process
Before 10,378 153.4 s 595.7 s
After 4,541 57.1 s 363.0 s

Not verified: the target of 15 s or less (from 26.5 s). This host could not give a stable absolute time. If the -28% holds on a calm host, 26.5 s becomes about 19 s. These costs stay after the change, and they are not raw parses of test files:

  • Each xdist worker that runs a test of src/ parses the 799 files of src/ once (parsed_source_trees).
  • The pytester runs of test_hermetic_floor.py register the support plugins again. Each registration parses the 100 support modules (devops_cli.core.test_selection.defines_fixture_or_hook and tests/support/bootstrap.py): 3,152 of the 4,541 calls.
  • Some tests are slow for other reasons: test_the_task_files_are_decommissioned (a search of the tree), test_a_moved_roadmap_module_keeps_its_history (git log --follow), and the pytester tests of the floor.

CI

Before: test_every_import_of_a_roadmap_module_names_a_module_that_exists took 10.65 s in shard 3 of a run of the 2026-11-11 audit. This branch deletes it. I add the job times of this pull request when its checks are complete.

Steps for a person

None. This change touches no cluster and no repository setting.

@dan-petty
dan-petty marked this pull request as ready for review November 11, 2026 22:52
@dan-petty
dan-petty merged commit 7a163a7 into main Nov 11, 2026
25 checks passed
@dan-petty
dan-petty deleted the perf/4700-parse-source-trees-once branch November 11, 2026 22:52
dan-petty pushed a commit that referenced this pull request Nov 11, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.114.1](v0.114.0...v0.114.1)
(2026-11-11)


### Performance Improvements

* **tests:** parse the source trees once per worker
([#4850](#4850))
([7a163a7](7a163a7))


### Code Refactoring

* **roadmap:** the package declares the workflow labels
([#4818](#4818))
([89926ff](89926ff))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: dan-petty-release[bot] <346998822+dan-petty-release[bot]@users.noreply.github.com>
@dan-petty

Copy link
Copy Markdown
Owner Author

CI job times (Actions API)

Seconds from started_at to completed_at of each job. The other rows are recent successful pull request runs of the CI Quality Gate that ran the full test shards.

Run Static Analysis Shard 1 Shard 2 Shard 3 Shard 4 Sum of shards
43111034063 (this pull request) 166 242 295 224 214 975
43109486029 143 208 291 247 244 990
43107281727 136 241 234 169 264 908
43106120332 115 222 304 210 188 924

The issue estimates a saving of 20 to 30 s of worker time in each run. That is smaller than the spread between runs (908 to 990 s), so the job times neither prove nor refute it. The local measurements in the comment above show the change of the 4 heaviest files. All 17 checks passed.

@dan-petty

Copy link
Copy Markdown
Owner Author

Review findings after the merge

This pull request is merged, so each finding goes to a new issue. Nothing in this pull request changes.

# Severity Finding Verdict Action
1 Major openspec/specs/roadmap-package-layout/spec.md keeps the scenario "No old roadmap module path remains" ("WHEN A test parses each import of devops_cli.roadmap in src/ and tests/"), but this pull request deleted that test and set skip_specs: true. Confirmed. git grep '#3187' origin/main -- 'tests/*.py' finds no import test. An AST probe finds 15 roadmap imports in if TYPE_CHECKING: blocks of tests/; mypy does not read tests/, so no tool checks them. Each of the 15 names a module that exists today. Filed #4882: a spec delta that modifies the requirement and names the proof that stays (mypy on src/, the test run for tests/).
2 Major Acceptance criterion 2 of #4700 ("the 4 heaviest files run in 15 s or less with -n 4 and no coverage") is not shown to be met, but #4700 is closed as completed with no follow-up issue. Confirmed. ABBA runs of the parent 66c9ef5a and the merge 7a163a72 give a ratio of 0.699 (pytest time) and 0.755 (CPU time). The baseline of 26.5 s becomes 18.5 s to 20.0 s, more than 15 s. The host was loaded (load 34 to 44 on 16 CPUs), so only the ratio is valid. Put the measurement on #4700 and filed #4890 (sub-issue of #4700) for the costs that stay: one src/ parse in each xdist worker, the telemetry walks and the in-process pytester runs.

dan-petty added a commit that referenced this pull request Nov 14, 2026
The requirement "Every caller imports a roadmap module by its path"
names the test
`test_every_import_of_a_roadmap_module_names_a_module_that_exists`.
#4850 deleted that test, and its change set `skip_specs: true`, so the
spec still names a check that no longer runs (#4882).

Closes #4882

Change: `openspec/changes/archive/2026-11-14-4882-layout-spec/`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope/tests The test suite, its fixtures, fakes and floor type/infra Type infra: the build, CI, images or cluster

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(tests): parse the source trees once per worker

1 participant