Skip to content

Draft: Use stubbed factories to speed up visibility_level_helper specs

Peter Leitzen requested to merge pl-spec-visibility-level-helper-perf into master

What does this MR do and why?

This MR refactors visibility_level_helper to use build_stubbed instead of create for all used factories.

Why?

According to https://gitlab-org.gitlab.io/rspec_profiling_stats/ this spec file was ranked 1️⃣ with most amount of SQL queries across all helper specs. For these types of specs we can almost always use stubs.

RSpec Profiling

Here's the output from test-prof when run with FPROF=1. Note:

  • Total amount of factories created went down from 247 to 1 🚀
  • Total events went down from 8147 to 505
  • Queries saved: 7642

Before

Total (potentially) bad examples: 52
Total wasted time: 00:04.010

VisibilityLevelHelper (./spec/helpers/visibility_level_helper_spec.rb:5) (54 records created, 00:04.010)
  excludes disallowed visibility levels (./spec/helpers/visibility_level_helper_spec.rb:220) – 1 record created, 00:00.345
  is expected to eq [20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.066
  is expected to eq [10, 20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.064
  is expected to eq [0, 20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.060
  is expected to eq [10] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.117
  is expected to eq [0] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.059
  is expected to eq [20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.059
  is expected to eq [0, 10, 20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.056
  is expected to eq [0, 10] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.054
  is expected to eq [0] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.066
  is expected to eq [10] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.058
  excludes disallowed visibility levels (./spec/helpers/visibility_level_helper_spec.rb:220) – 1 record created, 00:00.058
  is expected to eq [0, 10, 20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.055
  is expected to eq [0, 20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.115
  is expected to eq [10] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.062
  is expected to eq [20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.058
  is expected to eq [0, 10] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.058
  is expected to eq [10, 20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.056
  is expected to eq [10] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.071
  is expected to eq [0] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.058
  is expected to eq [0] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.056
  is expected to eq [20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.057
  excludes disallowed visibility levels (./spec/helpers/visibility_level_helper_spec.rb:220) – 1 record created, 00:00.060
  is expected to eq [0, 10] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.059
  is expected to eq [0] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.061
  is expected to eq [20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.052
  is expected to eq [20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.054
  is expected to eq [0] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.053
  is expected to eq [10] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.060
  is expected to eq [10, 20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.100
  is expected to eq [0, 20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.059
  is expected to eq [0, 10, 20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.055
  is expected to eq [10] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.057
  excludes disallowed visibility levels (./spec/helpers/visibility_level_helper_spec.rb:220) – 1 record created, 00:00.073
  is expected to eq [10, 20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.064
  is expected to eq [20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.066
  is expected to eq [0, 10, 20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.063
  is expected to eq [0] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.063
  is expected to eq [10] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.067
  is expected to eq [0, 10] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.054
  is expected to eq [10] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.061
  is expected to eq [20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.073
  is expected to eq [0, 20] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.090
  is expected to eq [0] (./spec/helpers/visibility_level_helper_spec.rb:217) – 1 record created, 00:00.084
  is expected to eq false (./spec/helpers/visibility_level_helper_spec.rb:304) – 1 record created, 00:00.082
  is expected to eq true (./spec/helpers/visibility_level_helper_spec.rb:304) – 1 record created, 00:00.073
  is expected to eq false (./spec/helpers/visibility_level_helper_spec.rb:304) – 1 record created, 00:00.074
  is expected to eq false (./spec/helpers/visibility_level_helper_spec.rb:281) – 1 record created, 00:00.075
  is expected to eq true (./spec/helpers/visibility_level_helper_spec.rb:281) – 1 record created, 00:00.064
  is expected to eq true (./spec/helpers/visibility_level_helper_spec.rb:281) – 1 record created, 00:00.065
  disallows levels (./spec/helpers/visibility_level_helper_spec.rb:130) – 2 records created, 00:00.232
  disallows levels (./spec/helpers/visibility_level_helper_spec.rb:99) – 2 records created, 00:00.185


[TEST PROF INFO] EventProf results for sql.active_record

Total time: 00:10.847 of 00:27.379 (39.62%)
Total events: 8147

Top 5 slowest suites (by time):

VisibilityLevelHelper (./spec/helpers/visibility_level_helper_spec.rb:5) – 00:10.847 (8147 / 86) of 00:27.379 (39.62%)



Finished in 35.39 seconds (files took 9.37 seconds to load)
86 examples, 0 failures

Randomized with seed 58492

[TEST PROF INFO] Factories usage

 Total: 247
 Total top-level: 160
 Total time: 00:16.241 (out of 00:42.736)
 Total uniq factories: 8

   total   top-level     total time      time per call      top-level time               name

      91          71        6.5089s            0.0715s             5.2174s               user
      44          44        2.2512s            0.0512s             2.2512s              group
      44           0        0.1797s            0.0041s             0.0000s namespace_settings
      43          43        8.5262s            0.1983s             8.5262s            project
      22           0        1.6409s            0.0746s             0.0000s          namespace
       1           1        0.0139s            0.0139s             0.0139s            license
       1           1        0.2327s            0.2327s             0.2327s            snippet
       1           0        0.0602s            0.0602s             0.0000s             author

After

[TEST PROF INFO] FactoryDoctor says: "Looks good to me!"
[TEST PROF INFO] EventProf results for sql.active_record

Total time: 00:00.476 of 00:06.155 (7.73%)
Total events: 505

Top 5 slowest suites (by time):

VisibilityLevelHelper (./spec/helpers/visibility_level_helper_spec.rb:5) – 00:00.476 (505 / 86) of 00:06.155 (7.73%)



Finished in 13.86 seconds (files took 9.5 seconds to load)
86 examples, 0 failures

Randomized with seed 45600

[TEST PROF INFO] Factories usage

 Total: 1
 Total top-level: 1
 Total time: 00:00.014 (out of 00:21.081)
 Total uniq factories: 1

   total   top-level     total time      time per call      top-level time               name

       1           1        0.0146s            0.0146s             0.0146s            license

How to set up and validate locally

bin/rspec spec/helpers/visibility_level_helper_spec.rb 

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Peter Leitzen

Merge request reports

Loading