wrk压测工具

 

1、参数说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
使用方法: 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请求

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

  

3、发送一个post请求

编写lua脚本post.lua

1
2
3
4
5
6
7
wrk.method = "POST"
wrk.body = '{"username":"admin","password":"123456"}'
wrk.headers["Content-Type"] = "application/json"
 
response = function(status, headers, body)
print(body) --调试用,正式测试时需要关闭,因为解析response非常消耗资源
end

执行命令

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

 

4、同时发送多个get请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//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格式。

执行命令

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

 

参考:

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

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

posted @   ☞@_@  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
点击右上角即可分享
微信分享提示