Skip to content

Instantly share code, notes, and snippets.

@koko1000ban
Created December 11, 2012 08:55
Show Gist options
  • Save koko1000ban/4257128 to your computer and use it in GitHub Desktop.
Save koko1000ban/4257128 to your computer and use it in GitHub Desktop.
benchmark memcached vs redis
# -*- coding: utf-8 -*-
require 'benchmark'
require "securerandom"
namespace :dev do
desc "benchmark mem vs redis"
task :benchmark => :environment do
dev_server = '172.19.60.176'
n = 10000
stores = {
:dalli_store => ["#{dev_server}:11211", {:namespace => 'dev', :compress => false }],
:redis_store => ["redis://#{dev_server}:6379/0/cache"]
}
actions = {
:hit => lambda{
Rails.cache.fetch("test")
},
:miss => lambda{
Rails.cache.read("test_" + SecureRandom.hex(10))
}
}
stores.each do |store, args|
ActionController::Base.cache_store = store, *args
Rails.cache.delete('test')
Rails.cache.fetch('test'){ SecureRandom.hex(30) }
Benchmark.bm(30) do |x|
actions.each do |label, proc|
x.report("cache_store:#{store}:#{label}") do
n.times do
proc.call
end
end
x.report("cache_store:#{store}:delete") do
n.times do |i|
Rails.cache.write("test_#{i}", i, :expires_in => 2)
end
n.times do |i|
Rails.cache.delete("test_#{i}", i)
end
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment