Created
May 26, 2011 13:48
-
-
Save jferris/993173 to your computer and use it in GitHub Desktop.
Benchmark some simple factories with factory_girl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "rubygems" | |
| gem 'activerecord', "3.0.7" | |
| gem 'sqlite3', "1.3.3" | |
| gem "factory_girl", "1.3.3" | |
| require "active_record" | |
| require "factory_girl" | |
| require "benchmark" | |
| ActiveRecord::Base.establish_connection( | |
| :adapter => 'sqlite3', | |
| :database => 'test.db' | |
| ) | |
| class CreateSchema < ActiveRecord::Migration | |
| def self.up | |
| create_table "posts", :force => true do |t| | |
| t.string "title" | |
| t.text "body" | |
| t.integer "user_id" | |
| t.datetime "created_at" | |
| t.datetime "updated_at" | |
| end | |
| create_table "users", :force => true do |t| | |
| t.string "name" | |
| t.string "email" | |
| t.integer "age" | |
| t.datetime "created_at" | |
| t.datetime "updated_at" | |
| end | |
| end | |
| end | |
| CreateSchema.suppress_messages { CreateSchema.migrate(:up) } | |
| class User < ActiveRecord::Base | |
| validates_presence_of :email, :name, :age | |
| has_many :posts | |
| end | |
| class Post < ActiveRecord::Base | |
| validates_presence_of :title, :body, :user_id | |
| belongs_to :user | |
| end | |
| Factory.define :user do |factory| | |
| factory.name "Bill" | |
| factory.email "bill@example.com" | |
| factory.age 26 | |
| end | |
| Factory.define :post do |factory| | |
| factory.title "Hello" | |
| factory.body "What's up" | |
| factory.association :user | |
| end | |
| ITERATIONS = 1000 | |
| Post.delete_all | |
| User.delete_all | |
| Benchmark.bmbm do |x| | |
| x.report("Post.new") do | |
| ITERATIONS.times do | |
| user = User.new | |
| user.name = "Bill" | |
| user.email = "bill@example.com" | |
| user.age = 26 | |
| post = Post.new | |
| post.title = "Hello" | |
| post.body = "What's up" | |
| post.user = user | |
| end | |
| end | |
| x.report("Factory.build(:post)") do | |
| ITERATIONS.times do | |
| user = Factory.build(:user) | |
| Factory.build(:post, :user => user) | |
| end | |
| end | |
| x.report("Post.new with save!") do | |
| ITERATIONS.times do | |
| user = User.new | |
| user.name = "Bill" | |
| user.email = "bill@example.com" | |
| user.age = 26 | |
| user.save! | |
| post = Post.new | |
| post.title = "Hello" | |
| post.body = "What's up" | |
| post.user = user | |
| post.save! | |
| end | |
| Post.delete_all | |
| User.delete_all | |
| end | |
| x.report("Factory.create(:post)") do | |
| ITERATIONS.times do | |
| user = Factory.create(:user) | |
| Factory.create(:post, :user => user) | |
| end | |
| Post.delete_all | |
| User.delete_all | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Rehearsal --------------------------------------------------------- | |
| Post.new 0.210000 0.010000 0.220000 ( 0.220546) | |
| Factory.build(:post) 0.280000 0.000000 0.280000 ( 0.278631) | |
| Post.new with save! 3.620000 1.180000 4.800000 ( 6.884482) | |
| Factory.create(:post) 3.710000 1.200000 4.910000 ( 6.995768) | |
| ----------------------------------------------- total: 10.210000sec | |
| user system total real | |
| Post.new 0.180000 0.000000 0.180000 ( 0.178181) | |
| Factory.build(:post) 0.230000 0.000000 0.230000 ( 0.235513) | |
| Post.new with save! 3.580000 1.190000 4.770000 ( 6.832289) | |
| Factory.create(:post) 3.730000 1.180000 4.910000 ( 7.073387) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment