Skip to content

Instantly share code, notes, and snippets.

@lucasrenan
Created April 18, 2012 20:38
Show Gist options
  • Save lucasrenan/2416419 to your computer and use it in GitHub Desktop.
Save lucasrenan/2416419 to your computer and use it in GitHub Desktop.
mongoid tests - embed x reference
require "benchmark"
require "mongoid"
require "./lib/perf/models"
Mongoid.configure do |config|
config.master = Mongo::Connection.new("localhost", 27017).db("embedded_referenced_development")
end
Mongoid.purge!
puts "Creating indexes..."
[ Person, Post, Address ].each(&:create_indexes)
person = Person.create(:birth_date => Date.new(1970, 1, 1))
times = [ 100, 1000, 10000, 100000 ]
puts "Starting benchmark..."
Benchmark.bm do |bm|
puts "\n[ Embedded 1-n Benchmarks ]"
times.each do |i|
Mongoid.unit_of_work do
puts "[ #{i} ]"
bm.report("#create ") do
i.times do |n|
person.addresses.create(
:street => "Wienerstr. #{n}",
:city => "Berlin",
:post_code => "10999",
:priority => n
)
end
end
bm.report("#first ") do
person.addresses.first
end
bm.report("#first_desc_priority") do
person.addresses.desc(:priority).first
end
bm.report("#first_asc_priority ") do
person.addresses.asc(:priority).first
end
bm.report("#count ") do
person.addresses.count
end
person.addresses.clear
GC.start
bm.report("#push (batch) ") do
[].tap do |addresses|
i.times do |n|
addresses << Address.new(
:street => "Wienerstr. #{n}",
:city => "Berlin",
:post_code => "10999",
:priority => n
)
end
person.addresses.push(addresses)
end
end
bm.report("#each ") do
person.addresses.each do |address|
address.street
end
end
bm.report("#each_desc_priority ") do
person.addresses.desc(:priority).each do |address|
address.street
end
end
bm.report("#each_asc_priority ") do
person.addresses.asc(:priority).each do |address|
address.street
end
end
address = person.addresses.last
bm.report("#find ") do
person.addresses.find(address.id)
end
person.addresses.delete_all
end
end
GC.start
puts
puts
puts "\n[ Referenced 1-n Benchmarks ]"
times.each do |i|
Mongoid.unit_of_work do
puts "[ #{i} ]"
bm.report("#create ") do
i.times do |n|
person.posts.create(:title => "Posting #{n}", :priority => n)
end
end
bm.report("#first ") do
post = person.posts.first
end
bm.report("#first_desc_priority") do
person.posts.desc(:priority).first
end
bm.report("#first_asc_priority ") do
person.posts.asc(:priority).first
end
bm.report("#count ") do
person.posts.count
end
person.posts.delete_all
Post.delete_all
GC.start
bm.report("#push (batch) ") do
[].tap do |posts|
i.times do |n|
posts << Post.new(:title => "Posting #{n}", :priority => n)
end
person.posts.push(posts)
end
end
bm.report("#each ") do
person.posts.each do |post|
post.title
end
end
bm.report("#each_desc_priority ") do
person.posts.desc(:priority).each do |post|
post.title
end
end
bm.report("#each_asc_priority ") do
person.posts.asc(:priority).each do |post|
post.title
end
end
post = person.posts.last
bm.report("#find ") do
person.posts.find(post.id)
end
Post.delete_all
GC.start
end
end
end
Mongoid.master.connection.drop_database("embedded_referenced_development")
class Person
include Mongoid::Document
field :birth_date, :type => Date
field :title, :type => String
embeds_many :addresses
has_many :posts
end
class Address
include Mongoid::Document
field :street, :type => String
field :city, :type => String
field :state, :type => String
field :post_code, :type => String
field :address_type, :type => String
field :priority, :type => Integer
embedded_in :person
end
class Post
include Mongoid::Document
field :title, :type => String
field :content, :type => String
field :priority, :type => Integer
belongs_to :person
index :person_id
index :priority
end
[ Embedded 1-n Benchmarks ]
[ 100 ]
#create 0.110000 0.000000 0.110000 ( 0.124073)
#first 0.000000 0.000000 0.000000 ( 0.000022)
#first_desc_priority 0.000000 0.000000 0.000000 ( 0.002232)
#first_asc_priority 0.020000 0.000000 0.020000 ( 0.019471)
#count 0.000000 0.000000 0.000000 ( 0.000082)
#push (batch) 0.050000 0.000000 0.050000 ( 0.048295)
#each 0.000000 0.000000 0.000000 ( 0.000202)
#each_desc_priority 0.000000 0.000000 0.000000 ( 0.002253)
#each_asc_priority 0.000000 0.000000 0.000000 ( 0.002124)
#find 0.000000 0.000000 0.000000 ( 0.001040)
[ 1000 ]
#create 0.770000 0.030000 0.800000 ( 0.789943)
#first 0.000000 0.000000 0.000000 ( 0.000015)
#first_desc_priority 0.020000 0.000000 0.020000 ( 0.022359)
#first_asc_priority 0.050000 0.000000 0.050000 ( 0.046777)
#count 0.000000 0.000000 0.000000 ( 0.000798)
#push (batch) 0.560000 0.000000 0.560000 ( 0.558600)
#each 0.000000 0.000000 0.000000 ( 0.002222)
#each_desc_priority 0.020000 0.000000 0.020000 ( 0.023145)
#each_asc_priority 0.030000 0.000000 0.030000 ( 0.022075)
#find 0.010000 0.000000 0.010000 ( 0.008490)
[ 10000 ]
#create 9.330000 0.330000 9.660000 ( 9.844733)
#first 0.000000 0.000000 0.000000 ( 0.000017)
#first_desc_priority 0.350000 0.010000 0.360000 ( 0.357919)
#first_asc_priority 0.470000 0.000000 0.470000 ( 0.494384)
#count 0.010000 0.000000 0.010000 ( 0.008637)
#push (batch) 6.020000 0.010000 6.030000 ( 6.033543)
#each 0.020000 0.000000 0.020000 ( 0.021635)
#each_desc_priority 0.330000 0.000000 0.330000 ( 0.330451)
#each_asc_priority 0.320000 0.000000 0.320000 ( 0.322409)
#find 0.090000 0.000000 0.090000 ( 0.084899)
[ 100000 ]
#create 145.370000 3.030000 148.400000 (1009.560006)
#first 0.000000 0.000000 0.000000 ( 0.000020)
#first_desc_priority 13.270000 0.100000 13.370000 ( 14.392949)
#first_asc_priority 14.110000 0.080000 14.190000 ( 15.748069)
#count 0.080000 0.000000 0.080000 ( 0.088599)
#push (batch) 121.490000 0.250000 121.740000 (122.224806)
#each 0.220000 0.000000 0.220000 ( 0.221331)
#each_desc_priority 8.560000 0.040000 8.600000 ( 8.621195)
#each_asc_priority 7.880000 0.010000 7.890000 ( 7.906480)
#find 1.530000 0.010000 1.540000 ( 1.539526)
[ Referenced 1-n Benchmarks ]
[ 100 ]
#create 0.070000 0.010000 0.080000 ( 0.086389)
#first 0.000000 0.000000 0.000000 ( 0.000018)
#first_desc_priority 0.000000 0.000000 0.000000 ( 0.001489)
#first_asc_priority 0.000000 0.000000 0.000000 ( 0.000823)
#count 0.000000 0.000000 0.000000 ( 0.000880)
#push (batch) 0.040000 0.000000 0.040000 ( 0.044277)
#each 0.000000 0.000000 0.000000 ( 0.000526)
#each_desc_priority 0.010000 0.000000 0.010000 ( 0.011827)
#each_asc_priority 0.010000 0.000000 0.010000 ( 0.008895)
#find 0.000000 0.000000 0.000000 ( 0.000902)
[ 1000 ]
#create 0.530000 0.020000 0.550000 ( 0.545588)
#first 0.000000 0.000000 0.000000 ( 0.000015)
#first_desc_priority 0.000000 0.000000 0.000000 ( 0.001001)
#first_asc_priority 0.000000 0.000000 0.000000 ( 0.000702)
#count 0.000000 0.000000 0.000000 ( 0.002231)
#push (batch) 0.420000 0.000000 0.420000 ( 0.424894)
#each 0.010000 0.000000 0.010000 ( 0.004960)
#each_desc_priority 0.060000 0.010000 0.070000 ( 0.099458)
#each_asc_priority 0.180000 0.020000 0.200000 ( 0.192820)
#find 0.000000 0.000000 0.000000 ( 0.001092)
[ 10000 ]
#create 6.390000 0.150000 6.540000 ( 6.580534)
#first 0.000000 0.000000 0.000000 ( 0.000014)
#first_desc_priority 0.000000 0.000000 0.000000 ( 0.001049)
#first_asc_priority 0.000000 0.000000 0.000000 ( 0.000825)
#count 0.000000 0.000000 0.000000 ( 0.016329)
#push (batch) 5.180000 0.010000 5.190000 ( 5.192524)
#each 0.050000 0.000000 0.050000 ( 0.059217)
#each_desc_priority 1.050000 0.130000 1.180000 ( 1.566796)
#each_asc_priority 1.260000 0.130000 1.390000 ( 1.413076)
#find 0.000000 0.000000 0.000000 ( 0.001061)
[ 100000 ]
#create 87.720000 1.430000 89.150000 ( 89.439757)
#first 0.000000 0.000000 0.000000 ( 0.000014)
#first_desc_priority 0.000000 0.000000 0.000000 ( 0.001168)
#first_asc_priority 0.000000 0.000000 0.000000 ( 0.000911)
#count 0.000000 0.000000 0.000000 ( 0.158089)
#push (batch) 95.010000 0.340000 95.350000 ( 95.483494)
#each 0.520000 0.000000 0.520000 ( 0.547517)
#each_desc_priority 33.510000 1.240000 34.750000 ( 38.717533)
#each_asc_priority 33.740000 1.200000 34.940000 ( 35.274610)
#find 0.000000 0.000000 0.000000 ( 0.001255)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment