5 Ways to Increase the Performance of Your RSpec Tests

At ChaiOne, we highly encourage that developers write tests in their projects. TDD (Test Driven Development) is also encouraged. Testing (and TDD) prevents bugs in production, forces better code, and encourages developer confidence when making changes to existing code bases.

In most of ChaiOne’s Ruby/Rails projects, RSpec is the tool of choice. It provides a unique and understandable way of writing tests that makes TDD even easier. Once you use the RSpec syntax, you’ll never (or won’t want to) go back.

Working with Large Code Bases

In a large code base, testing becomes even more important. While some argue that testing slows development down, we argue that it increases development velocity over time, especially when refactoring existing code and eliminating technical debt.

TDD encourages running your tests frequently and often. What you might find in a large code base however, is that running the full test suite locally in development is unrealistic. If your test suite takes 25 minutes to run 2000 tests, running all tests as you make changes will not be productive.

To help mitigate this, a two-fold solution can be used.

First, run unit tests that are directly affected by your code changes frequently. Guard::RSpec is a configurable development tool for running tests on file changes. Guard or a similar test runner can help catch errors quickly when working on a feature or change.

Secondly, and the focus of this post, is to increase the speed of your test suite. For our projects, we use a dedicated CI (Continuous Integration) server like Jenkins for running the full test suite. Developers make changes on a feature branch, commit, and push changes to Github. Jenkins will pull down these changes and run the entire test suite.

Lowering the test suite clock time provides feedback to the developer faster, increases productivity, and lowers the likelihood of test failures going unnoticed.

Let’s take a look at some ways to optimize your test suite for performance.

37 tests in 6 minutes. How is it so slow you ask? How do we make it fast?

The goal of this spec is to make various API requests to our internal API, asserting that the result is correct. In this case it serves as a form of a functional test, comparing the result of a request against the specification for that endpoint. In this test we are making commits to an actual test database.

In this spec, a pattern emerges: for each test we are doing a significant amount of setup. The following code runs before every test:

There are several ways to optimize this type of test, including various ways of setting up the environment. Here is one way to increase the performance of the spec.

If we can treat the environment as immutable and guarantee that the data is not modified between tests, then we can perform an optimization. By setting up the environment only once, the test will be dramatically faster.

We can move the setup into a before(:all) block, replacing lets with instance variables. This will create the data to assert against only once, and as long as we never change the data between tests, we should see a much faster passing spec.

All this speed comes with some warnings. First off, this type of optimization should only be done when you are making assertions on (what can be considered) immutable data. For example, in the above spec we are setting up an environment, then making GET network requests. Making assertions on a single response is ok in this instance, because the data is not changing.

In many cases though, this is not an optimization you should make. By default, a clean environment must be used for every test. Seeing what these types of optimizations bring you, however, will remind you of the expense of setup in tests.

2. Decrease Redundant Network Requests

In another spec we face a similar setup bottleneck as the previous example, but we are also making numerous assertions on a network request’s JSON response like this:

If we optimize this spec by making our request only once in a before(:all), we can make individual assertions without incurring the setup cost or the network request overhead.

Before:

Finished in 38.32 seconds
28 examples, 0 failures

After:

Finished in 3.9 seconds
28 examples, 0 failures

That’s 10 times faster.

3. Increase your Hardware Capability

While this might seem like a no brainer, throwing hardware at the problem will produce results. You can easily see double the (non-parallel) performance on your test suite by upgrading your CI server from a machine that is a couple years old (especially if you are moving away from a machine with a spinning disk).

As an added bonus, Jenkins can support slaves which makes it easier to add additional machines to your CI stack. You can also specify which machine to prefer per project, giving your largest projects preference on the faster machines.

4. Parallel Tests

Running specs across multiple CPU cores can further decrease the test time for your suite. ParallelTests is a very popular option for Ruby projects. It uses a database per thread and can run on CI servers like Jenkins.

Making your test suite stable in parallel will also make hardware upgrades even more effective, scaling out to machines with more cores. Single-core performance is no longer your bottleneck.

When parallelizing your suite, beware of shared state between tests that lives outside of the database such as search applications like ElasticSearch. These interdependencies will need to be addressed when making your suite parallel.

5. Failure Visibility (For Fun)

Another performance-unrelated optimization is using visual indicators to show the status of the build. As development teams we find some camaraderie among keeping the build green. We’ve been experimenting with the wifi-enabled Philips Hue light. Our post-build process has a simple hook that will turn the light green or red depending on the result. Fun times.