Skip to content

perf(ci): each test shard collects only its own test files #2371

Closed

Description

@dan-petty

Problem or Motivation

The test row runs in four shards. Each shard runs -n logical xdist workers, which is 4 on the runner. pytest-split picks the tests of a shard in pytest_collection_modifyitems (trylast=True, pytest_split/plugin.py, version 0.11.0). That hook runs after collection. So each of the 16 workers still collects the whole suite. At origin/main 4a2d958 (checked 2026-11-06; counts in this body are for that commit and move with main) that is 19,514 tests in 830 files. Then the worker drops three quarters of them.

Evidence from the comment of 2026-11-05 on this issue (one full run, 16-CPU host, 19,074 tests):

  • Collection costs 31.8 s of CPU in each worker. Over 16 workers it is 508.9 s, which is 13.5% of the 3,769.5 s of CPU of the run.
  • Worker start is 2.8% of the CPU. A preload fixture gives 0%. Fewer workers raise the wall time. So the first plan of this issue cannot be built as written.
  • On 2026-11-06 devops ci job-time gave a mean of 292 s for Tests & Coverage on one read and 277 s on a later read. The 20-run window moves and holds outliers, so no fixed baseline is used. The limit is 300 s (AGENTS.md rule 5).
  • The gate step of a shard took 115 s to 197 s in three recent runs.

Collection is the largest cost that all workers repeat. A shard needs only a quarter of the files. I measured it on 2026-11-06 at origin/main 4a2d958. The command was pytest --collect-only -q -n 0 -p no:cacheprovider, pinned with taskset -c 0-3. I ran the whole suite three times and each quarter three times. The files were dealt by test count. The host was loaded (load average 8 to 17), so only the ratio is useful.

  • The whole suite took 26.8 s, 25.4 s and 32.7 s of CPU. The mean is 29.0 s.
  • A quarter took 19.1 s on average over 12 runs (range 13.3 s to 24.0 s). That is 66% of the whole suite.
  • The cold run in the earlier comment gave 20.6 s against 38.4 s, which is 54%.

A quarter of the files does not cost a quarter of the CPU. The imports of shared product modules do not shrink with the number of files. So the saving is smaller than the first estimate.

  • Expected CPU saving: 16 workers x (29.0 s - 19.1 s), about 158 s. That is about 4% of one run.
  • Expected wall time saving: about 10 s to 15 s in each shard. The 4 workers of a shard collect at the same time on 4 CPUs.
  • I cannot test this here. The runner has 4 CPUs and the host is loaded. The pull request measures it, as the acceptance criteria say.

This issue was filed as a sub-issue of #2356, which is closed.

Proposed Solution & Architectural Design

Each shard passes the paths of its own test files to pytest. Pytest then collects only those files. No worker imports a test module of another shard. No plugin is needed.

  1. Shard files. A function in src/devops_cli/commands/ci.py splits the test modules into shards lists and returns list number shard.
    • It reuses _directory_modules("tests", root). That function gives the same 830 files that pytest collects (checked: the symmetric difference is empty).
    • It weighs each file by its number of lines.
    • It deals the files, heaviest first, to the shard with the lowest weight so far. A tie goes to the lower shard number.
    • It stores no data, so no file can go out of date. Each shard computes the same split from the same checkout.
  2. Test row command. With --shards above 1, _resolve_pytest_cmd ends with the sorted file paths of the shard.
    • It drops --splits, --group and --splitting-algorithm.
    • It keeps --cov=src, --cov-report=, --cov-fail-under=0 and --snapshot-warn-unused. The snapshots of the other shards are still unused in a shard.
  3. Remove pytest-split. Only pyproject.toml, uv.lock, ci.py, the shard test, two documents and the workflow comment name it. Delete the dependency, run uv lock, and fix the text.
  4. Spec. Change openspec/specs/ci-test-shards/spec.md through a delta in the change folder (see below).
  5. Tests first (docs/TESTING.md). Write the tests, see them fail, then write the code.

Balance. The split by lines gives 4,834, 5,181, 4,971 and 4,528 tests per shard at 4a2d958. The largest is 6.2% above the mean. A split by file size gave 14.4%, so it is not used. A split by test count is exact, but it needs a collection or a stored file. The current split is by test count too. There is no .test_durations file, so least_duration has no durations. If the shard times spread too far after the change, a follow-up item can add a stored per-file duration file. That is out of scope here.

