-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b645d02
commit acaf4ff
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 |
This file contains 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
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 |
This file contains 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