Skip to content

Instantly share code, notes, and snippets.

@siong1987
Created December 3, 2011 03:17
Show Gist options
  • Save siong1987/1425887 to your computer and use it in GitHub Desktop.
Save siong1987/1425887 to your computer and use it in GitHub Desktop.
redis ORMs/mongo ORM benchmarks
rvm 1.9.2@ohm
source 'http://rubygems.org'
gem 'ohm'
gem 'redis'
gem 'yajl-ruby'
gem 'mongoid'
gem 'bson_ext'
GEM
remote: http://rubygems.org/
specs:
activemodel (3.1.3)
activesupport (= 3.1.3)
builder (~> 3.0.0)
i18n (~> 0.6)
activesupport (3.1.3)
multi_json (~> 1.0)
bson (1.5.1)
bson_ext (1.5.1)
builder (3.0.0)
i18n (0.6.0)
mongo (1.5.1)
bson (= 1.5.1)
mongoid (2.3.4)
activemodel (~> 3.1)
mongo (~> 1.3)
tzinfo (~> 0.3.22)
multi_json (1.0.4)
nest (1.1.0)
redis (~> 2.1)
ohm (0.1.3)
nest (~> 1.0)
redis (2.2.2)
tzinfo (0.3.31)
yajl-ruby (1.1.0)
PLATFORMS
ruby
DEPENDENCIES
bson_ext
mongoid
ohm
redis
yajl-ruby
require 'benchmark'
require 'ohm'
require 'redis'
require 'yajl/json_gem'
require 'mongoid'
redis = Redis.new
Ohm.connect
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db("testings")
end
Mongoid.master.collections.select do |collection|
collection.name !~ /system/
end.each(&:drop)
class OhmUser < Ohm::Model
attribute :username
attribute :email
end
class MongoidUser
include Mongoid::Document
field :username, :type => String
field :email, :type => String
end
n = 500
Benchmark.bm do |x|
data = {:username => "ian", :email => 'ian@ruby-code.com'}
x.report("Ohm creates") {
Ohm.redis.flushdb
n.times do
OhmUser.create(data).save
end
}
x.report("raw redis creates, with json field") {
n.times do
redis.set("ian", data.to_json)
end
}
x.report("Mongoid creates") {
n.times do
MongoidUser.new(data).save
end
}
x.report("Ohm reads") {
n.times do
OhmUser.all
end
}
x.report("raw redis reads") {
n.times do
JSON.parse(redis.get("ian"))
end
}
x.report("Mongoid reads") {
n.times do
MongoidUser.all.map(&:_id)
end
}
end
~ $ ruby redis-vs-mongo.rb
user system total real
Ohm creates 0.370000 0.110000 0.480000 ( 0.716614)
raw redis creates, with json field 0.060000 0.010000 0.070000 ( 0.085520)
Mongoid creates 0.170000 0.010000 0.180000 ( 1.532263)
Ohm reads 0.010000 0.000000 0.010000 ( 0.013157)
raw redis reads 0.020000 0.000000 0.020000 ( 0.052765)
Mongoid reads 10.890000 1.050000 11.940000 ( 12.718300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment