Use FactoryDefault to speed up models/pipeline_spec
What does this MR do?
Related issue: https://gitlab.com/gitlab-org/plan/-/issues/145
Introduce TestProf::FactoryDefault: https://github.com/test-prof/test-prof/blob/master/docs/recipes/factory_default.md
It allows us to avoid cascading factories problem by creating a default factory:
let(:project) { create_default(:project) }
Next created factory that has project as an association will use the default record instead of creating a new one.
In combination with let_it_be
we'll also avoid creating multiple records for a test suite. However, it's not the default behavior at the moment.
By using the defaults, we can dramatically reduce the factories usage. This merge request optimizes spec/models/pipeline_spec.rb
for demonstrating purposes:
Before
-
sql.active_record
events: 31159 - Finished in 1 minute 55 seconds
[TEST PROF INFO] Factories usage
Total: 1386
Total top-level: 970
Total time: 65.2088s
Total uniq factories: 28
total top-level total time time per call top-level time name
356 356 13.2973s 0.0374s 13.2973s ci_empty_pipeline
208 0 9.5812s 0.0461s 0.0000s namespace
208 76 37.4214s 0.1799s 13.8749s project
178 168 9.0748s 0.0510s 6.9232s ci_build
152 132 17.1896s 0.1131s 13.6547s ci_pipeline
52 15 1.0555s 0.0203s 0.2016s ci_job_artifact
50 50 0.4698s 0.0094s 0.4698s commit_status
40 40 9.1351s 0.2284s 9.1351s merge_request
38 38 0.7938s 0.0209s 0.7938s generic_commit_status
29 26 1.1285s 0.0389s 1.0155s user
19 19 1.7541s 0.0923s 1.7541s ci_sources_pipeline
8 8 0.0400s 0.0050s 0.0400s ci_stage_entity
8 8 1.6688s 0.2086s 1.6688s label
7 7 1.3620s 0.1946s 1.3620s ci_bridge
5 5 0.1726s 0.0345s 0.1726s project_hook
5 5 0.0891s 0.0178s 0.0891s group
4 4 0.1143s 0.0286s 0.1143s milestone
4 4 0.0220s 0.0055s 0.0220s merge_request_diff_commit
4 4 0.0909s 0.0227s 0.0909s merge_request_diff
3 1 0.4980s 0.1660s 0.4077s cluster
1 1 0.0317s 0.0317s 0.0317s ci_runner
1 1 0.0179s 0.0179s 0.0179s external_pull_request
1 1 0.0616s 0.0616s 0.0616s ci_trigger_request
1 0 0.0489s 0.0489s 0.0000s ci_trigger
1 0 0.0346s 0.0346s 0.0000s owner
1 0 0.0563s 0.0563s 0.0000s cluster_provider_gcp
1 0 0.0611s 0.0611s 0.0000s cluster_platform_kubernetes
1 1 0.0100s 0.0100s 0.0100s license
After
-
sql.active_record
events: 18845 - Finished in 1 minute 19.99 seconds
[TEST PROF INFO] Factories usage
Total: 997
Total top-level: 920
Total time: 33.7668s
Total uniq factories: 28
total top-level total time time per call top-level time name
356 356 7.5738s 0.0213s 7.5738s ci_empty_pipeline
178 168 6.6493s 0.0374s 6.0678s ci_build
152 132 3.9507s 0.0260s 3.5854s ci_pipeline
52 15 1.4880s 0.0286s 0.3153s ci_job_artifact
50 50 0.6768s 0.0135s 0.6768s commit_status
40 40 7.3620s 0.1840s 7.3620s merge_request
38 38 0.5648s 0.0149s 0.5648s generic_commit_status
31 30 4.6378s 0.1496s 4.5366s project
19 19 0.5933s 0.0312s 0.5933s ci_sources_pipeline
17 14 0.7012s 0.0412s 0.6000s user
8 8 0.0417s 0.0052s 0.0417s ci_stage_entity
8 8 0.0978s 0.0122s 0.0978s label
8 8 0.3819s 0.0477s 0.3819s namespace
7 7 0.2042s 0.0292s 0.2042s ci_bridge
5 5 0.2559s 0.0512s 0.2559s project_hook
5 5 0.1131s 0.0226s 0.1131s group
4 4 0.1110s 0.0277s 0.1110s milestone
4 4 0.0141s 0.0035s 0.0141s merge_request_diff_commit
4 4 0.0913s 0.0228s 0.0913s merge_request_diff
3 1 0.4378s 0.1459s 0.3580s cluster
1 1 0.1150s 0.1150s 0.1150s ci_runner
1 1 0.0164s 0.0164s 0.0164s external_pull_request
1 1 0.0811s 0.0811s 0.0811s ci_trigger_request
1 0 0.0694s 0.0694s 0.0000s ci_trigger
1 0 0.0503s 0.0503s 0.0000s owner
1 0 0.0516s 0.0516s 0.0000s cluster_provider_gcp
1 0 0.1332s 0.1332s 0.0000s cluster_platform_kubernetes
1 1 0.0096s 0.0096s 0.0096s license
Queries saved: 12314
Edited by Igor Drozdov