Speed up group policy spec
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
-
Changelog entry -
Documentation created/updated or follow-up review issue created -
Code review guidelines -
Merge request performance guidelines -
Style guides -
Database guides -
Separation of EE specific content
Performance and Testing
-
Review and add/update tests for this feature/bug. Consider all test levels. See the Test Planning Process. -
Tested in all supported browsers
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
Edited by Thong Kuah