Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
speed up rails 5.0 feature specs by caching templates during suite
# 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
@timdiggins

This comment has been minimized.

Copy link
Owner Author

commented Jan 29, 2018

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:

     26432  (16.8%)       26432  (16.8%)     (garbage collection)
      9921   (6.3%)        9921   (6.3%)     ActionView::PathResolver#find_template_paths
     40713  (25.9%)        7433   (4.7%)     ActionView::Template#compile

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 or config.cache_template_loading = true in config/environments/test.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.