Sub topic 4
Ways to divide a suite: count, hash and timing
This chapter assumes you can run a test suite from the command line, read a CI YAML file, and write a short Python script. It compares three ways to decide which test files go to which shard. A shard is one slice of the suite, run by one parallel CI job.
What a splitting strategy decides
Every strategy answers the same question: given a list of test files and N shards, which shard runs each file? The answer should cover every file exactly once. A file that lands in no shard is silently never tested, and a file in two shards wastes time.
The strategies differ in two properties:
- Stability: whether a file stays in the same shard when you add or remove other files.
- Balance: whether the shards finish at about the same time.
Total wall-clock time equals the slowest shard, so balance matters most.
Count-based splitting
Sort the files, then give each shard an equal number of them. Shard 1 gets the first chunk, shard 2 the next, and so on. Or deal them out in turn, like cards.
This is simple and needs no stored data. Its weakness is that it assumes all files take equal time. One 120-second file next to nine 2-second files breaks that assumption. Adding a file also shifts every file after it into a different shard.
Hash-based splitting
Compute a hash of each file path and take it modulo the shard count. The hash decides the shard, and the other files do not affect it.
import zlib
def shard_for(path: str, total: int) -> int:
# crc32 is the same on every machine and run.
# Python's built-in hash() is salted per process, so don't use it here.
return zlib.crc32(path.encode("utf-8")) % total
print(shard_for("tests/login.test.js", 4))Adding a file never moves the existing files, so caches and per-shard history stay valid. Hashing still ignores duration. With few files, the split can also be lopsided by chance.
Timing-based splitting
Record how long each file took in a previous run, then pack files into shards so the totals are close. A simple method is the longest-processing-time-first heuristic. Sort files from slowest to fastest, and give each one to the shard with the least work so far.
import heapq
def split_by_time(durations: dict, shards: int) -> list:
"""Assign each test file to the currently least-loaded shard, longest files first."""
heap = [(0.0, i, []) for i in range(shards)]
heapq.heapify(heap)
for name, secs in sorted(durations.items(), key=lambda kv: kv[1], reverse=True):
total, i, items = heapq.heappop(heap)
items.append(name)
heapq.heappush(heap, (total + secs, i, items))
return [(total, items) for total, i, items in sorted(heap, key=lambda b: b[1])]
durations = {"a.test": 120, "b.test": 90, "c.test": 60, "d.test": 50, "e.test": 40, "f.test": 30}
for n, (total, items) in enumerate(split_by_time(durations, 2), start=1):
print(n, total, items)The files add up to 390 seconds, so a perfect split would be 195 seconds per shard. The heuristic gives:
1 200.0 ['a.test', 'd.test', 'f.test']
2 190.0 ['b.test', 'c.test', 'e.test']The slowest shard takes 200 seconds. That is close to the ideal, and much better than a count-based split that could put a.test and b.test together (210 seconds).
Comparing the strategies
| Strategy | Needs stored data | Stable when files change | Balance |
|---|---|---|---|
| Count | No | No | Poor if durations vary |
| Hash | No | Yes | Random, ignores duration |
| Timing | Yes | No, unless you keep old assignments | Best |
For a small suite with similar-sized files, hashing or counting is enough. When one or two slow files dominate, use timing data. Many runners also offer a built-in shard option. The next chapter covers it.
Exercise
A suite has files that take 100, 60, 40 and 20 seconds. Using the greedy longest-first method with two shards, what are the shard totals?
Show answer
The 100-second file goes to shard 1. The 60-second file goes to shard 2. The 40-second file goes to shard 2, the least loaded at 60, which brings it to 100. The 20-second file goes to either shard (both are at 100) and brings one to 120. The totals are 120 and 100, so the slowest shard takes 120 seconds.