Most of our projects have a big code base of assets which dramatically reduce not only browser page rendering but also increase passing test suites.
The Rails asset pipeline provides an assets:precompile rake task to allow assets to be compiled and cached up front rather than compiled every time the app boots.
We found that with precompiled assets all test suite is passed faster. For some project, we got 50% improvement.
This approach shows how you can skip compilation of assets every time you run a test.
Solution
This solution for CircleCI, but you could migrate it to other CI.
Add precompile assets step with caching result into .circle/config.yml:
- restore_cache:
keys:
- v1-asset-cache-{{ arch }}-{{ .Branch }}
- v1-asset-cache-
- run: bundle exec rake assets:precompile
- save_cache:
key: v1-asset-cache-{{ arch }}-{{ .Branch }}-{{ epoch }}
paths:
- public/assets
- tmp/cache/assets/sprockets
and here complete CircleCI config.
Benchmark
We generated a simple *Rails 5 *project with Bootstrap and Foundation.
On this simple example we got 20% improvement.
There are 2 branches: master — with assets precompilation enabled and without-assets-precompilation without assets precompilation enabled
Extra: speedup Rails 4 system tests
On Rails 5, the asset digest is enabled by default which allows us to cache assets on a browser, but on Rails 4 you should add some changes in order to make it works.
Rails.application.configure do
# ...
# For Rails 4 we need to add those settings
config.serve_static_files = true
config.static_cache_control = 'public, max-age=2592000'
config.assets.digest = true
config.assets.debug = false
if ENV['CI']
config.assets.compile = false
config.assets.js_compressor = :uglifier
end
end
Conclusion
This configuration allowed us to reduce and in most cases completely drop time on the assets compilation in tests.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories.
Top comments (0)