2009-02-10 19:17:55 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
#
|
|
|
|
# Note: Ruby 1.9 is faster than 1.8, as expected.
|
|
|
|
|
|
|
|
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
|
|
|
require 'mongo'
|
|
|
|
|
|
|
|
include XGen::Mongo::Driver
|
|
|
|
|
|
|
|
N = 30_000
|
|
|
|
|
|
|
|
def report(str, t0, t1, n)
|
|
|
|
dt = t1.to_f - t0.to_f
|
|
|
|
printf("%16s: %03.8f\n", str, dt)
|
|
|
|
end
|
|
|
|
|
2009-02-10 20:12:36 +00:00
|
|
|
def benchmark(str, n, db, after_proc=nil)
|
|
|
|
coll = db.collection('benchmark')
|
|
|
|
t0 = Time.new
|
|
|
|
n.times { |i| yield coll, i }
|
|
|
|
after_proc.call if after_proc
|
|
|
|
report(str, t0, Time.new, n)
|
|
|
|
end
|
|
|
|
|
2009-02-10 19:17:55 +00:00
|
|
|
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
|
|
|
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
|
|
|
|
|
|
|
|
db = Mongo.new(host, port).db('ruby-benchmark')
|
|
|
|
db.drop_collection('benchmark')
|
|
|
|
coll = db.collection('benchmark')
|
|
|
|
|
|
|
|
coll.create_index('foo', 'i')
|
|
|
|
|
2009-02-10 20:12:36 +00:00
|
|
|
# Call to db.error forces inserts to finish
|
|
|
|
benchmark('insert', N, db, Proc.new{db.error}) { |coll, i|
|
|
|
|
coll.insert('i' => i)
|
|
|
|
}
|
|
|
|
benchmark('find_first', N, db) { |coll, i|
|
|
|
|
coll.find_first
|
|
|
|
}
|
|
|
|
benchmark('find', N, db) { |coll, i|
|
2009-02-10 19:17:55 +00:00
|
|
|
coll.find('i' => 3).each { }
|
|
|
|
coll.find('i' => 234).each { }
|
|
|
|
coll.find('i' => 9876).each { }
|
|
|
|
}
|
2009-02-10 20:12:36 +00:00
|
|
|
benchmark('find gt/lt', N, db) { |coll, i|
|
|
|
|
h = {'i' => {'$gt' => 200, '$lt' => 200}}
|
|
|
|
coll.find(h).each {}
|
|
|
|
}
|