Sub topic 13
Choosing how many shards to run
This chapter assumes you can run a matrix-based CI pipeline and read YAML. You should also know how a test runner's --shard=index/total option works. More shards finish sooner, but only up to a point. Past that point you pay for extra jobs and get little speedup.
The wall-clock model
Each shard pays a fixed setup cost before its first test runs. That cost covers checkout, dependency install, browser download and build. If the whole suite takes T seconds of test time, N shards take roughly:
wall_clock ≈ setup + (T / N) + skewskew is the extra time the slowest shard takes compared with a perfectly even split. Adding shards shrinks T / N but leaves setup unchanged. Skew usually grows too, because small shards are more sensitive to one slow file.
Cost grows differently. You pay for N × (setup + T / N), which simplifies to N × setup + T. Every extra shard adds one more copy of the setup time to the bill.
Finding the knee
The knee is where adding a shard stops saving meaningful time. You can estimate it with a short script.
def estimate(total_test_secs: float, setup_secs: float, shards: int, skew: float = 0.1):
"""Return (wall_clock, billed_seconds) for a given shard count.
skew is the fraction by which the slowest shard exceeds an even split.
"""
per_shard = total_test_secs / shards
wall = setup_secs + per_shard * (1 + skew)
billed = shards * (setup_secs + per_shard)
return wall, billed
for n in (1, 2, 4, 8, 16, 32):
wall, billed = estimate(total_test_secs=1800, setup_secs=90, shards=n)
print(f"{n:>2} shards: wall {wall / 60:5.1f} min, billed {billed / 60:6.1f} min")With a 30-minute suite and 90 seconds of setup, going from 1 to 4 shards cuts the wall-clock time from about 33 minutes to about 10. Going from 16 to 32 saves only about a minute and a half, but it doubles the billed setup time.
Practical limits
Several constraints cap the useful shard count before the math does:
- Largest single file. A shard cannot finish sooner than its longest test file. If one file takes 4 minutes, more than
T / 240shards will not help. - Runner concurrency. Your CI plan may allow only a fixed number of simultaneous jobs. Extra shards then queue, and queue time counts as wall-clock time.
- Shared resources. Shards that hit the same database, staging server or API rate limit can slow each other down or cause failures.
- Flakiness exposure. Each additional job is another chance for an infrastructure failure, so overall reliability drops slightly as
Nrises.
A simple selection procedure
- Record total test time and setup time from a recent run.
- Set a target, such as "feedback in under 10 minutes".
- Pick the smallest
Nwhereestimatemeets the target. - Check that
Nfits your runner concurrency and that no single file exceeds the per-shard budget. - Run it for a week, then compare actual shard durations against the estimate.
Exercise
A suite has 1,200 seconds of test time, each shard needs 120 seconds of setup, and you assume no skew. How long is the wall-clock time with 4 shards and with 8 shards, and how many billed seconds does each use?
Show answer
With 4 shards, each runs 300 seconds of tests, so wall-clock is 120 + 300 = 420 seconds and billed time is 4 × 420 = 1,680 seconds. With 8 shards, each runs 150 seconds of tests, so wall-clock is 120 + 150 = 270 seconds and billed time is 8 × 270 = 2,160 seconds. Doubling the shards saves 150 seconds of wall-clock time and costs 480 more billed seconds.