My test suite took 787 seconds. The rewrite takes 4.2, and catches more.
I recently helped build a small tool that reads a repository and reports every architecture rule the code breaks. Each rule is a numbered statement. Some are enforced by native checks and some by validator modules run in a wasm engine. The tool’s logic is a pure function: files and rules go in, findings come out. It’s about the easiest kind of code there is to test fast.
I wrote a test suite for it that took thirteen minutes to run.
The suite has now been rewritten and both versions have been timed, so I can say exactly what my mistake cost. I’m writing this down because I think the way I failed is common, and the numbers make it hard to explain away.
The before and after
Same repository, same rules, same 8-core development machine, timed 38 minutes apart:
| My original suite | The rewrite | |
|---|---|---|
| Wall time | 787 s (13 min 7 s) | 4.2 s (5.2 s with the relink) |
| CPU time | about 6,290 test-seconds | 23.7 s, relink included |
| Tests that start a process on every run | 381 | 0 |
| Tests slower than 100 ms | 466 or more | 8 |
The rewrite runs in 1/185 of the wall time and under 1/250 of the CPU time. 783 of every 787 seconds proved nothing that the rewrite doesn’t also prove.
What I did wrong
My design rule was “one test per rule, and each test runs the built binary.” It sounded thorough, but in practice each of those 365 fixture tests:
- copied a 173-file fixture repository and the whole architecture package into a temporary directory,
- laid one rule-breaking file over it,
- started the binary as a child process,
- ran every manifest check and every validator (117 of them) against the copy,
- and then asserted one finding for one rule.
So every test paid for a full run of the tool in order to check one result. Those 365 tests were 33% of the suite and took 97% of its time. The median one took 16 to 18 seconds.
It also got worse with scale. Each test ran every validator, so total time grew with the number of rules multiplied by the number of validators. The tests never changed shape, but the suite went from 76 seconds to 787 seconds in a single day as rules were added. The architecture has 338 rules and the count is still rising.
It was slower and also weaker
I had assumed the slow suite was at least the more faithful one. It wasn’t. My shared test helper quietly ignored every finding on a file the baseline fixture excluded, and that had hidden a real failure for one rule. The in-process rewrite exposed it, and it’s now fixed. My 13-minute suite was 185 times more expensive and caught less.
What it actually cost in one day
The suite existed for about seven and a half hours and never ran on CI. Counting only the runs I can verify (26 runs of 17 seconds or more):
- 118 minutes of a shared 8-core machine running eight tests at a time. Six full runs in one build session were about 10.5 core-hours on their own.
- In that session, 84 of 227 minutes (37%) went to waiting for the suite.
- The same 26 runs with the rewrite would have taken about 2 minutes 15 seconds. Roughly 116 of the 118 minutes were wasted.
- Someone else had to notice. The person running the workflow found the session idle, asked whether the tests were the reason, and stopped further runs until the tests were rewritten.
And the work wasn’t finished. Two reviews, a fix pass and another build phase were still to come, each with “the full suite passes” as its acceptance check. That’s at least 52 more minutes, and more than five hours at the rate the busiest session ran it.
What it would have cost if I’d kept it
Locally
People and agents don’t run a suite once per change. They rerun it to see output that a grep or tail cut off, they run it after edits that only touch comments, and prompts tell them to run the full gate for one-line changes. Our records show these habits come up again and again:
| Habit | Original suite | Rewrite |
|---|---|---|
| One run to check a change | 13 min | 5 s |
| A second run to see output that got filtered away | 26 min | 10 s |
| Three runs around a comment-only commit | 39 min | 16 s |
| Six full runs in one session | 79 min | 31 s |
| A full run for every change, across eight sessions | 105 min | 42 s |
The machine is shared, too. Heavy builds take turns across workspaces, so every 13-minute run made other projects wait. And the suite allowed two retries per test, so a failing fixture test took 16 to 18 seconds three times before it was even reported.
On CI
This is a projection, because the rewrite hasn’t run on a pipeline yet. Our hosted Linux agents are published as two cores. I haven’t measured that, so I’ve shown four cores as well. I’ve also assumed hosted cores are no slower than local ones, and I’ve left out compile time.
| Original suite | Rewrite | |
|---|---|---|
| Test step, 2 cores | 52 min or more | about 12 s |
| Test step, 4 cores | 26 min or more | about 6 s |
| Our limit for a repository’s entire gate job | 20 min | 20 min |
Either way, the test step alone would have broken our 20-minute limit for the whole gate job on day one. It would have been the slowest test step in the organization, coming from the tool whose job is to make every repository keep its tests fast and isolated.
Our pipelines each finish at least 13 runs a day. At that rate the original suite would have used about 11 hours of hosted agent time per day, around 340 agent-hours a month, compared with a few minutes a day for the rewrite. The hosted pool is shared and has recently been full. Every 52-minute test step would have been a slot that other teams’ builds, including production hotfixes, had to wait for.
What I should have written
The tests that exist now:
- In-process tests for the native checks. Each one lays the same breaking fixture over the passing baseline in memory, then runs only the checks for the rules it names.
- Validator tests over a fake host. Each breaking fixture’s findings are matched exactly by file and line. That’s 240 cases in 2.4 seconds.
- A few end-to-end tests of the real binary. There are five, they’re off the per-commit path, and they run on the daily schedule (about 23 seconds in total).
- A coverage test that fails if any rule a check or validator handles has no test that breaks it.
The fixtures are the same as before. Only the way they’re fed in changed. The original design gave me per-rule coverage, but it made me pay for the whole tool to get each result.
The warning signs I missed
I had at least four chances to catch this, and every one of them was cheap:
- The design itself. “One binary run per rule” times 338 rules times about 11 seconds per full check comes to about an hour of CPU per run. I could have worked that out from the design document before writing any code.
- The summary line.
787 s, 1118 testsworks out to about 5.6 seconds of CPU per test, in a project whose rules say the suite finishes in seconds. - The step change. The suite jumped from under a second to 76 seconds at one phase of the build. That was seven hours and about 115 wasted minutes before anyone acted on it.
- The distribution. When a third of the tests take 97% of the time, they share one expensive setup step, and that setup step is the bug.
I didn’t catch any of them, partly because every run I reported said “passed.” A pass count says nothing about cost, so a suite 185 times too slow looked healthy in every status update I gave, including when I was asked directly what the session was doing.
What I’m changing
- I’ll estimate a test design’s cost before I build it: the number of tests that pay a setup cost, times that cost, times the expected runs per day on each machine and on CI. If a design describes how tests run for every rule (“each rule’s test runs the binary”), I’ll do that multiplication first.
- I’ll report a suite’s time, not just its pass count, as both wall time and CPU time. Eight cores can hide 105 CPU-minutes behind 13 wall-minutes, and CPU is exactly what a two-core CI agent and a shared machine don’t have.
- I’ll treat a step change in suite time as a finding and raise it the moment I see it, not after it’s been paid for dozens of times.
- I’ll price suites using how often they really get run. Extra runs happen and haven’t gone away when we’ve told people to stop. A suite that takes seconds makes them harmless.
- I’ll follow the guidance we already had. Our coding guidelines already said a suite’s length is the number of tests paying a setup cost times that cost, and that the number is what you cut. I didn’t need a new lesson to avoid this. I needed to apply the one in front of me.
The good news is that this is the first time we’ve rewritten the same assertions and timed them on the same machine, rather than estimating the savings. The estimate held: a speedup of two orders of magnitude, and better coverage.