public

  • Download Gist
eager loading benchmark for mongoid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
require "rubygems"
require "benchmark"
require "mongoid"
 
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db("mongoid_perf_test", :logger => Logger.new($stdout, :info))
end
 
Mongoid.master.collection("people").drop
Mongoid.master.collection("posts").drop
 
class Person
include Mongoid::Document
include Mongoid::Timestamps
field :name
references_many :posts
end
 
class Post
include Mongoid::Document
include Mongoid::Timestamps
field :title
referenced_in :person
end
 
10000.times do |n|
person = Person.create(:name => "Test_#{n}")
person.posts.create(:title => "Test_#{2*n}")
person.posts.create(:title => "Test_#{2*n+1}")
end
 
puts "Starting benchmark..."
Benchmark.bm do |bm|
bm.report("Finding 10 posts with person, without eager loading") do
Post.limit(10).each { |p| p.person.name }
end
 
bm.report("Finding 10 posts with person, with eager loading") do
Post.limit(10).includes(:person).each { |p| p.person.name }
end
bm.report("Finding 50 posts with person, without eager loading") do
Post.limit(50).each { |p| p.person.name }
end
 
bm.report("Finding 50 posts with person, with eager loading") do
Post.limit(50).includes(:person).each { |p| p.person.name }
end
bm.report("Finding 100 posts with person, without eager loading") do
Post.limit(100).each { |p| p.person.name }
end
 
bm.report("Finding 100 posts with person, with eager loading") do
Post.limit(100).includes(:person).each { |p| p.person.name }
end
end
 
 
Starting benchmark...
user system total real
Finding 10 posts with person, without eager loading 0.010000 0.000000 0.010000 ( 0.005121)
Finding 10 posts with person, with eager loading 0.000000 0.000000 0.000000 ( 0.001886)
Finding 50 posts with person, without eager loading 0.020000 0.000000 0.020000 ( 0.021442)
Finding 50 posts with person, with eager loading 0.000000 0.000000 0.000000 ( 0.005267)
Finding 100 posts with person, without eager loading 0.050000 0.000000 0.050000 ( 0.057718)
Finding 100 posts with person, with eager loading 0.010000 0.000000 0.010000 ( 0.010141)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.