Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I speed up the creation of 5,000 records for my rspec tests?

I am using Ruby on Rails 3.2.2, FactoryGirl 3.1.0, FactoryGirlRails 3.1.0, Rspec 2.9.0 and RspecRails 2.9.0. In order to test my application I have to create a lot of records (about 5000) in the database, but that operation is very slow (it takes more than 10 minutes to create records). I proceed like this:

before(:each) do
  5000.times do
    FactoryGirl.create(:article,)
  end
end

How can I improve my spec code so to go faster?

Note: Maybe the slowness is given by (5) article callbacks that run before and after each article creation process, but I can skip those (since the only things I have to test are articles and not associated models) if those slow creation of records... is it possible to make that and is it the right way to proceed?

like image 332
user12882 Avatar asked May 16 '12 04:05

user12882


1 Answers

When you start processing large number of records like that, it's better to do it in sql directly to avoid the ActiveRecord overhead that you don't need.
I would look to write a script or create a stored procedure to do this dependig on your sql implementation. It'll quite likely finish in a few seconds.

like image 78
Michael Durrant Avatar answered Sep 18 '22 06:09

Michael Durrant