Skip to content

Commit

Permalink
Add benchamark
Browse files Browse the repository at this point in the history
  • Loading branch information
PikachuEXE committed Dec 5, 2018
1 parent b645d02 commit acaf4ff
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
33 changes: 33 additions & 0 deletions benchmark/db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

ActiveRecord::Schema.define(version: 0) do

create_table(:blogs, force: true) do |t|
t.string :name
end

create_table(:posts, force: true) do |t|
t.string :title
t.integer :blog_id
t.integer :author_id
t.string :owner_type
t.integer :owner_id
end

create_table(:users, force: true) do |t|
t.string :name
end
end

class Blog < ActiveRecord::Base
has_many :posts
end

class Post < ActiveRecord::Base
belongs_to :blog
belongs_to :author, class_name: 'User'
end

class User < ActiveRecord::Base
has_many :posts, foreign_key: :author_id
end
53 changes: 53 additions & 0 deletions benchmark/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

$LOAD_PATH.push File.expand_path('lib', __dir__)

require 'benchmark/ips'

ENV['RAILS_ENV'] = 'test'

require 'active_record'

ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: ':memory:'
)

require_relative './db/schema.rb'

# Setup data
blog_1 = Blog.create!
blog_2 = Blog.create!
100.times do
user_1 = User.create!
user_2 = User.create!
Post.create!(author: user_1, blog: blog_1)
Post.create!(author: user_2, blog: blog_2)
end

Benchmark.ips do |x|
x.time = 5
x.warmup = 2

# Use AR's eager loading
x.report('AR eager loading: ') do
::Blog.all.includes(posts: :author).each do |blog|
blog.posts.each do |post|
post.author.id
end
end
end

require 'goldiloader'

# Use goldiloader
x.report('AR with goldiloader: ') do
::Blog.all.each do |blog|
blog.posts.each do |post|
post.author.id
end
end
end

x.compare!
end
1 change: 1 addition & 0 deletions goldiloader.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'activesupport', '>= 4.2', '< 5.3'

spec.add_development_dependency 'appraisal'
spec.add_development_dependency 'benchmark-ips'
spec.add_development_dependency 'coveralls'
spec.add_development_dependency 'database_cleaner', '>= 1.2'
spec.add_development_dependency 'mime-types'
Expand Down

0 comments on commit acaf4ff

Please sign in to comment.