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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With