Optimize factory usage in batched_job
What does this MR do and why?
This MR replaces create with build_stubbed in spec/lib/gitlab/database/background_migration/batched_job_spec.rb to optimize factory
usage as recommended in our handbook.
It was verified that database persistence is not needed using the factory doctor
command FDOC=1 bin/rspec spec/lib/gitlab/database/background_migration/batched_job_spec.rb.
The #can_split? and #can_reduce_sub_batch_size? examples only read attempts, batch_size and sub_batch_size, so the jobs they use don't need to be saved.
Factory Usage Optimization Results
Before optimization:
- Factory time: 1.511s (11.31% of total time)
After optimization:
- Factory time: 1.384s (11.61% of total time)
Improvement:
- Factory time reduced by: 0.127s
- Factory time percentage reduced by: -0.30% (the total run time also dropped, so the share of factory time rose slightly)
- Overall factory usage improvement: 8.41%
Estimated CI Cost Savings
Using the same methodology as !248403 (merged):
0.127s saved per pipeline × $0.03/minute (large runners: 3x cost factor on $10/1,000 base minutes).
| Scenario | Pipelines/month | Time saved/month | Savings/year |
|---|---|---|---|
| Pessimistic | 15,000 | 0.5h | $11.43 |
| Moderate | 30,000 | 1.1h | $22.86 |
| Optimistic | 50,000 | 1.8h | $38.10 |
Factory Doctor Output
[TEST PROF INFO] FactoryDoctor report
Total (potentially) bad examples: 1
Total wasted time: 00:00.016
Gitlab::Database::BackgroundMigration::BatchedJob (./spec/lib/gitlab/database/background_migration/batched_job_spec.rb:5) (2 records created, 00:00.016)
is expected to be truthy (./spec/lib/gitlab/database/background_migration/batched_job_spec.rb:370) – 2 records created, 00:00.016
[TEST PROF INFO] Time spent in factories: 00:01.883 (7.2% of total time)MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.