Andy Harvey
Andy Harvey

Reputation: 12653

How to identify bottlenecks and optimise a slow test suite?

What is the best strategy to systematically identify where slowdowns are happening, and where to focus optimisation efforts?

I know, I know. A simple Google search reveals many articles about optimising tests. Most of these suggest minimising interactions with the database and external APIs — all good advice, but this question is very different.

The background:

I've inherited a Rails app with a very slow test suite — run time is >45 mins! I've done some preliminary digging to try and figure out why it's slow.

I've run components individually, and it looks like Controllers, Models, etc are all reasonably fast, but that maybe 95% of the time is spent in System specs.

It also looks like a lot of the delay occurs in initialisation. The first spec in any given file seems to run in anywhere from 15-30 seconds, whereas all subsequent specs in the same file are running much quicker at <2 seconds.

But these are all educated guesses and observations, based on manually running a few different specs. And not really getting me any closer to working out where delays are occurring.

What is the best way to work out where I should focus my optimisation efforts - at a very basic level even working out whether I need to optimise the test suite or the app itself? And what is the best way to systematically identify and monitor which routines and methods are running slowly?

Upvotes: 1

Views: 737

Answers (2)

Greg
Greg

Reputation: 6628

Before bandaging the problem with Spring, I'd suggest some investigation.

Since your system specs are consistently slow when running the first one, pick any one and run it with the profiler https://ruby-doc.org/stdlib-2.6/libdoc/profiler/rdoc/Profiler__.html

Maybe it will show you slow initializers? If the tests are using fixtures - maybe loading them takes the most time and you'd consider switching to factories?

Isolate the slow part, improve it, repeat until you're satisfied.

It's really open ended problem, so it's hard to be more specific.

Upvotes: 1

fylooi
fylooi

Reputation: 3870

It's normal for the first spec to run slow as RSpec needs to load your entire Rails application.

The first thing you should do is to profile your specs with rspec --profile

After that, look at the top bunch and try to identify patterns in your system specs. Some culprits may include:

  • Too many examples in a system spec - you can consolidate more assertions into less examples for faster test execution, at the cost of more opaque test failures and debugging effort.
  • Overuse of manual waiting, eg. sleep(1) to ensure a JS collapse panel has fully opened. Proper use of Capybara have_ matchers might speed up things. Turning off animations for your CSS framework may help too.

Upvotes: 3

Related Questions