Created
July 24, 2014 00:13
-
-
Save justincampbell/eeb92e948f886022777a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
require 'redis' | |
class Redis | |
def del(*keys) | |
synchronize do |client| | |
client.call([:del] + keys) | |
end | |
end | |
def del_with_check(*keys) | |
return 0 if keys == [[]] | |
synchronize do |client| | |
client.call([:del] + keys) | |
end | |
end | |
def del_with_rescue(*keys) | |
synchronize do |client| | |
client.call([:del] + keys) | |
end | |
rescue Redis::CommandError | |
return 0 if keys == [[]] | |
raise | |
end | |
end | |
COUNT = (ENV['COUNT'] || 100_000).to_i | |
RUNS = (ENV['RUNS'] || 5).to_i | |
def client | |
@client ||= Redis.new(db: 15) | |
end | |
def keys | |
@keys ||= COUNT.times.map { |n| "key#{n}" } | |
end | |
def prepare | |
client.flushdb | |
client.pipelined do | |
keys.each do |key| | |
client.set key, key | |
end | |
end | |
GC.start | |
end | |
Benchmark.bm 15 do |x| | |
RUNS.times do | |
prepare | |
x.report("del") do | |
keys.each { |key| client.del(key) } | |
end | |
prepare | |
x.report("del_with_check") do | |
keys.each { |key| client.del_with_check(key) } | |
end | |
prepare | |
x.report("del_with_rescue") do | |
keys.each { |key| client.del_with_rescue(key) } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment