Forked from composerinteralia/factory_bot_benchmark.rb
Created
June 23, 2020 17:07
-
-
Save sashadev-sky/9b205c2337b14c9fe835348b40e8ddce to your computer and use it in GitHub Desktop.
Factory Bot Benchmark
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
begin | |
require "bundler/inline" | |
rescue LoadError => e | |
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler" | |
raise e | |
end | |
gemfile(true) do | |
source "https://rubygems.org" | |
gem "rails" | |
gem "sqlite3" | |
gem "factory_bot" | |
end | |
require "active_record" | |
require "logger" | |
require "factory_bot" | |
require 'benchmark' | |
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") | |
ActiveRecord::Schema.define do | |
create_table :users, force: true do |t| | |
end | |
create_table :posts, force: true do |t| | |
t.integer :user_id | |
end | |
end | |
class User < ActiveRecord::Base | |
has_many :posts | |
end | |
class Post < ActiveRecord::Base | |
belongs_to :user | |
end | |
FactoryBot.define do | |
factory :user do | |
end | |
factory :post do | |
user | |
end | |
end | |
TIMES = 2000 | |
puts "With no associations:" | |
Benchmark.bmbm do |bm| | |
bm.report("new") { TIMES.times { User.new } } | |
bm.report("build") { TIMES.times { FactoryBot.build(:user) } } | |
bm.report("build_stubbed") { TIMES.times { FactoryBot.build_stubbed(:user) } } | |
bm.report("attributes_for") { TIMES.times { FactoryBot.attributes_for(:user) } } | |
bm.report("create") { TIMES.times { FactoryBot.create(:user) } } | |
end | |
puts "\n###########################################################\n\n" | |
puts 'With one association:' | |
Benchmark.bmbm do |bm| | |
bm.report("new") { TIMES.times { Post.new } } | |
bm.report("attributes_for") { TIMES.times { FactoryBot.attributes_for(:post) } } | |
bm.report("build_stubbed") { TIMES.times { FactoryBot.build_stubbed(:post) } } | |
bm.report("build") { TIMES.times { FactoryBot.build(:post) } } | |
bm.report("create") { TIMES.times { FactoryBot.create(:post) } } | |
end | |
puts "\n###########################################################\n\n" | |
puts 'With one association using parent strategy:' | |
FactoryBot.use_parent_strategy = true | |
Benchmark.bmbm do |bm| | |
bm.report("new") { TIMES.times { Post.new } } | |
bm.report("attributes_for") { TIMES.times { FactoryBot.attributes_for(:post) } } | |
bm.report("build") { TIMES.times { FactoryBot.build(:post) } } | |
bm.report("build_stubbed") { TIMES.times { FactoryBot.build_stubbed(:post) } } | |
bm.report("create") { TIMES.times { FactoryBot.create(:post) } } | |
end | |
=begin | |
With no associations: | |
Rehearsal -------------------------------------------------- | |
new 0.018025 0.000013 0.018038 ( 0.018047) | |
build 0.135542 0.000384 0.135926 ( 0.136007) | |
build_stubbed 0.257159 0.002513 0.259672 ( 0.259871) | |
attributes_for 0.284504 0.005921 0.290425 ( 0.290730) | |
create 0.632074 0.004213 0.636287 ( 0.643327) | |
----------------------------------------- total: 1.340348sec | |
user system total real | |
new 0.017522 0.000055 0.017577 ( 0.017611) | |
build 0.093808 0.000660 0.094468 ( 0.095229) | |
build_stubbed 0.207590 0.000903 0.208493 ( 0.209350) | |
attributes_for 0.219949 0.001365 0.221314 ( 0.222297) | |
create 0.599802 0.002649 0.602451 ( 0.604623) | |
########################################################### | |
With one association: | |
Rehearsal -------------------------------------------------- | |
new 0.019851 0.000089 0.019940 ( 0.020124) | |
attributes_for 0.286370 0.001803 0.288173 ( 0.289053) | |
build_stubbed 0.577320 0.005476 0.582796 ( 0.586311) | |
build 0.877632 0.004104 0.881736 ( 0.884276) | |
create 1.552896 0.004795 1.557691 ( 1.559153) | |
----------------------------------------- total: 3.330336sec | |
user system total real | |
new 0.020835 0.000026 0.020861 ( 0.020866) | |
attributes_for 0.222913 0.001709 0.224622 ( 0.224888) | |
build_stubbed 0.501088 0.003089 0.504177 ( 0.504612) | |
build 0.789108 0.002804 0.791912 ( 0.792456) | |
create 1.478119 0.003536 1.481655 ( 1.483404) | |
########################################################### | |
With one association using parent strategy: | |
Rehearsal -------------------------------------------------- | |
new 0.019415 0.000020 0.019435 ( 0.019442) | |
attributes_for 0.298382 0.003267 0.301649 ( 0.301948) | |
build 0.286925 0.001244 0.288169 ( 0.288437) | |
build_stubbed 0.552910 0.002654 0.555564 ( 0.556511) | |
create 1.493076 0.003569 1.496645 ( 1.497386) | |
----------------------------------------- total: 2.661462sec | |
user system total real | |
new 0.020740 0.000106 0.020846 ( 0.020885) | |
attributes_for 0.225570 0.001421 0.226991 ( 0.227156) | |
build 0.253338 0.000944 0.254282 ( 0.254623) | |
build_stubbed 0.523369 0.002687 0.526056 ( 0.526744) | |
create 1.466646 0.004365 1.471011 ( 1.472622) | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment