wrk — 小巧輕盈的 http 性能測試工具(3)

發表于:2016-05-31來源:翱翔 Beta作者:欣雨小筑點擊數: 標簽:性能測試工具
function response(status, headers, body) if status ~= 200 then print(body) wrk.thread:stop() end end done函數在所有請求執行完以后調用, 一般用于自定義統計結果. done = function(

  function response(status, headers, body)

  if status ~= 200 then

  print(body)

  wrk.thread:stop()

  end

  end

  done函數在所有請求執行完以后調用, 一般用于自定義統計結果.

  done = function(summary, latency, requests)

  io.write("------------------------------\n")

  for _, p in pairs({ 50, 90, 99, 99.999 }) do

  n = latency:percentile(p)

  io.write(string.format("%g%%,%d\n", p, n))

  end

  end

  下面是 wrk 源代碼中給出的完整例子:

  localcounter = 1

  localthreads = {}

  function setup(thread)

  thread:set("id", counter)

  table.insert(threads, thread)

  counter = counter + 1

  end

  function init(args)

  requests = 0

  responses = 0

  localmsg = "thread %d created"

  print(msg:format(id))

  end

  function request()

  requests = requests + 1

  return wrk.request()

  end

  function response(status, headers, body)

  responses = responses + 1

  end

  function done(summary, latency, requests)

  for index, threadin ipairs(threads) do

  localid = thread:get("id")

  localrequests = thread:get("requests")

  localresponses = thread:get("responses")

  localmsg = "thread %d made %d requests and got %d responses"

  print(msg:format(id, requests, responses))

  end

  end

  測試復合場景時, 也可以通過 lua 實現訪問多個 url.例如這個復雜的 lua 腳本, 隨機讀取 paths.txt 文件中的 url 列表, 然后訪問.:

  counter = 1

  math.randomseed(os.time())

  math.random(); math.random(); math.random()

  function file_exists(file)

  local f = io.open(file, "rb")

  if f then f:close() end

  return f ~= nil

  end

  function shuffle(paths)

  local j, k

  local n = #paths

  for i = 1, n do

  j, k = math.random(n), math.random(n)

  paths[j], paths[k] = paths[k], paths[j]

  end

  return paths

  end

  function non_empty_lines_from(file)

  if not file_exists(file) then return {} end

  lines = {}

  for linein io.lines(file) do

  if not (line == '') then

  lines[#lines + 1] = line

  end

  end

  return shuffle(lines)

  end

  paths = non_empty_lines_from("paths.txt")

  if #paths <= 0 then

  print("multiplepaths: No paths found. You have to create a file paths.txt with one path per line")

  os.exit()

  end

  print("multiplepaths: Found " .. #paths .. " paths")

  request = function()

  path = paths[counter]

  counter = counter + 1

  if counter > #paths then

  counter = 1

  end

  return wrk.format(nil, path)

  end

  關于 cookie

  有些時候我們需要模擬一些通過 cookie 傳遞數據的場景. wrk 并沒有特殊支持, 可以通過 wrk.headers[“Cookie”]=”xxxxx”實現.

  下面是在網上找的一個離職, 取 Response的cookie作為后續請求的cookie

  function getCookie(cookies, name)

  localstart = string.find(cookies, name .. "=")

  if start == nilthen

  return nil

  end

  return string.sub(cookies, start + #name + 1, string.find(cookies, ";", start) - 1)

  end

  response = function(status, headers, body)

  localtoken = getCookie(headers["Set-Cookie"], "token")

  if token ~= nilthen

  wrk.headers["Cookie"] = "token=" .. token

  end

  end

  wrk 本身的定位不是用來替換 loadrunner 這樣的專業性能測試工具的. 其實有這些功能已經完全能應付平時開發過程中的一些性能驗證了.

原文轉自: https://blog.satikey.com/p/5768/wrk-the-compact-and-lightweight-htt

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97