Ballast

December 2, 20267 min read
On This PageBallast5 sections

Not too long ago I worked on a very large Elixir monolith. The whole test suite used to run in a single CI job, and a run took a long time, around 30 minutes if I remember correctly. To speed things up I introduced mix test --partitions and split the suite across several jobs. Only after I had moved on did I realize that splitting the tests by how long they actually take would have come in handy there. So in October I built Ballast, mostly to see if the idea actually works, and to share it so others can use it.

The problem is how mix test --partitions works. It sorts the test files and deals them out round-robin, one file per partition, without knowing anything about how long they take. If a couple of slow files end up in the same partition, that’s just bad luck. And the whole run is only as fast as its slowest job, so the extra machines don’t buy you as much as you’d hope.

Ballast records how long each test file takes and then uses those timings to split the files so the shards finish at about the same time. Here is what that looks like on a synthetic test suite with 199 files and four shards:

$ mix ballast.plan --shards 4
199 files, 0 without history, max_cases 36, plan 0556c9144666
 
shard  files  ballast  round-robin
    1     12    57.9s        58.6s
    2    105    57.7s        47.7s
    3     30    57.9s        42.2s
    4     52    57.9s        92.6s
 
slowest shard: 57.9s (round-robin: 92.6s)

With round-robin the slowest shard takes 92.6 seconds while another one is done after 42. With Ballast all four land at around 58 seconds. The file counts look strange at first (12 files in one shard, 105 in another), but that’s expected since Ballast balances time, not file count.

How it works

Building this turned out to be surprisingly straightforward. Ballast comes with an ExUnit formatter that you add next to the default one:

ExUnit.start(formatters: [ExUnit.CLIFormatter, Ballast.Formatter])

Each CI job runs its shard with mix ballast.test --shard 3/8 instead of mix test, and the formatter writes the timings for that shard to a JSON file. Once all shards are done, mix ballast.merge combines those files into a snapshot that the next run plans from. Under a plain mix test the formatter does nothing, so you can leave it in.

I made merge refuse shards that had failing tests. A test that crashes right away looks fast and a test that runs into a timeout looks slow, and neither number says much about how long the test takes when it passes. Partial runs like --failed or --only are never recorded either.

Why not split by individual tests?

Smaller pieces would be easier to balance, so why move whole test files around instead of single tests? Mostly because ExUnit doesn’t really work at the level of single tests. mix test takes file paths, and loading a file defines every test module in it. You can narrow a run down to certain lines or tags, but each shard would still load and compile everything. Inside a module, ExUnit runs the tests one after another anyway, so the parallelism comes from running modules side by side, not tests. And modules often have expensive setup in setup_all, like starting a server or seeding data. If the tests of one module were spread over four shards, all four would pay for that setup.

Since a module can’t be loaded without the rest of its file, the file ends up being the smallest unit that’s practical to move. The formatter still records some details per module (like the longest async module in a file), because that’s what the cost estimate below needs.

The downside is that a single huge test file can’t be split. No shard can finish faster than the slowest file in it, so if one file takes three minutes, three minutes is your floor. In that case you’ll have to split the file yourself, same as without Ballast.

Estimating how long a shard takes

Simply adding up the durations of the files in each shard and balancing those sums doesn’t work, because that’s not how ExUnit runs tests.

Sync test modules run one after another, so their times add up. Async modules run concurrently, up to max_cases at a time (twice the number of schedulers by default). Ten one-second sync files take ten seconds, but ten one-second async files might take one second. So Ballast estimates the cost of a shard roughly like this:

cost = sync + max(longest async module, async / max_cases)

The async work is spread across max_cases, but a shard can never finish before its longest single async module does. Later I also added support for ExUnit’s :group option, where async modules in the same group run one at a time, which makes the formula a bit more involved but works the same way.

For the assignment itself I went with the textbook greedy approach. Sort the files from heaviest to lightest, then put each one into the shard whose cost is lowest after adding it. That’s not guaranteed to find the best possible split (that problem is NP-hard), but it gets close and it’s fast.

New test files that don’t have a timing yet get the median weight of the known files. And without any snapshot at all every file weighs the same, which gives you exactly the same split as --partitions. So adding Ballast to a project never makes things worse on the first run.

Making sure the shards agree

There is no central coordinator. Every CI job calculates the entire plan by itself and then runs only its own part. That’s simple, but it only works if every job sees the same test files and the same snapshot. If one shard plans from yesterday’s timings and another from today’s, some files run twice and some don’t run at all, and you would never notice.

To catch that, the planner is deterministic (ties are broken by file path), and every shard prints a short hash of the plan. That’s the plan 0556c9144666 in the output above. mix ballast.merge --check then makes sure all shards used the same plan and that every file ran exactly once. I’d recommend running it on every CI run.

The easiest way to get this wrong is in how the snapshot is cached. If each shard looks up the newest cache entry on its own, another CI run can save a newer snapshot in between and two shards of the same run end up with different data. The GitHub Actions example in the README avoids this by looking up the cache key once in a separate job and passing that exact key to every shard.

A few other things that work: --failed reruns only the failures of one shard without moving files between shards, --cover writes per-shard coverage data you can combine with mix test.coverage, and other mix test flags like --exclude are passed through. Umbrella projects and splitting a single test file across shards aren’t supported yet.

Try it out

Ballast is on Hex, and if you use Igniter the setup is a single command:

$ mix igniter.install ballast

The README has the manual setup and a full GitHub CI config.

To be honest, I haven’t used it on a real large code base yet. My own projects are small and their tests run fast anyway, so there isn’t much time to save. I’m really curious how it does on something like the monolith I mentioned at the start, with a slow and uneven test suite. That’s where it should help the most. If you have one of those, run mix ballast.plan once you have a snapshot and let me know how the numbers compare to round-robin. If something breaks, the plan looks off, or you need umbrella support or another CI provider, please open an issue or reach out on Bluesky.