Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save solnic/275081 to your computer and use it in GitHub Desktop.
Save solnic/275081 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "rubygems"
require "benchmark"
# DM Adapter
require "dm-core"
require "dm-timestamps"
require "dm-pager"
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'mongo_adapter'))
DataMapper.setup(:default, :adapter => 'mongo', :database => 'benchmarking')
class DmFoo
include DataMapper::Mongo::Resource
property :id, ObjectID
property :approved, Boolean
property :count, Integer
property :approved_at, Time
property :expire_on, Date
property :created_at, Time
property :updated_at, Time
end
DmFoo.repository.adapter.send(:database).drop_collection('dm_foos')
Benchmark.bm(5) do |x|
puts('DataMapper Mongo Adapter')
ids = []
x.report("write") do
1000.times { |i| ids << DmFoo.create(:count => 0, :approved => true, :approved_at => Time.now, :expire_on => Date.today).id }
end
x.report("read ") do
ids.each { |id| DmFoo.get(id) }
end
x.report("all ") do
DmFoo.all(:approved => true).each do |f|
f.id
end
end
end
# ~ DM Adapter
# MongoMapper
require 'mongo_mapper'
MongoMapper.database = 'benchmarking'
class Foo
include MongoMapper::Document
key :approved, Boolean
key :count, Integer
key :approved_at, Time
key :expire_on, Date
timestamps!
end
Foo.collection.remove
Benchmark.bm(5) do |x|
puts('MongoMapper')
ids = []
x.report("write") do
1000.times { |i| ids << Foo.create(:count => 0, :approved => true, :approved_at => Time.now, :expire_on => Date.today).id }
end
x.report("read ") do
ids.each { |id| Foo.first(:id => id) }
end
x.report("all ") do
Foo.all(:approved => true).each do |f|
f.id
end
end
end
# ~ MongoMapper
# Mongoid
require 'mongoid'
connection = Mongo::Connection.new('localhost')
Mongoid.database = connection.db('benchmarking')
class Bar
include Mongoid::Document
include Mongoid::Timestamps
field :approved, :type => Boolean
field :count, :type => Integer
field :approved_at, :type => Time
field :expire_on, :type => Date
end
Bar.collection.remove
Benchmark.bm(5) do |x|
puts 'MongoId'
ids = []
x.report("write") do
1000.times { |i| ids << Bar.create(:count => 0, :approved => true, :approved_at => Time.now, :expire_on => Date.today).id }
end
x.report("read ") do
ids.each { |id| Bar.find(:first, :conditions => {:id => id}) }
end
x.report("all ") do
Bar.find(:conditions => {:approved => true}).each do |f|
f.id
end
end
end
# ~ Mongoid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment