The slowest shard is the build.
Why an even split of test files can leave one worker doing most of the waiting.
ASH #965: weight test shards by duration
Automated Security Helper Developer tools
At a glance
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.
Three workers run at the same time, but the first carries three long tests.
Each lane gets three test blocks. One lane still has much more work.
The three longest blocks are on the first worker, so the outlined finish line sits much farther away.
Diagram key
- Top laneWorker 1
- Three long tests set the finish time.
- Lower lanesWorkers 2 and 3
- Their three tests each are shorter.
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.
Elapsed time has more than one part as well. CI latency has more than one clock, on LinkedIn, opens in a new tab separates the time a job spends waiting for a runner from the time it spends running.
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.
The tiles keep their letters as they change which worker runs them.
The same blocks move into gentler loads across three workers.
Follow the same blocks, A through I, as they move. Watch the longest lane shrink. The historical replay models scheduled test-seconds, not observed CI wall time or passing tests.
Each worker gets one of the long blocks. Short blocks fill the gaps.
Diagram key
- Long labeled tilesLong blocks
- One moves into each of the three lanes.
- Dashed lineLast worker
- The finish moves earlier in this example.
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.
ASH's scan side has a placement problem of its own. Designing a scanner fan-out that can be collected honestly, on LinkedIn, opens in a new tab separates who owns each piece of work from who may announce the result.
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.
The new layout holds the same nine test blocks as the opening layout.
All nine illustrated blocks are still there, once each.
The labels A through I are unchanged even though the assignments are different.
Diagram key
- All three lanesA through I
- The same nine identities appear once each.
- Right of longest laneFinish line
- An estimate of the load, not a wall-clock measurement.
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?
Running shards in parallel also makes the suite a small concurrent system, on LinkedIn, opens in a new tab with deadlines, processes and cleanup of its own, which I wrote about on LinkedIn.
For a closer technical look
- Reported seconds are a replay of historical durations, not a post-merge benchmark or isolated Linux result.
- The counts describe a collect-only comparison. They are not claims about test pass rates.
What I take from it
Balance the time people wait, then prove that every test still runs exactly once.
Code and further reading
Covers work from November 2026Posted