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

相关推荐
不求大富大贵只求富可敌国1 天前
Gtest, Junit,以及pytest对比理解
junit·pytest
weixin_408773883 天前
redis主从哨兵模式+Lua报错-READONLY You can‘t write against a read
redis·junit·lua
Rhys..4 天前
如何在Jenkins上查看Junit报告
servlet·junit·jenkins
2501_903238656 天前
深入理解 JUnit 的 @RunWith 注解与自定义 Runner
数据库·junit·sqlserver·个人开发
Rhys..7 天前
Jenkins上无法查看已成功生成的Junit报告
junit·sqlserver·jenkins
Rhys..7 天前
如何生成Junit报告
python·junit·jenkins
阿湯哥8 天前
Lua脚本核心语法介绍
开发语言·junit·lua
言慢行善10 天前
Junit——白盒测试
junit
刘一二11 天前
Spring单元测试(一)入门与实践
spring·junit·单元测试
不听话的小耳朵13 天前
2.7 静态方法/构造函数Mock
java·junit