【wrk】wrk 压测工具入门

1. 简介

wrk 是我无意间发现的一款简单好用的 HTTP 接口性能测试工具,目前在 Github 上已经有 38k 的 star 数了!

⭐ Github地址:https://github.com/wg/wrk

2. 安装

环境要求:

  • windows10 平台
  • 安装过 ubuntu 等 Linux 子系统

在 windows 中通过 bash 进入 linux 环境依次执行以下命令:sudo apt-get install wrk

如上图所示,如果执行wrk命令能看到以下结果证明安装完成!

3. 使用

wrk 工具的使用非常简单:只需要掌握以下这个命令:wrk -t2 -d10s -c100 -s ./script/login.lua http://172.24.0.1:8081/users/login,其中各个参数选项含义如下:

  • -t:启动线程的数量
  • -d:持续时间,比如这里就是10s(也可以指定成1m表示1分钟)
  • -c:并发请求数
  • -s:运行脚本的路径

❗ 注意:如果在 windows 中的 linux 子系统中,直接使用 localhost 访问是不可行的,需要先在 windows cmd 中使用 ipconfig 获取本机ip地址,然后替代 localhost,参考博客:https://blog.csdn.net/qq_41407687/article/details/142172220

测试结果如上图所示:我们可以调整参数寻找最优测试结果

4. 附:Lua 脚本

4.1 signup.lua

wrk.method="POST"
wrk.headers["Content-Type"] = "application/json"

local random = math.random
local function uuid()
  local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
  return string.gsub(template, '[xy]', function (c)
      local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
      return string.format('%x', v)
    end)
end

-- 初始化
function init(args)
  -- 每个线程都有一个 cnt,所以是线程安全的
  cnt = 0
  prefix = uuid()
end

function request()
  body=string.format('{"email":"%s%d@qq.com", "password":"hello#world123", "confirmPassword": "hello#world123"}', prefix, cnt)
  cnt = cnt + 1
  return wrk.format('POST', wrk.path, wrk.headers, body)
end

function response()

end

4.2 login.lua

wrk.method="POST"
wrk.headers["Content-Type"]="application/json"
wrk.body='"{"email": "wjjbangbangbang@163.com","password": "hello#world123"}"'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值