-
-
Save dougal/992170 to your computer and use it in GitHub Desktop.
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 |
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.
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.
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.
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.
- 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.
- 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.
My point was that you were being rude about someone elses project. Criticism is fine.
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.
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)