Benchmark code refactoring.

This commit is contained in:
Jim Menard 2009-02-10 15:12:36 -05:00
parent 22d4ba8daa
commit 4f7fc8a112

View File

@ -14,6 +14,14 @@ def report(str, t0, t1, n)
printf("%16s: %03.8f\n", str, dt) printf("%16s: %03.8f\n", str, dt)
end end
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
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
@ -23,24 +31,19 @@ coll = db.collection('benchmark')
coll.create_index('foo', 'i') coll.create_index('foo', 'i')
t0 = Time.new # Call to db.error forces inserts to finish
N.times { |i| coll.insert('i' => i) } benchmark('insert', N, db, Proc.new{db.error}) { |coll, i|
db.error # forces pause until all finished coll.insert('i' => i)
report('insert', t0, Time.new, N) }
benchmark('find_first', N, db) { |coll, i|
t0 = Time.new coll.find_first
N.times { coll.find_first } }
report('find_first', t0, Time.new, N) benchmark('find', N, db) { |coll, i|
t0 = Time.new
N.times {
coll.find('i' => 3).each { } coll.find('i' => 3).each { }
coll.find('i' => 234).each { } coll.find('i' => 234).each { }
coll.find('i' => 9876).each { } coll.find('i' => 9876).each { }
} }
report('find', t0, Time.new, N * 3) benchmark('find gt/lt', N, db) { |coll, i|
h = {'i' => {'$gt' => 200, '$lt' => 200}}
h = {'i' => {'$gt' => 200, '$lt' => 200}} coll.find(h).each {}
t0 = Time.new }
N.times { coll.find(h).each { } }
report('find gt/lt', t0, Time.new, N)