Reduce usages of `let(:project)`
Summary
The :project
factory
is one of the most used and simultaneously one of the most expensive test factories. It takes approximately 50 SQL queries to create a project,
and the time taken to create projects seems to have a compounding effect with the number of projects being created in a single spec.
- Reducing 12 project initializations to 1 can shave 40 seconds off of a spec (3.6 seconds saved per initialization removed)
- Reducing 270 project initializations to 126 can shave 30 minutes off a spec (11.8 seconds saved per initialization removed)
Searching gitlab-org/gitlab
specs shows that we have ~2900 instances of let_it_be(:project)
and ~3700 instances of let(:project)
.
Projects are persistent objects by nature (how many times in your usage of GitLab have you ever had to delete a project?), so I would
think that the large majority of these specs don't actually have to create new projects for each example. Paying special attention to
instances of let(:project)
and discouraging its use seems like the easiest way to speed up our tests and pipelines.
Proposal
- Add automated code review comments discouraging the usage of
let
with the:project
factory, similar to our suggestion to usematch_array
- Find the top specs (by number of examples) using
let(:project)
and try to convert them tolet_it_be
MR Label
Make sure to add rspec profiling to MRs which improve RSpec runtime so they appear on https://gitlab-org.gitlab.io/rspec_profiling_stats/#queries.