lukad

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.Formatter is an ExUnit formatter that records how long each test file takes

  • After a CI run, mix ballast.merge combines the per-shard timings into a merged timings.json snapshot, which you keep in your CI cache

  • On the next run, mix ballast.test --shard 3/8 uses 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 to max_cases

  • mix ballast.merge --check checks that all shards used the same plan and that every file ran exactly once.

  • mix ballast.plan --shards 4 shows 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.

Showing Posts 1 to 2

rhcarvalho

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 --json flag 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

lukad OP

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.json

{
  "version": 2,
  "shard": 1,
  "total": 2,
  "plan_digest": "c2c542427842",
  "clean": true,
  "max_cases": 36,
  "files": {
    "test/integration_test.exs": {"sync_us": 13344856, "async_us": 0, "longest_async_us": 0}
  }
}

tmp/ballast/timings.json

{
  "version": 2,
  "max_cases": 36,
  "files": {
    "test/ballast/args_test.exs": {"sync_us": 0, "async_us": 2081, "longest_async_us": 2081},
    "test/ballast/files_test.exs": {"sync_us": 32899, "async_us": 0, "longest_async_us": 0},
    "test/ballast/formatter_test.exs": {"sync_us": 0, "async_us": 278638, "longest_async_us": 278638},
    "test/ballast/planner_test.exs": {"sync_us": 0, "async_us": 160533, "longest_async_us": 160533},
    "test/ballast/report_test.exs": {"sync_us": 0, "async_us": 26348, "longest_async_us": 26348},
    "test/ballast/timings_test.exs": {"sync_us": 0, "async_us": 37132, "longest_async_us": 37132},
    "test/integration_test.exs": {"sync_us": 13344856, "async_us": 0, "longest_async_us": 0},
    "test/mix/tasks/ballast.install_test.exs": {"sync_us": 0, "async_us": 200258, "longest_async_us": 200258}
  }
}

ballast.merge and ballast.plan already support machine readable output via a --json flag:

❯ mix ballast.merge --json | jq .
{
  "ok": true,
  "output": "tmp/ballast/timings.json",
  "file_count": 8,
  "reports": [
    "tmp/ballast/shard-1-of-2.json",
    "tmp/ballast/shard-2-of-2.json"
  ]
}
❯ mix ballast.plan --shards 2 --json | jq .
{
  "total": 2,
  "shards": [
    {
      "files": [
        "test/integration_test.exs"
      ],
      "predicted_us": 13344856,
      "round_robin_us": 13623494,
      "shard": 1
    },
    {
      "files": [
        "test/ballast/args_test.exs",
        "test/ballast/files_test.exs",
        "test/ballast/formatter_test.exs",
        "test/ballast/planner_test.exs",
        "test/ballast/report_test.exs",
        "test/ballast/timings_test.exs",
        "test/mix/tasks/ballast.install_test.exs"
      ],
      "predicted_us": 311537,
      "round_robin_us": 233157,
      "shard": 2
    }
  ],
  "max_cases": 36,
  "file_count": 8,
  "plan_digest": "c2c542427842",
  "round_robin_slowest_us": 13623494,
  "slowest_us": 13344856,
  "without_history": []
}

Looking forward to read your blog post on the integration tests whenever you get around to writing it.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

achempion
Hi, I would like to tell about my initiative to further maintain and develop Waffle project which is the fork of Arc library. The progre...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
gtcode
Aloha gang, There’s a new class of AI model out from TypeSafe. Jev is their flagship model. You can learn about it here: ...
New
danschultzer
I’ve moved the strategies out of PowAssent and created a multi-provider framework for Elixir. This way the strategies can be used in any ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
AstonJ
With advancements in AI gathering pace and set to increasingly accelerate, perhaps it’s worth having an annual thread on the topic - feel...
New
bartblast
If you haven’t tried Hologram yet, what’s stopped you? Here’s why I’m asking. Building a framework turns out to be two jobs, the framewo...
New
mat-hek
Popcorn, the library that runs Elixir code in the browser, is now stable :popcorn: This is possible because it now builds on a very stab...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews