Skip to content

Instantly share code, notes, and snippets.

@catwell
Created October 18, 2011 20:11
Show Gist options
  • Select an option

  • Save catwell/1296564 to your computer and use it in GitHub Desktop.

Select an option

Save catwell/1296564 to your computer and use it in GitHub Desktop.

Revisions

  1. catwell created this gist Oct 18, 2011.
    119 changes: 119 additions & 0 deletions bench.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,119 @@
    require "redis"
    local WMAX = 100000

    local rc = Redis.connect("unix:///tmp/redis.sock")

    local rs_mhset = [[
    local keys,argv,rcall = KEYS,ARGV,redis.call
    local n,f = 0,argv[#keys+1]
    for i,k in ipairs(keys) do n = n + rcall("hset",k,f,argv[i]) end
    return n
    ]]

    local rs_hmset = [[
    local keys,argv,rcall = KEYS,ARGV,redis.call
    local n,k = 0,keys[1]
    for i=0,#argv/2-1 do
    n = n + rcall("hset",k,argv[2*i+1],argv[2*i+2])
    end
    return n
    ]]

    local rs_hmset2 = [[
    local keys,argv,rcall = KEYS,ARGV,redis.call
    local k = keys[1]
    return rcall("hmset",k,unpack(argv))
    ]]

    local random_doc = function(n)
    local r = {}
    local w = math.random(1,WMAX)
    for i=1,n do
    while (r[w] ~= nil) do w = math.random(1,WMAX) end
    r[w] = math.random(1,255)
    end
    return r
    end

    local rk = function(x,i) return string.format("%s:%d",x,i) end

    local h2redis = function(doc)
    local t = {}
    for w,f in pairs(doc) do
    t[#t+1] = w
    t[#t+1] = f
    end
    return t
    end

    local mhset_pipeline = function(doc)
    local d_id = rc:incr("d:next")
    rc:pipeline(function(p)
    for w,f in pairs(doc) do
    p:hset(rk("w",w),d_id,f)
    end
    end)
    end

    local mhset_script = function(doc)
    local d_id = rc:incr("d:next")
    local t = {}
    for w,_ in pairs(doc) do t[#t+1] = rk("w",w) end
    local n = #t
    for _,f in pairs(doc) do t[#t+1] = f end
    t[#t+1] = d_id
    rc:eval(rs_mhset,n,unpack(t))
    end

    local hmset_multi = function(doc)
    local d_id = rc:incr("d:next")
    rc:hmset(rk("d",d_id),unpack(h2redis(doc)))
    end

    local hmset_pipeline = function(doc)
    local d_id = rc:incr("d:next")
    local d_key = rk("d",d_id)
    rc:pipeline(function(p)
    for w,f in pairs(doc) do
    p:hset(d_key,w,f)
    end
    end)
    end

    local hmset_script1 = function(doc)
    local d_id = rc:incr("d:next")
    rc:eval(rs_hmset,1,rk("d",d_id),unpack(h2redis(doc)))
    end

    local hmset_script2 = function(doc)
    local d_id = rc:incr("d:next")
    rc:eval(rs_hmset2,1,rk("d",d_id),unpack(h2redis(doc)))
    end

    local mydoc = random_doc(1000)

    local t0,t1,t2,t3,t4
    print("MHSET (pipeline, script)")
    for j=1,5 do
    t0 = os.clock()
    for i=1,100 do mhset_pipeline(mydoc) end
    t1 = os.clock()
    for i=1,100 do mhset_script(mydoc) end
    t2 = os.clock()
    print(t1-t0,t2-t1)
    end
    print()

    print("HMSET (multi, pipeline, script1, script2)")
    for j=1,5 do
    t0 = os.clock()
    for i=1,100 do hmset_multi(mydoc) end
    t1 = os.clock()
    for i=1,100 do hmset_pipeline(mydoc) end
    t2 = os.clock()
    for i=1,100 do hmset_script1(mydoc) end
    t3 = os.clock()
    for i=1,100 do hmset_script2(mydoc) end
    t4 = os.clock()
    print(t1-t0,t2-t1,t3-t2,t4-t3)
    end
    13 changes: 13 additions & 0 deletions results.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    MHSET (pipeline, script)
    2.49 0.37
    1.64 0.85
    3.16 0.86
    1.69 0.37
    1.44 0.37

    HMSET (multi, pipeline, script1, script2)
    0.43 1.45 0.37 0.38
    0.37 1.45 0.37 0.39
    0.49 2.67 0.86 0.87
    0.73 3.07 0.84 0.42
    0.47 2.59 0.87 0.59