JT Book a call

Blog

4 lines to Speed Up your Rails Test Suite on CircleCI

Published

2 min read

4 lines to Speed Up your Rails Test Suite on CircleCI
In this post

Meme reading You don’t need to compile assets if you don’t have any

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 #

Two successful CircleCI builds, 45 seconds with asset caching and 1 minute 3 seconds without

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.

Tell us what you need built

JetThoughts has been building software since 2008. Book a call, or write to sales@jetthoughts.com. You'll leave the call with a written one-page scope within 2 working days.

#01 Paul Keen
Paul KeenChief Technology Officer & Open Source Contributor

A 30-minute call; pick a time.