Skip to content

Instantly share code, notes, and snippets.

@devdazed
Created January 14, 2011 02:07
Show Gist options
  • Save devdazed/779031 to your computer and use it in GitHub Desktop.
Save devdazed/779031 to your computer and use it in GitHub Desktop.
Testing increment, speed in Mongo and Redis
require 'redis'
require 'mongo'
require 'benchmark'
class MongoVsRedis
ITERATIONS = 100_000
MONGO_CRITERIA = {"_id"=>BSON::ObjectId.from_string("4d2fa98a858d68644662156d")}
REDIS_CRITERIA = "benchmark_test"
def initialize
@mongo = Mongo::Connection.new.db('test').collection('incr_test')
@redis = Redis.new
@mongo.update(MONGO_CRITERIA, {"$set"=>{"test"=>0}}, {:upsert=>true})
@redis.set(REDIS_CRITERIA, 0)
end
def run
puts "REDIS: #{ITERATIONS/redis_increments.real} increments per second"
puts "MONGO: #{ITERATIONS/mongo_increments.real} increments per second"
end
def measure(&block)
Benchmark.measure do
ITERATIONS.times { block.call }
end
end
def mongo_increments
measure{ @mongo.update(MONGO_CRITERIA, {"$inc"=>{"test"=>1}}) }
end
def redis_increments
measure{ @redis.incr REDIS_CRITERIA}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment