Speed up a slow test file
These techniques sped up slow RSpec files in gitlab-org/gitlab.
Read them before you work on a slow-file issue,
or give them to the AI agent that does the work.
Most have a merged example with its measured result.
The details of each technique are in Testing best practices.
A backend test file is slow when it averages more than 5 minutes on the default branch. The test file runtime dashboard (internal) lists the slowest files in CI, and you can filter it by file path.
Measure first
Profile the file before you change it, and again after:
FPROF=1 RD_PROF=1 bin/rspec <your_slow_spec>
FDOC=1 bin/rspec <your_slow_spec>
bin/rspec --profile 20 <your_slow_spec>Use the output to pick a technique:
| The profile shows | Try |
|---|---|
FPROF=1: a factory created hundreds of times | Create shared records once or Skip records the test never reads |
RD_PROF=1: most of the time in before and let | Create shared records once |
FDOC=1: examples that create records but run no database queries | Use build or build_stubbed instead of create |
--profile: many slow examples with the same setup | Check more in each example |
| One slow call in every example | Stub slow calls the test does not check |
When the profile shows more than one of these, start with the one that takes the most time.
Local timings vary between runs and seeds. Compare CI job times when you can and put the before and after numbers in the merge request.
Keep the change safe
Before you push, check the change against Keep a speed-up safe. It covers keeping every assertion at full strength, not skipping or deleting examples, not inventing RSpec metadata tags, and running the file in random order.
Techniques
Create shared records once
Use this when the same records are built for every example and no example changes them.
Move them to let_it_be or before_all.
This includes members: add them once in before_all, not in each example.
See Assigning members through the factory.
When examples change a record, use let_it_be_with_reload, which reloads it in each example.
Use let_it_be_with_refind when the record caches values in instance variables, for example with strong_memoize.
It loads a new instance for each example, so those values do not carry over.
- Example: merge request 268151 took a 249-example spec from 8 to 10 minutes to under 1 minute in CI.
- Example: merge request 267910
drew routes once per example group in
before_allinstead of reloading them twice per example, and restored them inafter(:all). The file got about 90% faster in CI. - Watch out:
let_it_bedoes not work in examples tagged:delete, which use the delete cleaner strategy.let_it_bealso must not depend on aletorbeforeblock. See When not to convertlettolet_it_beandlet_it_bemust not depend on a before block. - Watch out:
before_allrolls back only the database. If examples write to a Git repository, give them their own project, or reset the branches they change before each example.
Check more in each example
Use this when many examples build the same setup only to check one more thing.
Combine them into one example with :aggregate_failures, or check every row of a
table in one example. Keep every assertion.
:aggregate_failures still reports every failed assertion, not only the first.
- Example: merge request 268144
cut the search visibility specs from 124 to 61 minutes of
rspec-ee unittime per pipeline. - Example: merge request 258551
combined
:jsfeature examples that loaded the same page in the same state, from 11.3 to 4.7 minutes in CI. - Watch out: keep examples that select something or change state separate.
Stub slow calls the test does not check
Use this when the profile shows time in code the test does not assert on, such as a network call or mail rendering.
- Example: merge request 271431 stubbed a registry API check that spent about 5 seconds per example retrying a refused connection.
- See Mock expensive external operations.
Skip records the test never reads
Use this when factories create repositories, projects, or associated records that no example uses.
- Example: merge request 268343 stopped the Geo registry shared examples from creating replicated records, from 4,177 to 976 seconds per full run.
- Add
:repositoryonly when the test reads Git data. Each one adds a few tenths of a second. For traits with smaller repositories, see Repositories. - Add
:skip_diff_creationtocreate(:merge_request)when the test does not read diffs.
Remove examples that run elsewhere
Use this when a shared example or a feature flag runs the same checks twice. Before you remove an example, find the spec that makes the same assertions.
- Example: merge request 266232
removed a feature flag and its flag-off examples,
which cut
user_sees_pipelines_spec.rbby about 40% in CI.
Move browser checks to frontend tests
Use this when a :js feature example only checks what the page shows, and the page gets
its data from an API. Move those checks to a
frontend integration test with mocked API
responses, and keep the feature spec for the full user flow.
- Example: merge request 274602 moved most of the job page feature spec to frontend integration tests. The feature spec went from 6 minutes 51 seconds to 1 minute 16 seconds.
- Watch out: this cuts RSpec time but adds Jest time. Count both.
Split the file, if it is still slow
If the file still averages more than 5 minutes after the techniques above,
follow Fix a slow file.
Splitting shortens the longest rspec job. It does not reduce total CI time.
A file that combined these
ee/spec/models/ee/project_spec.rb went from a 10.6-minute median to 4.5 minutes on
the default branch over 12 merge requests, without a split.
Most of them created shared records once.
One cut 3,146 examples to 297 by checking each feature once,
and one dropped repositories the examples never read.
Two made shared test setup cheaper: the Geo node setup and :small_repo.
GitLab team members can read the summary in this confidential issue:
https://gitlab.com/gitlab-org/quality/test-failure-issues/-/work_items/46050#note_4338039431