Skip to content

Instantly share code, notes, and snippets.

@chuckremes
Created April 19, 2010 18:19
Show Gist options
  • Select an option

  • Save chuckremes/371389 to your computer and use it in GitHub Desktop.

Select an option

Save chuckremes/371389 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'benchmark'
#require 'md5'
require 'json/ext'
require 'mongo'
require 'bson'
require 'em-mongo'
HASH = {'one' => nil, 'two' => nil, 'three' => nil, 'four' => {'a' => nil, 'b' => nil, 'c' =>nil},
'five' => {'d' => nil, 'e' => nil},
'six' => {'f' => nil, 'g' => nil, 'h' =>nil, 'i' => nil, 'j' => nil, 'k' => nil, 'l' => nil},
'seven' => nil, 'eight' => nil,
'nine' => {'m' => {'A' => nil, 'B' => nil}}}
def random_string
Digest::MD5.hexdigest "#{Time.now + rand(999)}"
end
def random_number
rand 999_999_999
end
def random_float
random_number + rand
end
def randomize_entries hsh
hsh.each_pair do |key, value|
case value
when NilClass
hsh[key] = send METHODS[rand(3)]
# puts "key [#{key}] value [#{hsh[key]}]"
when Hash
# puts "randomize! key [#{key}] value [#{value.inspect}]"
randomize_entries value
end
end
end
METHODS = [:random_string, :random_number, :random_float]
org_array = []
generated_array = []
one = []
COUNT = 40_000
0.upto(COUNT) do |i|
hsh = HASH.dup
randomize_entries hsh
org_array << hsh
end
#Benchmark.bmbm(40) do |x|
Benchmark.bm(40) do |x|
x.report("mongo-ruby-driver, insert") do
mrd_connection = Mongo::Connection.new.db 'benchmark'
mrd_collection = mrd_connection.collection 'mrd'
org_array.each do |element|
mrd_collection.insert element
end
mrd_connection.last_status
mrd_collection.remove
end
x.report("em-mongo driver, insert") do
EM.run do
db = EM::Mongo::Connection.new.db 'benchmark'
collection = db.collection 'em'
i = 0
callback = proc {
element = org_array[i]
i += 1
collection.insert element
if i < COUNT
EM.next_tick callback
else
collection.remove
EM.stop
end
}
EM.next_tick callback
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment