Last active
October 22, 2015 13:26
-
-
Save bogdan/74ac22af36e62c3040de 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
$:.unshift("./lib") | |
$:.unshift("../active_support/lib") | |
$:.unshift("../active_model/lib") | |
require "active_record" | |
require "diffbench" | |
GC.disable | |
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:") | |
ActiveRecord::Base.configurations = {} | |
#ActiveRecord::Base.logger = TEST_LOGGER | |
ActiveRecord::Schema.verbose = false | |
ActiveRecord::Schema.define(:version => 1) do | |
create_table :entries do |t| | |
t.integer :group_id | |
t.string :name | |
t.timestamps | |
end | |
create_table :groups do |t| | |
t.string :name | |
t.timestamps | |
end | |
class ::Entry < ActiveRecord::Base | |
belongs_to :group | |
end | |
class ::Group < ActiveRecord::Base | |
has_many :entries | |
end | |
end | |
group = Group.create!(name: 'main') | |
entry = group.entries.create!(name: 'hello') | |
association = entry.association(:group) | |
reflection = association.reflection | |
100.times do | |
Group.create! | |
end | |
DiffBench.bm do |x| | |
#x.report "column_name" do | |
#100000.times do | |
#association.respond_to?(:cached_counter_attribute_namel) ? | |
#association.cached_counter_attribute_namel : reflection.counter_cache_column | |
#end | |
#end | |
#x.report "association created" do | |
#puts 'start' | |
#100.times do | |
#group.entries.create!(name: 'hello') | |
#end | |
#end | |
[10, 100].map do |groups_count| | |
[1, 10, 100].map do |entries_count| | |
Entry.delete_all | |
groups = Group.limit(groups_count).map do |g| | |
entries_count.times do | |
g.entries.create!(name: 'zz') | |
end | |
g.entries.reset | |
g | |
end.to_a | |
preloader = ActiveRecord::Associations::Preloader.new | |
iterations = [groups_count, entries_count].max <= 10 ? 1000 : (groups_count * entries_count >= 10000 ? 10 : 100) | |
#iterations = 1000 | |
#iterations = 1 | |
x.report "includes #{iterations} iterations #{entries_count} associated for #{groups_count} given" do | |
iterations.times do | |
Group.limit(groups_count).includes(:entries).to_a | |
end | |
end | |
#x.report "preload #{iterations} iterations #{entries_count} associated for #{groups_count} given" do | |
#iterations.times do | |
#groups.map{|g| g.entries.reset} | |
##raise groups.count.inspect | |
#preloader.preload(groups, :entries) | |
#end | |
#end | |
#reflection = Group.reflect_on_association(:entries) | |
#all_groups = Group.all | |
#x.report "fetch #{iterations} iterations #{entries_count} associated for #{groups_count} given" do | |
#iterations.times do | |
#preloader = ActiveRecord::Associations::Preloader.new | |
#loader = ActiveRecord::Associations::Preloader::HasMany.new( | |
#Entry, groups, reflection, all_groups) | |
#loader.send(:associated_records_by_owner, preloader) | |
#end | |
#end | |
end | |
end | |
end | |
GC.enable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment