Skip to content

Speed up group policy spec

Heinrich Lee Yu requested to merge speed-up-group-policy-spec into master

What does this MR do?

Use let_it_be and before_all to reduce DB queries and speed up the spec.

I ran EVENT_PROF=sql.active_record bundle exec rspec spec/policies/group_policy_spec.rb and EVENT_PROF=sql.active_record bundle exec rspec ee/spec/policies/group_policy_spec.rb to see how the tests performed before and after the change.

Before:

[TEST PROF INFO] EventProf results for sql.active_record

Total time: 00:06.663 of 00:18.662 (35.7%)
Total events: 15266

Top 5 slowest suites (by time):

GroupPolicy (./spec/policies/group_policy_spec.rb:3) – 00:06.663 (15266 / 62) of 00:18.662 (35.7%)

Finished in 20.26 seconds (files took 24.73 seconds to load)
62 examples, 0 failures
[TEST PROF INFO] EventProf results for sql.active_record

Total time: 00:06.400 of 00:18.150 (35.26%)
Total events: 15045

Top 5 slowest suites (by time):

GroupPolicy (./ee/spec/policies/group_policy_spec.rb:5) – 00:06.400 (15045 / 70) of 00:18.150 (35.26%)

Finished in 19.76 seconds (files took 20.62 seconds to load)

After:

[TEST PROF INFO] EventProf results for sql.active_record

Total time: 00:01.629 of 00:04.432 (36.76%)
Total events: 2658

Top 5 slowest suites (by time):

GroupPolicy (./spec/policies/group_policy_spec.rb:3) – 00:01.629 (2658 / 62) of 00:04.432 (36.76%)

Finished in 6.06 seconds (files took 19.37 seconds to load)
62 examples, 0 failures
[TEST PROF INFO] EventProf results for sql.active_record

Total time: 00:00.738 of 00:02.161 (34.19%)
Total events: 1104

Top 5 slowest suites (by time):

GroupPolicy (./ee/spec/policies/group_policy_spec.rb:5) – 00:00.738 (1104 / 70) of 00:02.161 (34.19%)

Finished in 4.41 seconds (files took 23.93 seconds to load)
70 examples, 0 failures

We can see the reduction of test runtime from ~40 seconds to just ~10 seconds. We can also see the massive reduction of ActiveRecord events from ~30k to just ~4k.

This is just a small example and I think we can do even more with larger spec files.

Screenshots

Does this MR meet the acceptance criteria?

Conformity

Performance and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team

#32945 (closed)

Edited by Thong Kuah

Merge request reports