Optimize factory usage for cluster presenter

What does this MR do and why?

This MR replaces create with build_stubbed in spec/presenters/clusters/cluster_presenter_spec.rb to optimize the factory usage as recommended in our handbook. This spec is part of the list .rubocop_todo/rspec/factory_bot/avoid_create.yml. It was verified that database persistence is not needed using the factory doctor command FDOC=1 bin/rspec spec/presenters/clusters/cluster_presenter_spec.rb.

Factory Usage Optimization Results

Before optimization:

  • Factory time: 11.497s (39.51% of total time)

After optimization:

  • Factory time: 0.171s (0.97% of total time)

🚀 Improvement:

  • Factory time reduced by: 11.326s
  • Factory time percentage reduced by: 38.54%
  • Overall factory usage improvement: 98.5%

Factory Doctor Output

[TEST PROF INFO] FactoryDoctor enabled (event: "sql.active_record", threshold: 0.01)
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}
==> Using precompiled Gitaly binaries from cache

Test environment set up in 1.0992630000037025 seconds
.....................[TEST PROF INFO] FactoryDoctor report

Total (potentially) bad examples: 15
Total wasted time: 00:11.025

Clusters::ClusterPresenter (./spec/presenters/clusters/cluster_presenter_spec.rb:5) (218 records created, 00:11.025)
  returns existing migration (./spec/presenters/clusters/cluster_presenter_spec.rb:118)  18 records created, 00:01.868
  returns existing migration (./spec/presenters/clusters/cluster_presenter_spec.rb:118)  19 records created, 00:00.820
  returns existing migration (./spec/presenters/clusters/cluster_presenter_spec.rb:118)  16 records created, 00:00.506
  takes a cluster and optional params (./spec/presenters/clusters/cluster_presenter_spec.rb:20)  22 records created, 00:01.145
  exposes cluster (./spec/presenters/clusters/cluster_presenter_spec.rb:24)  22 records created, 00:01.084
  forwards missing methods to cluster (./spec/presenters/clusters/cluster_presenter_spec.rb:28)  22 records created, 00:01.068
  is expected to include "us-east-1" (./spec/presenters/clusters/cluster_presenter_spec.rb:59)  7 records created, 00:00.267
  is expected to include "test-cluster" (./spec/presenters/clusters/cluster_presenter_spec.rb:60)  7 records created, 00:00.474
  is expected to include "us-central1-a" (./spec/presenters/clusters/cluster_presenter_spec.rb:66)  7 records created, 00:00.247
  is expected to include "test-cluster" (./spec/presenters/clusters/cluster_presenter_spec.rb:67)  7 records created, 00:00.249
  is expected to eq "/namespace22/project-22/-/clusters/52" (./spec/presenters/clusters/cluster_presenter_spec.rb:77)  20 records created, 00:01.049
  is expected to eq "/groups/group3/-/clusters/55" (./spec/presenters/clusters/cluster_presenter_spec.rb:84)  21 records created, 00:00.880
  is expected to eq "/admin/clusters/58" (./spec/presenters/clusters/cluster_presenter_spec.rb:90)  18 records created, 00:00.643
  is expected to eq "Elastic Kubernetes Service" (./spec/presenters/clusters/cluster_presenter_spec.rb:41)  6 records created, 00:00.475
  is expected to eq "Google Kubernetes Engine" (./spec/presenters/clusters/cluster_presenter_spec.rb:47)  6 records created, 00:00.242




Finished in 25 seconds (files took 13.73 seconds to load)
21 examples, 0 failures

Randomized with seed 8202

[TEST PROF INFO] Time spent in factories: 00:11.538 (40.36% of total time)

Factory time after update

[TEST PROF INFO] Time spent in factories: 00:00.171 (0.97% of total time)

References

Relates to: #378910.

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.

Merge request reports

Loading