Skip to content

Instantly share code, notes, and snippets.

@dougal
Created May 25, 2011 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dougal/992170 to your computer and use it in GitHub Desktop.
Save dougal/992170 to your computer and use it in GitHub Desktop.
Factory Girl vs User.create
phoenix:stats [master*]$ rails runner 'Benchmark.bm {|x| x.report { 1000.times { Factory.create(:user) } } }'
user system total real
5.050000 0.890000 5.940000 ( 7.895676)
phoenix:stats [master*]$ rails runner 'Benchmark.bm {|x| x.report { 1000.times { |i| User.create(:email => "me@example#{i}.com", :name => "bob", :password => "password1") } } }'
user system total real
5.110000 0.920000 6.030000 ( 8.807853)
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin10.6.0]
Rails 3.1.0.rc1
sqlite3 (1.3.3)
factory_girl_rails (1.0.1)
class User < ActiveRecord::Base
include Clearance::User
# Used on settings screen to display a field for current password.
attr_reader :current_password
validates :name, :presence => true
validates :time_zone, :presence => true
has_many :projects
end
# All three of these attributes are required. Authentication setup by clearance. https://github.com/thoughtbot/clearance
Factory.define :user do |user|
user.name "Bob Smith"
user.sequence(:email) { |n| "user#{n}@example.com" }
user.password "password"
end
@dougal
Copy link
Author

dougal commented May 25, 2011

ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin10.6.0]
Rails 3.1.0.rc1
sqlite3 (1.3.3)
factory_girl_rails (1.0.1)

@egervari
Copy link

Your example is too simple to make a real, legitimate comparison. The example on Stackoverflow is far more realistic of a real project - because it IS a real project. As the User model grows to be more complex, the Factory.create call will eventually get slower and slower compared to calling Rails directly.

@dougal
Copy link
Author

dougal commented May 25, 2011

I can't see how that would possibly be the case. Factory girl is running the exact same code Rails' code under the hood, it just provides some nice syntactic sugar to make setting default attributes easy. Have a look at the source, there is nothing magic going on there.

@egervari
Copy link

Maybe the syntactic sugar is what is causing the slow downs? I'm not sure... I just know that calling Factory.build() many times was many orders of magnitude slower than building the objects myself. The proof is in the pudding... no? The tests are so much faster by not using factory_girl. It is night and day.

@dougal
Copy link
Author

dougal commented May 25, 2011

Even when you call Factory.build(), and related objects are created, not built. this is where you database time is going. It is possible to write the factories without this happening.

I'm sure your open source projects have lots to be bitched about. Oh, no, wait... you have none.

@egervari
Copy link

  1. If you look at the post on stackoverflow, you will see that I already provided an example where I forced factory_girl to use Factory.build() for EVERY association. I have proved that it never touched the database once, and yet the tests still took forever to run. I have shown this already. So your assumption that Factory.build() using Factory.create() for the associations is irrelevant.
  2. Making or not making an open-source project has NOTHING to do with pointing out an error in another open-source project. If something is true, it is true. It does not matter who says it or how many open-source projects they have under their belt.

Nonetheless, I just started using Ruby so I obviously won't have any Ruby/github projects. All of my experience has been in the Java world, and I have written/contributed to many open-source projects in this space.

@dougal
Copy link
Author

dougal commented May 25, 2011

My point was that you were being rude about someone elses project. Criticism is fine.

@TheEmpty
Copy link

TheEmpty commented Jun 6, 2011

FactoryGirl is a great tool and much better then building the objects. For example when we switched the way we saved associations it just required two line changes and all our tests still passed. If we have to go over every time we built it... dear Lord. Not to mention that we are always adding new fields and validations. I'll have to give this a shot on my own project soon as it has a much more complex user model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment