wrk如何测试post请求

wrk git地址

https://github.com/wg/wrk

wrk 默认是针对 GET 请求 的,但它也可以通过添加自定义的 HTTP 请求体头部信息来进行 POST 请求的压测。以下是详细的步骤:

wrk -t4 -c100 -d30s -s post.lua http://example.com
  • -t4:使用 4 个线程。
  • -c100:模拟 100 个并发连接。
  • -d30s:测试持续 30 秒。
  • -s post.lua:加载一个自定义的 Lua 脚本 post.lua,用于指定 POST 请求的内容。

编写 post.lua 脚本

创建一个 post.lua 文件并写入以下内容:

wrk.method = "POST"
wrk.body   = '{"key1":"value1", "key2":"value2"}'
wrk.headers["Content-Type"] = "application/json"
参数说明:
  1. wrk.method :指定请求方法为 POST
  2. wrk.body:定义请求的 JSON 数据。
  3. wrk.headers["Content-Type"] :指定 HTTP 头部的 Content-Typeapplication/json

完整示例

假设你的 API 是 http://example.com/api,需要发送以下 POST 请求:

  • URL : http://example.com/api
  • Body : {"username": "testuser", "password": "testpass"}
  • Headers : Content-Type: application/json
脚本内容:
Lua 复制代码
wrk.method = "POST"
wrk.body   = '{"username": "testuser", "password": "testpass"}'
wrk.headers["Content-Type"] = "application/json"

执行命令:

wrk -t2 -c50 -d10s -s post.lua http://example.com/api
  • -t2:使用 2 个线程。
  • -c50:模拟 50 个并发连接。
  • -d10s:测试持续 10 秒。

动态参数的高级脚本

如果需要动态生成请求体(例如每次发送不同的参数),可以在 Lua 脚本中自定义逻辑:

Lua 复制代码
counter = 0

request = function()
   counter = counter + 1
   local body = string.format('{"username": "user%d", "password": "pass%d"}', counter, counter)
   wrk.body = body
   wrk.headers["Content-Type"] = "application/json"
   return wrk.format("POST")
end

在这个脚本中,每次请求都会生成不同的 usernamepassword

分析测试结果

运行完成后,wrk 会输出以下指标:

  • Requests/sec:每秒完成的请求数。
  • Latency:延迟(平均、最大、最小)。
  • Transfer/sec:数据传输速度。

通过这些结果,可以评估 POST 请求的性能表现。

相关推荐
2501_903238652 天前
深入解析JUnit中的@ClassRule注解
数据库·junit·个人开发
LuiChun5 天前
http3网站的设置(AI不会配,得人工配)
junit
Narutolxy5 天前
使用 OpenResty 构建高效的动态图片水印代理服务20250127
junit·openresty
上不如老下不如小7 天前
Spring整合Mybatis、junit纯注解
spring·junit·mybatis
厂里英才10 天前
软件质量与测试报告3-功能测试 JUnit与覆盖测试 EclEmma
功能测试·junit
qw94912 天前
Spring 6 第6章——单元测试:Junit
spring·junit·单元测试
逆风局?13 天前
JUnit单元测试
junit·单元测试
码明14 天前
SpringBoot整合junit
数据库·spring boot·junit
大道之简15 天前
Mockito+PowerMock+Junit单元测试
junit·单元测试
肉三17 天前
使用 JUnit 和 SuiteRunner 测试私有方法
java·junit·log4j