Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchamark #77

Merged
merged 1 commit into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this was copied from spec/db/schema.rb. If we think benchmarks and correctness tests will use the same schemas can we share that file across both types of tests. Alternatively we think the schemas for benchmarks and correctness tests will diverge, can we slim down benchmark/db/schema.rb to just the schema elements needed by the current benchmarks.


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