Test sharding in CI
Why an even split of test files can leave one worker doing most of the waiting.

Parallel tests don't finish when the average worker is done. The last worker sets the wait.
A test run can have eight green jobs and still keep you waiting on the ninth. When I worked on test sharding in Automated Security Helper, I wanted to know how much of that wait came from the tests themselves and how much came from the way we divided them. The two problems need different fixes.
The old assignment gave each test module a stable home by hashing its name. That made it easy to reproduce a split and to absorb new modules. But it did not know what a module cost to run. Equal numbers of files looked fair on paper. They could put two slow modules in one worker and a handful of quick ones in another.
Equal files are not equal work.
I drew the workers as lanes and the tests as blocks. If two long blocks land on the same lane, the empty space in the other lanes does not help. They cannot go back in time and pick up the work. If the test job is a gate, its finish time depends on the heaviest lane, not the mean.
That changed the question I asked of the planner. It didn't need to predict the next run down to the second. It had to make a better decision about where the expensive work went with the information we already had.
Measure the wait reviewers actually see.
- Look at the last worker to finish
- Separate test execution from setup
- Ask which work could have gone elsewhere
A test plan can balance recorded execution time without accounting for runner startup, contention, or variance.
Give the heavy work a place first.
In the change I submitted, recorded durations drive a longest-first plan. Start with the largest job, put it on the least loaded lane, and repeat. Smaller pieces can fill the gaps that are left. There was also a case where the module itself was too big to balance as one unit. Splitting that module helped in a way that shuffling it around never could.
On historical Python 3.12 Windows integration results, the heaviest lane in the replay went from 4,708 to 1,695 test-seconds. An even share of the recorded work would have been about 1,659. Those are modeled loads, not shorter wall-clock times measured after deployment. A replay tells me the plan is worth trying. A real run tells me whether the saved waiting survived contact with the runners.
A new test cannot wait for a timing record before it is allowed to run. The plan keeps deterministic hash assignment as a fallback for tests it hasn't seen. If the plan is malformed, the tooling rejects it. Missing history and broken configuration get different responses.
Prove the faster plan kept the tests.
There is a straightforward way to get a faster suite that nobody wants: accidentally stop running part of it. I compared the original collection with the union collected by the new shards. The review records 7,832 integration tests and 61,601 unit tests in that check, with no dropped or duplicated tests. Collection establishes membership, not whether execution will pass.
I would keep the assignment and its proof apart. Show that balancing reduces the modeled peak. Separately show that every test still has exactly one home. If one check fails, you can tell whether you lost the time savings or the meaning of the gate.
If you have already added workers and the suite still feels slow, what does the last worker know about your tests that the scheduler doesn't?
What I take from it
Balance the time people wait, then prove that every test still runs exactly once.
Code and further reading
About the author
- Principal AI Architect. I help teams govern, ship, and scale AI, security, cloud, and developer systems. Illustrated essays and selected work at https://mh0pe.github.io/
Comments
Join the discussionNo comments yet. Be the first to comment.



