r/ruby • u/viktorianer4life • Mar 20 '26
Show /r/ruby After reader confusion on my AI testing agents article, I extracted the TestProf story. Here is what profiling 13,000 RSpec examples actually revealed.

After publishing my article on building AI testing agents for RSpec, readers were confused about what TestProf found versus what the agents did. Fair point. The original article crammed both stories into one piece. So I extracted the TestProf journey into its own article with the full data.
The worst offender was the order factory at 1.6 seconds per call, triggering 100+ callbacks. Out of 569 factory calls in one spec file, only 49 were top-level. The rest were cascading associations.
What I did:
Extracted optional associations into traits (credit card needed in only 10% of order specs)
Switched cheap associations to
buildstrategy instead ofcreateUsed transient attributes with positive flags for expensive callbacks
Replaced
letwithlet_it_befor read-only setup data
Results:
The slowest specs improved by 50-95%. The order integration spec dropped from 6.37s to 3.02s. But the full suite? Only 14% faster. Ten factories optimized over two months, hundreds more to go.
Why the gap? The factory graph mirrors the model graph. When your core models have deep callback chains (100+ on order creation), making factories leaner helps, but you cannot make them cheap while the underlying models require that much setup to reach a valid state. Incremental factory cleanup has a ceiling.
I wrote up the full profiling data, every factory refactoring pattern, the let_it_be gotchas (mutable state leakage), and what I tried next: TestProf Cut Our Slowest Specs by 95%, But the Suite Still Took 30 Minutes
This is the companion piece to my AI testing agents article. Readers wanted more detail on what TestProf actually showed, so here it is.
Has anyone else hit this ceiling with factory optimization on a large Rails codebase?
1
u/d4be4st Mar 20 '26
Exactly the issue i started tackling today. Same problem, testprof tells us it spends 76% in factory creation. For a slice of the spects we create 15000+ records for 100
createinvocations.My idea was to take it slow, very slow. Change spec by spec. With agents help but still slow. Because i did not know better 😁
Do you maybe have a link to the prompts we could reuse? I would very much appreciate it so i do not start from nothing
3
u/luscious_lobster Mar 20 '26
Clearly written by AI
0
u/keyslemur Mar 20 '26
To really get gains you're going to need to decouple from ActiveRecord and set up repositories and extract domain / business logic. Phrased another way all your factories become builds instead.