Created
December 22, 2014 22:26
-
-
Save nmeylan/c26d846aabc161367a5b to your computer and use it in GitHub Desktop.
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
RAILS_VERSION = '4.2.0' # switch '4.1.8' '4.2' | |
# Remember to rm Gemfile when switch version. | |
unless File.exist?('Gemfile') | |
File.write('Gemfile', <<-GEMFILE) | |
source 'https://rubygems.org' | |
gem 'rails', '#{RAILS_VERSION}' | |
gem 'sqlite3' | |
gem 'pg' | |
gem 'byebug' | |
GEMFILE | |
system 'bundle' | |
end | |
require 'bundler' | |
Bundler.setup(:default) | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
require 'byebug' | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
# ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :issues do |t| | |
t.string :title | |
t.integer :tracker_id, index: true | |
t.integer :category_id, index: true | |
t.timestamps null: false | |
end | |
create_table :nested_relations do |t| | |
t.string :name | |
t.timestamps null: false | |
end | |
create_table :categories do |t| | |
t.string :name | |
t.timestamps null: false | |
end | |
create_table :trackers do |t| | |
t.string :name | |
t.integer :nested_relation_id, index: true | |
t.timestamps null: false | |
end | |
end | |
class Category < ActiveRecord::Base; | |
end | |
class NestedRelation < ActiveRecord::Base; | |
end | |
class Tracker < ActiveRecord::Base | |
belongs_to :nested_relation | |
end | |
class Issue < ActiveRecord::Base | |
belongs_to :tracker | |
belongs_to :category | |
end | |
# Insert categories | |
categories_iterations = 50 | |
categories_iterations.times do |_| | |
category = Category.new | |
category.name = 'hello' | |
category.save | |
end | |
# Insert nested relation | |
nested_relations_iterations = 3 | |
nested_relations_iterations.times do |_| | |
nested_relation = NestedRelation.new | |
nested_relation.name = 'hello' | |
nested_relation.save | |
end | |
# Insert trackers | |
nested_relations = NestedRelation.all | |
nested_relations_count = nested_relations.size - 1 | |
trackers_iterations = 3 | |
trackers_iterations.times do |_| | |
tracker = Tracker.new | |
tracker.name = 'Tracker name' | |
tracker.nested_relation = nested_relations[rand(0..nested_relations_count)] | |
tracker.save | |
end | |
# Insert issues | |
issues_iterations = 10_000 | |
trackers = Tracker.all | |
categories = Category.all | |
trackers_count = trackers.size - 1 | |
categories_count = categories.size - 1 | |
issues_iterations.times do |_| | |
issue = Issue.new | |
issue.title = 'Issue title' | |
issue.tracker = trackers[rand(0..trackers_count)] | |
issue.category = categories[rand(0..categories_count)] | |
issue.save | |
end | |
ITERATIONS = 5 | |
# Database contains : | |
# 10_000 issues | |
# 30 categories | |
# 3 trackers | |
# 3 nested_relations | |
Benchmark.bm(40) do |bm| | |
ITERATIONS.times do |time| | |
puts "\nIteration n: #{time + 1}" | |
issues = Issue.all.includes(:category, tracker: :nested_relation) # To bypass cache | |
bm.report("Rails #{RAILS_VERSION} : 1 relation") do | |
issues.each do |issue| | |
caption = issue.tracker.name | |
end | |
end | |
issues = Issue.all.includes(:category, tracker: :nested_relation) # To bypass cache | |
bm.report("Rails #{RAILS_VERSION} : 2 relations") do | |
issues.each do |issue| | |
caption = issue.tracker.name | |
caption1 = issue.category.name | |
end | |
end | |
issues = Issue.all.includes(:category, tracker: :nested_relation) # To bypass cache | |
bm.report("Rails #{RAILS_VERSION} : 1 nested relation") do | |
issues.each do |issue| | |
caption1 = issue.tracker.nested_relation.name | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment