Last active
January 29, 2018 21:59
-
-
Save timdiggins/a634a38da676528af74564c9e0e30705 to your computer and use it in GitHub Desktop.
speed up rails 5.0 feature specs by caching templates during suite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/support/disable_template_cache_clearing | |
# may also need to configure, either here | |
Rails.application.config.action_view.cache_template_loading = true | |
# or in config/environments/test.rb: | |
# config.action_view.cache_template_loading = true | |
# ensure ActionView::Digestor is already autoloaded before patching it | |
ActionView::Digestor | |
# stop the PerExecutionDigestCacheExpiry actually clearing the cache | |
module ActionView | |
class Digestor | |
module PerExecutionDigestCacheExpiry | |
def self.before(target) | |
# not clearing cache now | |
end | |
end | |
end | |
end | |
# ensure view context is cleared once before each test run | |
RSpec.configure do |config| | |
config.before(:suite) { | |
ActionView::LookupContext::DetailsKey.clear | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Background:
After the upgrade of a rails app from 4.2 to 5.0.6, the performance of features tests went slower by 50-100% and overall test suite went from 11min +/-1min to 17min +/-1min (with parallelism). The app had some STI and abstract model inheritance and plenty of views, and a preponderance of feature tests.
Mechanism:
Collected stats on speeds of the suite for 4.2 and 5.0.6 and compared significant changes in different specs and determined that the features were the ones with the significant change. Used stackprof (https://gist.github.com/leonelgalan/a613c1d8e293f3219327 with wall-time sampling) on a small set of feature specs and found that the top three items on the list were:
the second two were nowhere in the list in 4.2.
Searching on find_template_paths found some suggestions like rails/rails#20752 (comment) (didn't make an impact). But then reading https://github.com/rails/rails/blob/v5.0.6/actionview/lib/action_view/digestor.rb#L9 and https://github.com/rails/rails/blob/v5.0.6/actionview/lib/action_view/railtie.rb#L42 got me to creating this to work around the problem.
By adding the spec support above both the sampled feature tests and the overall suite became the same as it was on the 4.2 branch.
However this is fixed in rails 5.1: rails/rails@2380354#diff-5630af1610e29418fbdfd318b8f68eb3 provided either:
config.cache_classes = false
orconfig.cache_template_loading = true
inconfig/environments/test.rb