lukad
Hi folks! I’ve released Ballast (hex, docs), a small library that splits test files across shards based on how long they take.
The problem
mix test --partitions N partitions tests files round-robin without looking at how long they take. If a few slow files end up on the same partition, they can take longer than other partitions, causing the whole CI run to take longer.
What Ballast does
-
Ballast.Formatteris an ExUnit formatter that records how long each test file takes -
After a CI run,
mix ballast.mergecombines the per-shard timings into a mergedtimings.jsonsnapshot, which you keep in your CI cache -
On the next run,
mix ballast.test --shard 3/8uses that snapshot to split the files so the shards finish at about the same time. It accounts for sync and async modules separately, since async modules run concurrently up tomax_cases -
mix ballast.merge --checkchecks that all shards used the same plan and that every file ran exactly once. -
mix ballast.plan --shards 4shows the estimated split and total timings compared to what round-robin would have done, without running anything:shard files ballast round-robin 1 41 31.4s 38.9s 2 22 31.4s 32.1s 3 3 31.5s 15.6s 4 7 31.5s 35.4s slowest shard: 31.5s (round-robin: 38.9s)
mix ballast.test passes through all mix test flags as usual. --failed reruns a shard’s failures without moving files to other shards, and --cover writes coverdata for each shard so you can combine it later. The README includes a complete GitHub Actions workflow.
Install with Igniter (won’t set up CI configuration):
mix igniter.install ballast
Managing expectations
Don’t expect huge time savings from Ballast. I did this mostly as an experiment to see how much it could help in real-world scenarios. I have not gotten huge savings with it but I’m curious to see if other projects benefit from it. Whether you see a benefit depends on your test suite.
- If your files take roughly the same time, round-robin will already balance them well and you’ll see little or no difference
- Sharding is done on a per-file basis, so if one file takes 40s, no shard can finish in less than 40s
- It needs some CI setup to save timing snapshots
In some cases the savings may be negligible, like in this example.
❯ mix ballast.plan --shards 4
73 files, 0 without history, max_cases 36, plan b11f0588d8d5
shard files ballast round-robin
1 9 31.8s 40.6s
2 34 32.2s 32.2s
3 14 31.8s 15.6s
4 16 32.1s 35.8s
slowest shard: 32.2s (round-robin: 40.6s)
In codebases where a small amount of slow files dominate the test suite, Ballast can provide some benefit.
❯ mix ballast.plan --shards 4
145 files, 0 without history, max_cases 36, plan 6c6c6eb2238b
shard files ballast round-robin
1 61 48.5s 44.8s
2 31 48.7s 40.5s
3 28 48.5s 53.6s
4 25 48.9s 44.1s
slowest shard: 48.9s (round-robin: 53.6s)
Run mix ballast.plan on your own timings before you decide whether the extra CI setup is worth it.
I’d love to hear feedback, bug reports and stories about how it works on your suites, especially heavier ones.
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #security
- #elixirconf-eu
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
rhcarvalho
Hi! Nice idea!
I’ve recently done something similar for the Phoenix integration tests, though the partitioning scheme is manual and static.
I’m yet to write a blog post on it, but I can link slides of a presentation I gave at the last Elixir Vienna Meetup:
There are more tricks there that helped us go from 8-10min down to ~3-4min while improving observability.
Part of that work also involved a custom formatter to extract test timings without losing parallelism. In the Phoenix CI case I use the timings to build a report, including a mermaid chart and a table where one can easily spot the slowest tests.
If I ever get time, adding a well thought out
--jsonflag to ExUnit might be a worthy proposal to pursue.For anyone curious, an example run and report:
Apparently one needs to be logged in to GitHub to see the summary details.
lukad
Oh that’s interesting! An improvement by ~6 minutes is impressive and I didn’t know you could create custom markdown summaries github, that’s a cool trick.
FWIW here this is how ballast’s shard and merged timing snapshots look like.
tmp/ballast/shard-1-of-2.jsontmp/ballast/timings.jsonballast.mergeandballast.planalready support machine readable output via a--jsonflag:Looking forward to read your blog post on the integration tests whenever you get around to writing it.