#!/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 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') t0 = Time.new N.times { |i| coll.insert('i' => i) } db.error # forces pause until all finished t1 = Time.new report('insert', t0, t1, N) t0 = Time.new N.times { coll.find_first } t1 = Time.new report('find_first', t0, t1, N) t0 = Time.new N.times { coll.find('i' => 3).each { } coll.find('i' => 234).each { } coll.find('i' => 9876).each { } } t1 = Time.new report('find', t0, t1, N * 3) h = {'i' => {'$gt' => 200, '$lt' => 200}} t0 = Time.new N.times { coll.find(h).each { } } t1 = Time.new report('find gt/lt', t0, t1, N)