Other tools, and why not:

  • pytest-split deselects after collection. It has no file-level option. Its algorithms take test items.
  • pytest-xdist --dist loadfile or loadgroup change how collected tests go to workers. Every worker still collects all tests.
  • pytest-test-groups --test-group-by filename splits by file. Its README says it runs a subset of the collected tests, so I expect it to collect first. I did not read its source, so this is an expectation and not a verified fact.
  • pytest-shard splits by test case only.
  • -p no:cacheprovider removes no collection work.
  • A pytest_ignore_collect hook can also skip files before collection. It needs a conftest plugin and a --shard option. A list of paths does the same with no plugin. I did not check any outside measurement of this hook, and the decision does not need one.

Cost if built (AGENTS.md rule 3): about 30,000 tokens to build, about 2,000 tokens a year to keep, and about half a day elapsed. The build is one function of about 25 lines, three tests, the spec delta, four documents and the lock file. The function replaces a dependency, so the net code is small.

OpenSpec. The change folder openspec/changes/2371-measure-and-cut-what-each-of/ is already on main (plan pull request #2986, merged). The implementation pull request rewrites its proposal.md, design.md and tasks.md for this scope. It removes skip_specs: true from .openspec.yaml. It adds specs/ci-test-shards/spec.md with this delta:

  • MODIFIED requirement "The test row runs one shard of the suite". With --shards above 1, the command of the test row SHALL hold --cov=src and the paths of the test modules of shard <n>. It SHALL hold none of --splits, --group and --splitting-algorithm.
    • Scenario "Shard two of four": the planned command holds the paths of shard 2 and no --splits.
    • Scenario "No shard": unchanged.
    • Scenario "A shard above the total": unchanged.
  • ADDED requirement "Each shard collects only its own test files". The shards SHALL partition the test modules of tests/. Each module SHALL be in exactly one shard. The split SHALL be the same in every shard.
    • Scenario "Four shards cover every test module once".
    • Scenario "The split is the same in every call".
    • Scenario "The split has no stored input".
  • The requirement "A shard checks no floor and prints no report" stays as it is.

Text to change in the same pull request, so that it reads right after the merge:

  • the comment above test-shard in .github/workflows/ci.yml (it names pytest-split);
  • the docstring of _resolve_pytest_cmd;
  • docs/SDLC.md, line 217;
  • docs/TESTING.md, section Run time, second bullet;
  • the module docstring of tests/commands/test_ci_shard.py.

Paragraph for docs/TESTING.md, section Run time. It replaces the words "split by pytest-split" in the second bullet. It adds this bullet after the third bullet:

A shard collects only its own test files. Pytest collects every file it gets, in every xdist worker. pytest-split deselected tests after that, so each worker collected the whole suite. That cost 31.8 s of CPU in each of 16 workers, which is 13.5% of the CPU of a run (2026-11-05, 16-CPU host, 19,074 tests). Now devops ci --shard <n> --shards <total> passes the paths of the test modules of the shard to pytest. The split is by lines of test code, heaviest file first, and it stores no data. A new test module joins a shard with no other step. A quarter of the files collects in about two thirds of the CPU time of the whole suite, not a quarter. The imports of shared modules stay. In the same run, worker start was 2.8% of the CPU and the controller was 1.4%. A preload fixture gave no saving. A test must not depend on a test of another file, because a shard holds only some of the files.

Acceptance criteria

Tests come first. The new tests fail on the current code and pass after the change (devops ci fail-to-pass --base origin/main).

  1. Partition. A test in tests/commands/test_ci_shard.py builds the file lists of shards 1 to 4 for the real tests/ tree. It asserts that every module from _directory_modules("tests", root) is in exactly one list, and that no list is empty. It runs the same check with 2 and 3 shards.
  2. Same split everywhere. A test asserts that two calls for the same shard return the same list. It asserts that the order in which the shards are asked does not change the lists.
  3. Command. A test asserts that the planned command of devops ci --dry-run --only test --shard 2 --shards 4 holds the paths of shard 2. It asserts that the command holds none of --splits, --group and --splitting-algorithm. It asserts that the unsharded row is unchanged.
  4. Same tests as before. The pull request comment states the test counts. The counts that pytest --collect-only -q -n 0 gives for the four lists add up to one total. That total equals the count for the whole suite at the base of the pull request (19,514 at 4a2d958). The largest shard is at most 10% above the mean (6.2% at 4a2d958). This checks test counts only. Criterion 6a checks shard time.
  5. Shorter collection. The pull request comment states the CPU time of pytest --collect-only -q -n 0 -p no:cacheprovider, user plus system, under taskset -c 0-3. One shard list takes at most 75% of the CPU time of the whole suite. Use the mean of three runs of each, and record the host load before each run. At 4a2d958 the ratio is 66%.
  6. Lower CPU. The pull request comment states the summed user plus system CPU time of the four shard commands (devops ci --only test --shard <n> --shards 4). Run them one after the other under taskset -c 0-3.
    • The sum is at least 3% below the sum before the change. Use the mean of three runs of each.
    • Run on an idle host (load average under 2) and record the load. The expected saving is about 4%.
    • If no idle host is available, the comment says so. Criterion 5 then stays the hard check, because it is the most direct one. A CPU result from a loaded host does not fail the pull request.
      6a. Shard time. The old split dealt single tests. The new split deals whole files, so slow files can group in one shard. The pull request comment records the duration of the four Tests & Coverage Quality Gate steps from the Actions API for one run after the change. The slowest shard is at most 15% above the mean of the four. If it is not, a follow-up item adds a stored per-file duration file.
  7. Runner time. Just before the merge, the pull request comment records the devops ci job-time mean for Tests & Coverage. After the merge, devops ci job-time reports a mean for the 20 runs after the change. The new mean SHALL be at most 300 s. It SHALL NOT be above the recorded mean before the merge. A comment on this issue records both numbers. The mean of the step Tests & Coverage Quality Gate of a shard is expected to fall by 10 s to 15 s. That gain is inside the spread of the 20-run window (261 s to 331 s on 2026-11-06), so noise can hide it.

Which criterion decides. Criteria 1 to 5, 8 and 9 are hard checks. Criterion 6 is informative on a loaded host. Criterion 6a and criterion 7 together are the final judge. If criterion 7 is inside the noise, criterion 6a and criterion 5 decide, and the comment says so.
8. Dependency. pytest-split is gone from pyproject.toml and uv.lock. git grep -il pytest-split finds only files under openspec/changes/archive/.
9. Spec and documents. openspec validate passes with the ci-test-shards delta. docs/TESTING.md, docs/SDLC.md and ci.yml hold no text that says pytest-split splits the shards.

This fits in one pull request. It depends on no other item.

Target Subsystem

SDLC, Git & PR Governance. The parts are devops ci, ci.yml and openspec/specs/ci-test-shards.

Existing Tool Considered

  • pytest takes file paths as arguments and then collects only those files. This open tool does the work.
  • pytest-split 0.11.0 is in use now. It selects after collection and cannot split by file. This item removes it.
  • pytest-xdist (-n logical --dist worksteal) stays. It spreads the tests of a shard over the workers.
  • pytest-test-groups, pytest-shard and a pytest_ignore_collect plugin are rejected, for the reasons above.
  • The new function is about 25 lines and replaces a dependency.

Activity

  1. added
    source/agent
    type/infra
    on Oct 27, 2026
  2. dan-petty commented on Oct 27, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones on the board.

    • Type: type/infra. It is a CI performance change.
    • Priority: P2-Medium.
    • Value: Medium. It saves CI minutes for every run, but product behavior is unchanged.
    • Effort: Medium.
  3. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Status set to Ready at bb46660 (see proposed design in issue body).

  4. added
    priority/p2-medium
    status/ready
    value/medium
    effort/medium
    scope/ci
    on Oct 28, 2026
  5. devops-cli-service commented on Nov 1, 2026

    @devops-cli-service
    Contributor

    Staged: branch perf/2371-measure-and-cut-what-each-of, draft pull request #2986. The pull request plans the item and closes no issue.

    The task groups, with the tokens of the inputs of each step, for a budget of 40000 tokens:

    1. Add session fixture for heavy module imports: 3073 tokens
  6. 12 remaining items

  7. devops-cli-service commented on Nov 5, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at f481be8 (see proposed design in issue body).

  8. added
    status/planned
    and removed
    status/ready
    on Nov 5, 2026
  9. changed the title [-]perf(ci): measure and cut what each of the 16 test workers repeats at start[/-] [+]perf(ci): each test shard collects only its own test files[/+] on Nov 6, 2026
  10. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/planned
    status/in-progress
    on Nov 6, 2026
  11. removed
    status/in-review
    owner/agent
    on Nov 6, 2026
  12. devops-cli-service commented on Nov 6, 2026

    @devops-cli-service
    Contributor
  13. devops-cli-service commented on Nov 6, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-06 18:04 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/cisource/agenttype/infravalue/medium

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions