Skip to content

CI: spread the tests over the shards by cost, not by name (DRAFT for review) - #1

Draft
ArubikU wants to merge 4 commits into
ci-basefrom
ci-balanced-shards
Draft

ArubikU wants to merge 4 commits into
ci-basefrom
ci-balanced-shards

Conversation

@ArubikU

@ArubikU ArubikU commented Nov 26, 2026

Copy link
Copy Markdown
Owner

DRAFT for review. Not sent upstream. Branch ci-balanced-shards on ArubikU/hpx, four commits on top of master b9a1a8203.

Proposed Changes

Split the tests of the sharded CI workflows by interleaving their targets, not by consecutive names, so that no shard gets all the expensive tests.

  • generate_test_matrix.py: new option --split {contiguous,interleaved}. The default, contiguous, is the current behaviour; its output is identical byte for byte (checked on the 3,288 tests of a real run). interleaved deals the targets out one by one; tests of the same target (the same program for each parcelport) stay together, so a target is still built in one shard only.
  • test_generate_test_matrix.py: 15 tests of both splits.
  • sharded-build-and-test.yml: new input split, default contiguous, passed to the script through the environment. No caller changes.
  • build-and-test.yml (Linux, Debug, all tests): split: interleaved, as a pilot.
  • ci-scripts-tests.yml: a workflow that runs those tests when .github/ci-scripts changes (seconds).

Each commit stands alone and can be dropped. Dropping the last two leaves a change that no workflow uses.

Any background context you want to provide?

The problem, measured

Head commit of a recent pull request (TheHPXProject#8240), taken from the Actions API:

workflow runs / jobs 52 / 155
sum of job minutes 3,544 (59 machine-hours)
start to end of the whole pull request 104 min

Two sharded workflows decide that time. A sharded workflow builds the libraries in one job, then runs the tests in shards that each build and run their part. The pull request is as slow as libraries + slowest shard:

workflow libraries job shards tests per shard slowest shard median shard start to end
Linux (Debug, all tests) 27 min 25 132 (120 in the last) 52 min 26 min 79 min
Windows (Release, VS2022, C++20 Modules) 29 min 20 84 75 min 12 min 104 min

Every shard has the same number of tests and the cost is not the same by far. In the Linux run:

shard ninja steps build time tests run ctest time
slowest (24-runtime_configuration-serialization) 240 44.0 min 132 401 s
median (16-futures-algorithms) 243 23.2 min 132 118 s
fastest (13-synchronization-local) 14 0.8 min 132 629 s

11 of the 25 shards built nothing at all (their tests were built by the libraries job), and one of those took 38 minutes only running tests.

The cause

generate_test_matrix.py sorts the test names and cuts them in chunks of ceil(n / shards) consecutive tests:

bucket_size = math.ceil(total_tests / num_buckets)
...
bucket_tests = tests[start:end]

The number of tests says little about their cost, and tests of neighbouring names are alike, so the heavy ones fall into a few shards. In the Linux run the slowest shard holds 60 of the 90 segmented_algorithms tests of the whole run; in the Windows run the slowest shard (18-schedulers-segmented_algorithms) holds 56 of its 61.

The change, replayed on the measured costs

I ran the real script on the test names of both runs and priced each shard with the build time and the ctest time measured in that run. The model spreads the build time of a shard equally over its targets, so it keeps the differences between shards (the cause) and ignores those inside a shard. On the current split it reproduces the observed shards with a mean error of 0.5 minutes.

split slowest shard median start to end
Linux (Debug, all tests) contiguous 52 min 26 min ~78 min
interleaved 31 min 28 min ~57 min
Windows (VS2022, C++20 Modules) contiguous 75 min 12 min ~103 min
interleaved 22 min 17 min ~51 min

The total work does not change (709 and 345 job minutes in the model).

This is a model, not a measurement. It is an upper bound: if a few single targets are very heavy it is optimistic. The run of this pull request on the Linux workflow is the measurement, see below.

What I checked

how
the default is unchanged the old and the new script give the same 458,520 bytes on the 3,288 tests of the real run, with --exclude .github/workflows/exclude.targets
the tests catch mistakes four deliberate breaks of split_interleaved / split_contiguous each fail some test
the step works the body of the "Generate Test Matrix" step, with ctest -N replaced by the real list, runs under bash with both values of SPLIT
no test depends on another the only test properties set in the CMake files are RUN_SERIAL (a test alone in its ctest run), TIMEOUT, WILL_FAIL and ENVIRONMENT: no DEPENDS, FIXTURES_* or RESOURCE_LOCK
the YAML the three files parse; the new input is declared once and read once
hygiene no tabs, non-ASCII, trailing whitespace; SPDX header; LF in the committed files

Run the tests yourself:

python3 .github/ci-scripts/test_generate_test_matrix.py

and compare the splits on any ctest -N output:

ctest -N | python3 .github/ci-scripts/generate_test_matrix.py 25 --split contiguous  > a.json
ctest -N | python3 .github/ci-scripts/generate_test_matrix.py 25 --split interleaved > b.json

What I did not check

  • Only the Linux workflow (Clang, Debug) and one Windows run were replayed; macOS and the Release modules builds were not.
  • No real CI run with interleaved yet. That needs a run of the workflow on this branch.
  • Heavy tests that share a machine with different neighbours now: with a different mix of concurrent tests, a timing-sensitive test could behave differently. I have no data on this.
  • Windows runners: the same change is one line in each of two files (windows_release_vs2022_modules.yml, windows_release_vs2026_modules.yml) once the Linux run confirms it. Not done here.

Risks and how they are contained

  • Check names change for the Linux workflow, from 24-runtime_configuration-serialization (132) to 24 (132). Only Codacy is a required check on master, so no rule refers to them. A person looking for "the segmented algorithms shard" now reads the log.
  • A different shard fails for the same test than before. The failing test is still named in the log of the shard.
  • Rollback is one line (split: interleaved in build-and-test.yml).
  • The new input is read through an environment variable, not pasted into the script, unlike the older inputs.

How to confirm it (needs a run)

Run Linux (Debug, all tests) on this branch and on its base with workflow_dispatch, on the same commit, and compare:

  1. the slowest shard and the median (Actions API, jobs),
  2. start to end of the workflow,
  3. the sum of job minutes (it should stay about the same),
  4. that all tests pass in both.

A single pair of runs says little about the noise of the runners; three each would.

Not in this pull request

Things I saw while measuring that this change does not touch. They are separate decisions and I did not want to mix them in:

  • On master, only Codacy Static Code Analysis is a required status check. The 150 or so CI jobs are not required, and there is no merge queue (no merge_group trigger anywhere). On 2026-11-21 ten merges in a row left master red for 26 minutes: Match split credits to the ids being serialized by their gid TheHPXProject/hpx#8146 and Find the extra data of archives and futures in constant time TheHPXProject/hpx#8167 were merged one minute apart and do not compile together (post_receive_actions.cpp:28). A merge queue needs both a repository setting and required checks, which is for the maintainers.
  • The Linux (Debug, all tests) build passes -DCMAKE_CXX_CLANG_TIDY=clang-tidy-21, so clang-tidy runs on every translation unit while building. I did not measure what it adds to the 735 job minutes of that workflow. If it is a lot, running it on the changed files only is the next thing to look at.
  • There is no compiler cache (ccache or similar) in any workflow; the only cache is for the Windows dependencies.
  • Documentation Build takes 88 minutes of one job on every pull request. The single HTML (20 min) and PDF (34 min) steps could be left for master and releases, but that is 54 minutes, 1.5% of the job minutes, and it is not on the path that sets the time from start to end, so I left it alone.

Checklist

Not all points below apply to all pull requests.

  • I have added a new feature and have added tests to go along with it.
  • I have fixed a bug and have added a regression test.
  • I have added a test using random numbers; I have made sure it uses a seed, and that random numbers generated are valid inputs for the tests.

generate_test_matrix.py splits the tests of a CI run into consecutive
chunks, by name, with the same number of tests each. What a test costs
(building its target and running it) varies by far more than the number of
tests, and tests of neighbouring names cost about the same, so the expensive
ones end up in a few shards.

In a Linux (Debug, all tests) run on a pull request every shard had 132
tests (120 in the last), but the slowest shard took 52 minutes and the median
26. In a Windows (Release, VS2022, C++20 Modules) run the slowest took 75
minutes and the median 12; there the slowest shard decided how long the pull
request took.

--split interleaved deals the targets out one by one instead. Tests of the
same target (the same program run for each parcelport) stay together, so a
target is still built in one shard only. The default is the existing split,
and its output is the same, byte for byte, as before.

Add tests for both splits.

Signed-off-by: Arubik <102335860+ArubikU@users.noreply.github.com>
Add the input split, 'contiguous' or 'interleaved', and pass it to
generate_test_matrix.py. The default is contiguous, which is what the
workflow did so far, so no caller changes.

The value goes through the environment and not into the script text, as
the other inputs do, so that it can't be used to inject a command.

Signed-off-by: Arubik <102335860+ArubikU@users.noreply.github.com>
Use the interleaved split in the workflow that costs the most (27 jobs,
about 735 job minutes for a pull request, 79 minutes from start to end).

Replaying the measured costs of one run with this split gives a slowest
shard of about 31 minutes instead of 52, and about 57 minutes for the whole
workflow instead of 78. The total work stays the same. This is a model, not
a measurement: it spreads the build time of each shard equally over its
targets. The run of this change is the measurement.

The names of the checks change, from '24-runtime_configuration-serialization
(132)' to '24 (132)'. Undoing this is removing the line.

Signed-off-by: Arubik <102335860+ArubikU@users.noreply.github.com>
The scripts in .github/ci-scripts decide how the tests of every pull request
are split over the CI jobs. Run their tests when they change: the workflow
takes seconds and does not run for other changes.

Signed-off-by: Arubik <102335860+ArubikU@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant