Skip to content

Instantly share code, notes, and snippets.

@catwell
Created May 2, 2011 09:24
Show Gist options
  • Save catwell/951352 to your computer and use it in GitHub Desktop.
Save catwell/951352 to your computer and use it in GitHub Desktop.
Redis scripting MHSET benchmark
require "rubygems"
require "redis"
def time(x)
t0 = Time.now
yield
puts "#{x} - #{1000 * (Time.now - t0)}"
end
L = 1.upto(10000.to_i).to_a
R = Redis.new(path: "/tmp/redis.sock",thread_safe: false)
LUA_MHSET_SRC = <<-EOS
n = #KEYS
f = ARGV[n+1]
for i,k in ipairs(KEYS) do
redis("hset",k,f,ARGV[i])
end
return n
EOS
def lua_mhset(field,keys,values)
R.eval(LUA_MHSET_SRC,keys,values+[field])
end
R.flushall; sleep 2
time("naive"){ L.each{|i| R.hset(i,1,i)} }
R.flushall; sleep 2
time("pipeline"){ R.multi{L.each{|i| R.hset(i,1,i)}} }
R.flushall; sleep 2
time("lua"){ lua_mhset(1,L,L) }
# RESULTS:
# naive - 646.516
# pipeline - 226.891
# lua - 81.239
@catwell
Copy link
Author

catwell commented May 2, 2011

Note: "eval" here does not use method_missing, I rewrote it as:

def eval(source, keys, rest)
  synchronize do
    @client.call(:eval, source, keys.size, *(keys+rest))
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment