TestProf: Profiling and Speeding Up Ruby and Rails Test Suites

TestProf: Profiling and Speeding Up Ruby and Rails Test Suites

Slow test suites kill developer productivity. A 10-minute RSpec run means 10 minutes of broken feedback loops. TestProf is a collection of profilers and optimisation tools built specifically for Ruby and Rails tests — it finds the slowdowns and tells you exactly what to fix.

Installation

# Gemfile
group :test do
  gem 'test-prof'
end
bundle install

No configuration required to start profiling. Most features activate via environment variables, so you can run them against your existing suite without touching test files.

RubyProf Integration

RubyProf gives a full call-graph profile of your test suite:

TEST_RUBY_PROF=1 bundle exec rspec

Output options:

TEST_RUBY_PROF=call_stack bundle exec rspec   # HTML call stack visualisation
TEST_RUBY_PROF=graph      bundle exec rspec   # GraphHTML (best for finding hot methods)

Open the generated HTML file to see which methods consume the most time across all tests. Common culprits: database setup, serialisation, and deeply nested callbacks.

StackProf Integration

StackProf is lighter than RubyProf and works better in CI:

TEST_STACK_PROF=1 bundle exec rspec

This produces a binary .dump file. Analyse it with:

bundle exec stackprof --text tmp/stack_prof/run.dump | head -30

Target the methods at the top of the list — those are your bottlenecks.

EventProf

EventProf measures how much time specific events take across all tests. The most useful target is factory.create:

EVENT_PROF=factory.create bundle exec rspec

Sample output:

[EVENT PROF] factory.create
Total time: 45.2s
Total events: 3842

Top 5 slowest groups (by time):
OrdersController  ./spec/controllers/orders_controller_spec.rb:1  (18.3s / 1231 events)
UserMailer        ./spec/mailers/user_mailer_spec.rb:1           (9.1s / 623 events)
...

This tells you which spec files create the most objects and spend the most time in factory setup. Fix those files first for the biggest impact.

Other useful event targets:

EVENT_PROF=sql.active_record bundle exec rspec   # total SQL query time
EVENT_PROF=instantiation.active_record bundle exec rspec  # AR instantiation overhead

FactoryProf

FactoryProf gives a detailed breakdown of factory usage:

FPROF=1 bundle exec rspec

Output:

[TEST PROF] Factories usage

Total: 3842
Total top-level: 1209

   total   top-level   name
    1823         891   user
     612         201   order
     401          89   product

total = all factory calls (including nested associations). top-level = explicit create(:factory) calls in tests.

High total vs top-level ratios mean factories are creating many hidden associations — prime candidates for build_stubbed or create_list optimisation.

TagProf

TagProf groups slow tests by metadata tag:

TAG_PROF=type bundle exec rspec

Output shows which spec types (:model, :controller, :request, :system) are slowest, helping you prioritise where to focus.

let_it_be

The biggest single optimisation for database-heavy tests. Standard let(:user) { create(:user) } re-creates the record before every example. let_it_be creates it once per context and wraps everything in a transaction:

# Gemfile: gem 'test-prof'
require 'test_prof/recipes/rspec/let_it_be'

RSpec.describe User, type: :model do
  let_it_be(:user) { create(:user) }
  let_it_be(:admin) { create(:user, :admin) }

  it "has a name" do
    expect(user.name).to be_present
  end

  it "can be an admin" do
    expect(admin).to be_admin
  end
end

Both examples share the same user and admin rows — created once, rolled back after the group. For specs with 50+ examples on a shared setup, this can cut factory time by 90%.

Modifiers

let_it_be(:order, reload: true) { create(:order) }   # reload before each example
let_it_be(:cart,  refind: true) { create(:cart) }    # re-query from DB before each example

Use reload: true when tests mutate the record and you need fresh state.

before_all

before_all is the hook equivalent of let_it_be:

require 'test_prof/recipes/rspec/before_all'

RSpec.describe "bulk import", type: :request do
  before_all do
    @products = create_list(:product, 100)
    @category = create(:category)
  end

  it "imports all products" do
    post "/import", params: { category_id: @category.id }
    expect(response).to have_http_status(:ok)
  end
end

The block runs once before all examples in the group. Use @instance_variables inside — let is not available in before_all.

build_stubbed

build_stubbed creates an object that behaves like a persisted ActiveRecord model but never touches the database:

let(:user) { build_stubbed(:user) }

it "returns the user's full name" do
  expect(user.full_name).to eq "#{user.first_name} #{user.last_name}"
end

Use it for unit tests that only exercise model methods. Reserve create for tests that need real persistence (associations, scopes, callbacks that hit the DB).

AnyFixture

AnyFixture lets you share setup across the entire test suite run:

require 'test_prof/any_fixture'

RSpec.configure do |config|
  config.before(:suite) do
    TestProf::AnyFixture.register(:countries) do
      load Rails.root.join("db/seeds/countries.rb")
    end
  end
end

# In specs:
before(:all) { TestProf::AnyFixture.use(:countries) }

Seed data loads once per suite, not once per file or example.

Practical Checklist

When a Rails test suite is slow:

  1. Run EventProf → find which files create the most factories
  2. Run FactoryProf → identify factories with deep hidden associations
  3. Replace let with let_it_be in specs with many examples on stable data
  4. Replace create with build_stubbed in pure unit tests
  5. Run StackProf → find non-factory hot spots (serialisation, callbacks, etc.)
  6. Profile with --format progress in CI → see per-test times without full profiler overhead

A typical mid-size Rails suite drops from 8–10 minutes to under 3 minutes after applying let_it_be and build_stubbed systematically.

Monitoring Suite Health Over Time

TestProf finds the slow spots — but once you've optimised, you need to prevent regression. HelpMeTest runs your test suite on a schedule and tracks trends in run time and pass rate, alerting you when a new factory or callback creeps back in. Connect your suite at helpmetest.com.

Read more

Start now free