wrk2 icon indicating copy to clipboard operation
wrk2 copied to clipboard

Unable to share tables between threads

Open fhalde opened this issue 5 years ago • 2 comments

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 avatar Feb 14 '20 20:02 fhalde

@giltene are you aware of this?

fhalde avatar Feb 19 '20 19:02 fhalde

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

nivekastoreth avatar May 25 '20 16:05 nivekastoreth