Sub topic 7
Stable hash-based assignment of test files
This chapter assumes you can run a test suite from the command line, read a CI matrix in YAML, and write a short Python script. It covers one splitting strategy: assigning each test file to a shard by hashing its path.
The idea
A hash function turns a string into a number. If you take the number modulo the shard count, you get a shard index between 0 and N-1. The same path always gives the same index, so each file has a fixed home.
This differs from count-based splitting, where you sort the files and cut the list into equal slices. With slices, adding one file near the top shifts every file below it into a different shard. With hashing, adding or deleting a file changes nothing for the other files.
A stable hash in Python
import sys
import zlib
def shard_for(path: str, total: int) -> int:
# crc32 gives the same result on every machine and every run,
# unlike Python's built-in hash(), which is salted per process.
return zlib.crc32(path.encode("utf-8")) % total
def files_for_shard(paths, index: int, total: int):
# index is 1-based, matching the --shard=index/total convention
return [p for p in sorted(paths) if shard_for(p, total) == index - 1]
if __name__ == "__main__":
index, total = int(sys.argv[1]), int(sys.argv[2])
for path in files_for_shard(sys.argv[3:], index, total):
print(path)Run shard 2 of 4 over a list of files:
python shard.py 2 4 tests/test_a.py tests/test_b.py tests/test_c.py tests/test_d.pyEach CI job runs the same script with its own index. Every file lands in exactly one shard, and no coordination between jobs is needed.
How assignment flows
Trade-offs
| Property | Hash-based | Timing-based |
|---|---|---|
| Stable when files are added | Yes | No, shards may be reshuffled |
| Needs stored data | No | Yes, recorded durations |
| Balances by duration | No | Yes |
| Balances by file count | Roughly, for many files | Not a goal |
Hashing spreads files evenly by count only on average. With 8 files and 4 shards, one shard can easily get 4 files and another 0. The spread improves as the file count grows.
It also ignores duration. If one file takes 10 minutes and the rest take 10 seconds, the shard holding the slow file decides your wall-clock time, however the others are filled.
Common errors
- Hashing different strings in different jobs. If one job sees
tests/a.pyand another sees./tests/a.py, the hashes differ. Normalize paths, for example relative to the repository root with forward slashes. - Mixing 0-based and 1-based indexes. CI matrices and
--shardflags usually count from 1. The modulo result counts from 0. Theindex - 1in the script handles this. - Changing the shard count. Stability holds only for a fixed N. Going from 4 to 5 shards reassigns most files.
- Discovering files differently per job. Sort the list and use the same glob everywhere.
When to choose it
Pick hash-based assignment when you want a simple, stateless split and your test files are similar in size. Move to timing-based splitting when a few slow files dominate.
Exercise
A teammate switches the script to use Python's hash() and the suite starts skipping some tests in CI. What causes this, and how do you fix it?
Show answer
hash() on strings is salted per process, so each CI job computes a different shard for the same path. Some files map to no job's index and are skipped, while others map to several jobs. Replace it with a deterministic function such as zlib.crc32 on the UTF-8 bytes of a normalized path.
Exercise
You run 6 test files across 3 shards by hash and one shard gets 4 files. Does this mean the hash is broken? What would you change if the problem persists on a larger suite?
Show answer
No. Hashing balances only on average, and 6 files is too few for an even spread. If imbalance persists with many files, the cause is usually uneven file durations, so switch to timing-based splitting.