wrk压测工具的使用

Date: 2019/05/15 Categories: 工作 Tags: wrk http benchmark



Pro and Con

wrk的优点

  • 轻量
  • 快速
  • 灵活

wrk的缺点

  • 默认输出格式是文本, 不利于机器处理
  • 只能输出吞吐量, 延迟
  • 要使用文件来post压测需要每个线程都加载一份数据, 消耗内存

wrk脚本

Single Post Body

Post Body from File

counter = 0

-- Initialize the pseudo random number generator - http://lua-users.org/wiki/MathLibraryTutorial
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 read_lines(file)
  if not file_exists(file) then return {} end
  lines = {}
  for line in io.lines(file) do
    if not (line == '') then
      lines[#lines + 1] = line
    end
    if (#lines % 10000 == 0) then
        print("read " .. #lines .. " lines")
    end
    if #lines == 100000 then
        break
    end
  end
  return shuffle(lines)
end

bodies = read_lines("tql.txt")

print("Found " .. #bodies.. " bodies")

url = '/'
counter = 0
request = function()
    body = bodies[counter]
    counter = counter + 1
    if counter > #bodies then
        counter = 1
    end
    return wrk.format('POST', url, nil, body)
end

这里使用了前100000行的数据从tql.txt中作为post body, 注意wrk的每个线程都会加载一份数据.

运行命令

wrk --latency -c 16 -t 8 -d 86400s -s 1.lua http://127.0.0.1:8182

输出类似

Running 1440m test @ http://127.0.0.1:8182
  8 threads and 16 connections
n  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    25.48ms   51.70ms   1.97s    97.20%
    Req/Sec   109.65     45.33   740.00     68.81%
  Latency Distribution
     50%   18.95ms
     75%   39.68ms
     90%   52.60ms
     99%  119.05ms
  75182324 requests in 1440.01m, 237.05GB read
  Socket errors: connect 0, read 16373, write 3118830, timeout 803
  Non-2xx or 3xx responses: 16338
Requests/sec:    870.16
Transfer/sec:      2.81MB

可以看到qps为870, 平均延迟是25.48ms, 50 percentile为18.95ms