wrk压测工具

发布时间 2023-04-21 15:59:09作者: ☞@_@

 

1、参数说明

使用方法: wrk <选项> <被测HTTP服务的URL>                            
  Options:                                            
    -c, --connections <N>  跟服务器建立并保持的TCP连接数量  
    -d, --duration    <T>  压测时间           
    -t, --threads     <N>  使用多少个线程进行压测   
                                                      
    -s, --script      <S>  指定Lua脚本路径       
    -H, --header      <H>  为每一个HTTP请求添加HTTP头      
        --latency          在压测结束后,打印延迟统计信息   
        --timeout     <T>  超时时间     
    -v, --version          打印正在使用的wrk的详细版本信息
                                                      
  <N>代表数字参数,支持国际单位 (1k, 1M, 1G)
  <T>代表时间参数,支持时间单位 (2s, 2m, 2h)

  

2、发送一个get请求

wrk -t12 -c400 -d30s --latency http://www.baidu.com

  

3、发送一个post请求

编写lua脚本post.lua

wrk.method = "POST"
wrk.body = '{"username":"admin","password":"123456"}'
wrk.headers["Content-Type"] = "application/json"

response = function(status, headers, body)
print(body) --调试用,正式测试时需要关闭,因为解析response非常消耗资源
end

执行命令

wrk -t12 -s ./post.lua -c400 -d30s --latency http://127.0.0.1:8080/login

 

4、同时发送多个get请求

//counter = 0
counter = 1

-- 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 non_empty_lines_from(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
  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 = 0
     counter = 1
    end
    return wrk.format(nil, path)
end

在wrk当前目录下创建一个paths.txt,将要压测的url保存在这个文件中,一行一个。注意paths.txt如果是dos格式,会报错,得改成unix格式。

执行命令

wrk -c 100 -t 4 -d 30s -s wrk-scripts/multiplepaths.lua http://localhost

 

参考:

  性能测试工具 wrk 使用教程 - 犬小哈 - 博客园 (cnblogs.com)

      wrk入门(2):发送post请求 - 章土 - 博客园 (cnblogs.com)