giltene / wrk2

A constant throughput, correct latency recording variant of wrk
Apache License 2.0
4.28k stars 395 forks source link

Unable to share tables between threads #91

Open fhalde opened 4 years ago

fhalde commented 4 years ago

I have the following script

function read_lines(file)
    lines = {}
    for line in io.lines(file) do
        table.insert(lines, line);
    end
    return lines
end

requests = nil
function setup(t)
    if not requests then
        requests = read_lines("f.txt")
    end
        print(#requests)
    t:set('r', requests)
end

function init(args)
    print(#r)
end

prints

3000
0

which basically means the table that I am setting t:set('r', requests) is not being shared or accessible. This is working correctly inwrk

fhalde commented 4 years ago

@giltene are you aware of this?

nivekastoreth commented 4 years ago

I ran into what may be a similar issue where when I view a lua table from a different thread context, the keys and values are swapped.

local counter = 0
local threads = {}

function setup(thread)
  thread:set("id", counter)
  table.insert(threads, thread)
  counter = counter + 1
end

function init(args)
  uniq = 0
  codes={left="right"}
end

function increment(t,k,v)
  t[k]=(t[k] or 0)+(v or 1)
end

function response(status, headers, body)
  increment(codes, status)
end

function status_pairs(tbl, flip)
  local lst={}
  for k, v in pairs(tbl) do
    if flip then k, v = v, k end
    if k ~= "left" then 
      table.insert(lst, {k, v}) 
    end
  end
  table.sort(lst, function(l,r) return l[1] < r[1] end)

  local i, n = 0, table.getn(lst)
  return function()
    i = i + 1
    if i <= n then 
      return lst[i][1], lst[i][2]
    end
  end
end

function done(summary, latency, requests)
  local results={}
  for _, thread in ipairs(threads) do
    local codes = thread:get("codes")

    -- i have no idea why (or even how) wrk2 flips this table
    -- but it does (but wrk doesn't), so we check for to see 
    -- if it's flipped when iterating over status code/count pairs
    local flip = codes["left"] ~= "right"

    local status = string.format("Thread Codes %-2s =>", thread:get("id"))
    for k, v in status_pairs(codes, flip) do
      status = string.format("%s %s:%s", status, k, v)
      increment(results, k, v)
    end
    io.write(status .. "\n")
  end
  io.write("\n")
end