Rspec extremely slow
Asked Answered
R

2

6

My rspec tests seem to run extremely slow even with guard & spork.

Finished in 5.36 seconds
13 examples, 2 failures

I understand that there are several things I can do to optimize my tests & reduce interaction with the database, but I strongly suspect that the spec_helper has been improperly setup. I'm on rails 3.2.11 with mongoid. Database cleaner cleans up after every run.

spec_helper.rb

require 'rubygems'
require 'spork'
Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'
  require 'capybara/rspec'

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
  DatabaseCleaner[:mongoid].strategy = :truncation

  RSpec.configure do |config|
    config.infer_base_class_for_anonymous_controllers = false
    config.order = "random"
    config.filter_run focus: true
    config.filter_run_excluding :remove => true
    config.run_all_when_everything_filtered = true
    config.include Mongoid::Matchers
    config.include Capybara::DSL
    ActiveSupport::Dependencies.clear
  end
end


Spork.each_run do
  Fabrication.clear_definitions
  RSpec.configure do |config|
    config.before(:each) do
      DatabaseCleaner.clean
    end
  end
end

UPDATE: The problem was with one of my tests. It was taking 3 seconds. Please check @Sam Peacey's answer for the command I used to get the below result

Dynamic Model should destroy collection when related source is destroyed
    2.46 seconds ./spec/models/dynamic_model_spec.rb:10
Dynamic Model Validations should validate uniqueness
    0.66357 seconds ./spec/models/dynamic_model_spec.rb:69
Rosenfeld answered 18/3, 2013 at 7:21 Comment(12)
5 seconds isn't that bad. Ruby isn't C you know...Nicholenicholl
For 13 tests? I've seen people run 200+ tests in 5-6 seconds. The difference seems to be too large.Rosenfeld
I've had the Ruby VM take something like 3-4 seconds just to start up, and that's on recent hardware.Nicholenicholl
That's correct. But doesn't Rspec exclude that time from its log? So 5 seconds should address tests only right?Rosenfeld
I agree that's slow for just 13 tests, have you tried removing the database cleaner calls just to see if that's the culprit (some of your tests will quite possibly fail, but you may find out where the problem is)?Callant
I thought you were talking about total running time (like time ruby something), not RSpec time. In that case, you may have a point.Nicholenicholl
Have you tried running your specs individually to see which one is taking the longest? This may give you some clues.Myxoma
@cheeseweasel 5.14 seconds without database cleaner.Rosenfeld
@Myxoma Only 1 test file. Gist hereRosenfeld
Rather then running them individually, you can run with the -p flag, which will tell you how long the 10 (by default, you can change this) slowest examples take.Callant
That's fantastic. one of my tests was taking 3 seconds to run. Thanks @cheeseweasel. Could you please convert this to an answer :)Rosenfeld
Okay great, glad to hear it helped. :)Callant
C
15

You can profile your specs by running rspec with the -p / --profile flag:

rspec spec -p [-drb, and whatever else]

This will list the 10 slowest examples with their execution time. You can change the default of 10 by providing an optional count to the -p flag. More info by using rspec --help

Callant answered 18/3, 2013 at 8:28 Comment(0)
F
1

In my case problem was in RSpec running in development ENV, not in test. I fixed it by adding ENV['RAILS_ENV'] ||= 'test' as a first line in spec_helper.rb - otherwise it didn't worked.

Also I must recommend very useful gem for profiling code - test-prof

Flaggy answered 2/1, 2019 at 5:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.