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

相关推荐
Jomurphys13 小时前
测试 - 单元测试(JUnit)
android·junit·单元测试
一过菜只因1 天前
使用Junit测试
服务器·数据库·junit
张较瘦_2 天前
Springboot3 | JUnit 5 使用详解
spring boot·junit
IMPYLH2 天前
Lua 的 warn 函数
java·开发语言·笔记·junit·lua
yeshihouhou3 天前
redis实现分布式锁
redis·分布式·junit
小雨下雨的雨4 天前
第5篇:Redis事务与Lua脚本
redis·junit·lua
BuHuaX4 天前
Lua入门
开发语言·unity·junit·c#·游戏引擎·lua
联系QQ 180809514 天前
离线DP节能速度规划联合仿真:Matlab Simulink 2021a与Carsim 201...
junit
weixin_307779135 天前
Jenkins JUnit插件:自动化测试报告与质量守护者
开发语言·junit·单元测试·自动化·jenkins
爬山算法6 天前
Redis(171)如何使用Redis实现分布式事务?
redis·分布式·junit