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 请求的性能表现。

相关推荐
爱瑞瑞3 天前
Junit5测试类编写笔记
junit
黄暄4 天前
分布式锁优化:使用Lua脚本保证释放锁的原子性问题
java·redis·分布式·后端·junit·lua
cxh_陈6 天前
org.junit.runners.model.InvalidTestClassError:此类问题的解决
java·开发语言·junit
.生产的驴9 天前
SpringBoot 执行Lua脚本 服务端执行 减少性能损耗 优化性能 优化连接性能
java·数据库·spring boot·后端·junit·maven·lua
小小数媒成员9 天前
Unity—lua基础语法
unity·junit·lua
奋斗的老史10 天前
利用Lua脚本限制用户的访问频率
开发语言·junit·lua
渡梦酒10 天前
Redis批量删除Key的三种方式
数据库·redis·junit
兰德里的折磨55010 天前
为什么要使用stream流
java·jvm·spring boot·spring·junit·log4j·intellij-idea
i1yo_kiki11 天前
junit单元测试
junit·log4j
啥都想学的又啥都不会的研究生12 天前
log日志最佳实践
java·spring boot·后端·spring·junit·log4j·logback