Browse files

First commit with Mongo ruby driver, MongoMapper and Mongoid

  • Loading branch information...
0 parents commit e83b05ebf74690332d93e0f6776c92b135373a6b Joseph HALTER committed Jul 24, 2010
Showing with 68 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +6 −0 Gemfile
  3. +59 −0 benchmark.rb
3 .gitignore
@@ -0,0 +1,3 @@
+.bundle
+.DS_Store
+Gemfile.lock
6 Gemfile
@@ -0,0 +1,6 @@
+source "http://rubygems.org"
+
+gem "bson_ext"
+gem "mongo"
+gem "mongo_mapper"
+gem "mongoid"
59 benchmark.rb
@@ -0,0 +1,59 @@
+require "benchmark"
+require "bundler"
+Bundler.require :default
+
+puts "Cleaning old data..."
+db_name = "mongobench"
+connection = Mongo::Connection.new
+db = connection.db(db_name)
+db.collections.each{|c| c.remove}
+db[:mongo_driver].create_index(:a)
+
+n = 1000
+
+puts "\n*** Mongo ruby driver ***"
+Benchmark.bm(25) do |x|
+ x.report("#{n} inserts:"){n.times{|i| db[:mongo_driver].insert(:a => i, :b => n-i)}}
+ x.report("#{n} lookups:"){n.times{|i| db[:mongo_driver].find_one(:a => i)}}
+ x.report("#{n/25} select alls:"){(n/25).times{|i| db[:mongo_driver].find.to_a}}
+ x.report("#{n} updates:"){n.times{|i| db[:mongo_driver].update({:a => i}, {'$set' => {:b => i}})}}
+ x.report("#{n} deletes:"){n.times{|i| db[:mongo_driver].remove(:a => i)}}
+end
+
+MongoMapper.connection = connection
+MongoMapper.database = db_name
+class MongoMapperObject
+ include MongoMapper::Document
+ key :a, Integer, :index => true
+ key :b, Integer
+end
+
+puts "\n*** MongoMapper ***"
+Benchmark.bm(25) do |x|
+ x.report("#{n} inserts:"){n.times{|i| MongoMapperObject.create!(:a => i, :b => n-i)}}
+ x.report("#{n} lookups:"){n.times{|i| MongoMapperObject.first(:a => i)}}
+ x.report("#{n/25} select alls:"){(n/25).times{|i| MongoMapperObject.all.to_a}}
+ x.report("#{n} updates:"){n.times{|i| MongoMapperObject.first(:a => i).update_attributes(:b => i)}}
+ x.report("#{n} deletes:"){n.times{|i| MongoMapperObject.first(:a => i).destroy}}
+end
+
+puts "\n*** Mongoid ***"
+
+Mongoid.configure do |config|
+ config.master = db
+end
+class MongoidObject
+ include Mongoid::Document
+ field :a, :type => Integer
+ field :b, :type => Integer
+ index :a
+end
+
+puts "\n*** MongoMapper ***"
+Benchmark.bm(25) do |x|
+ x.report("#{n} inserts:"){n.times{|i| MongoidObject.create!(:a => i, :b => n-i)}}
+ x.report("#{n} lookups:"){n.times{|i| MongoidObject.first(:conditions => {:a => i})}}
+ x.report("#{n/25} select alls:"){(n/25).times{|i| MongoidObject.all.to_a}}
+ x.report("#{n} updates:"){n.times{|i| MongoidObject.first(:conditions => {:a => i}).update_attributes(:b => i)}}
+ x.report("#{n} deletes:"){n.times{|i| MongoidObject.first(:conditions => {:a => i}).destroy}}
+end

0 comments on commit e83b05e

Please sign in to comment.