Sub topic 1
Why Split a Test Suite Across CI Jobs
Prerequisites
This chapter assumes you can run a test suite from the command line, and you know what a CI job, step and runner are. It also assumes you can read a YAML configuration file. Later chapters use short Python and shell scripts.
The problem: slow feedback
Continuous integration (CI) runs your tests on every push. When the suite takes 40 minutes, developers wait or switch to other work. Fixes arrive late, and pull requests pile up. The time between a push and a pass or fail result is the feedback time.
Running the whole suite on one machine makes feedback time equal to the total test time plus setup. Faster hardware helps only a little. Splitting the suite is the more effective change.
The idea: shards
A shard is one slice of the test suite. Instead of one job running every test, you start several jobs at once, and each job runs only its own shard. If the work divides evenly, four shards cut a 40-minute run to about 10 minutes of testing.
The flow on the right is the pattern this tutorial builds. Each shard runs on its own runner (a machine or container that executes a job). The shards start together, and the pipeline succeeds only if every shard succeeds.
What limits the speedup
Adding shards does not divide the time forever. Three things set the limit.
- Setup cost. Every shard must check out code, install dependencies and build. If setup takes 3 minutes, each shard pays it again.
- Imbalance. The pipeline finishes when the slowest shard finishes. If one shard gets the slow tests, the others sit idle.
- Indivisible tests. One test file that takes 8 minutes cannot be split by file-level sharding. No shard can finish faster than that file.
A simple model captures this. Wall-clock time is roughly the setup time plus the time of the slowest shard.
def wall_clock(total_test_minutes, shards, setup_minutes, imbalance=1.0):
# imbalance of 1.0 means perfectly even; 1.2 means the slowest
# shard takes 20% longer than the average.
return setup_minutes + (total_test_minutes / shards) * imbalance
for n in (1, 2, 4, 8, 16):
print(n, round(wall_clock(40, n, 3, imbalance=1.2 if n > 1 else 1.0), 1))Running this prints 43.0, 27.0, 15.0, 9.0 and 6.0 minutes. Going from 1 to 4 shards saves 28 minutes. Going from 8 to 16 saves 3, while using twice the runner time.
When splitting is worth it
Sharding pays off when the suite is long enough that developers wait on it, and when tests can run independently of each other. It pays off less when:
- The suite already finishes in a few minutes.
- Setup takes longer than the tests.
- Tests share state, such as one database, and break when run in separate jobs.
The next chapters cover each of these: how job-level sharding differs from parallelism on one machine, how to make tests safe to split, and how to choose a splitting strategy.
Exercise
A suite takes 30 minutes of test time. Setup takes 2 minutes per job. You split it into 5 shards, and the slowest shard takes 25% longer than the average. Using the model in this chapter, what is the wall-clock time?
Show answer
Average shard test time is 30 / 5 = 6 minutes. The slowest shard takes 6 × 1.25 = 7.5 minutes. Adding 2 minutes of setup gives 9.5 minutes.
Exercise
Name two reasons that doubling the number of shards does not halve the wall-clock time.
Show answer
Any two of these: each shard repeats the setup work, the slowest shard sets the finish time so imbalance adds delay, and a single long test file cannot be divided across shards.