wrk的安装与使用

wrk的使用

wrk的下载与安装

wrk的源码在github上,地址是https://github.com/wg/wrk.git,下载之后解压,在其目录下运行make命令安装,之后可直接使用

git clone https://github.com/wg/wrk
make
参数
-t 需要模拟的线程数
-c 需要模拟的连接数,总的连接数,如-t 10 -c 20,则表示起十个线程,每个线程2个连接
--timeout 超时的时间
-d 测试的持续时间

结果
Latency:响应时间
Req/Sec:每个线程每秒钟的完成的请求数
Avg:平均
Max:最大
Stdev:标准差,标准差如果太大说明样本本身离散程度比较高. 有可能系统性能波动很大.
+/- Stdev: 正负一个标准差占比

示例
wrk  -t5 -c20 -d10s --latency "http://www.baidu.com"

wrk使用说明

wrk
Usage: wrk <options> <url>
  Options:
    -c, --connections <N>  Connections to keep open
    -d, --duration    <T>  Duration of test
    -t, --threads     <N>  Number of threads to use
    -s, --script      <S>  Load Lua script file
    -H, --header      <H>  Add header to request
        --latency          Print latency statistics
        --timeout     <T>  Socket/request timeout
    -v, --version          Print version details
  Numeric arguments may include a SI unit (1k, 1M, 1G)
  Time arguments may include a time unit (2s, 2m, 2h)

wrk的生命周期

wrk 可以在lua脚本里添加下面的Hook函数,你可以想象成生命周期,每个生命周期做的事情都不一样, 但是生命周期是有时间顺序的。我们常用一般是 request 和 delay 周期.

setup
线程出事后支持会调用一次。
function setup(thread)

-- thread提供了1个属性,3个方法
-- thread.addr 设置请求需要打到的ip
-- thread:get(name) 获取线程全局变量
-- thread:set(name, value) 设置线程全局变量
-- thread:stop() 终止线程

init
每次请求发送之前被调用。可以接受 wrk 命令行的额外参数。
function init(args)

delay
这个函数返回一个数值,在这次请求执行完以后延迟多长时间执行下一个请求,可以对应 thinking time 的场景。
function delay()

request
通过这个函数可以每次请求之前修改本次请求体和Header,我们可以在这里写一些要压力测试的逻辑。
function request()

response
每次请求返回以后被调用,可以根据响应内容做特殊处理,比如遇到特殊响应停止执行测试,或输出到控制台等等。
function response(status, headers, body)

done
该方法在整个测试过程中只会调用一次,可从参数给定的对象中,获取压测结果,生成定制化的测试报告。
function done(summary, latency, requests)

使用lua脚本制定个性化wrk压测

-- example script that demonstrates use of setup() to pass
-- data to and from the threads
 
local counter = 1
local threads = {}
 
function setup(thread)
-- 给每个线程设置一个 id 参数
   thread:set("id", counter)
-- 将线程添加到 table 中
   table.insert(threads, thread)
   counter = counter + 1
end
 
function init(args)
-- 初始化两个参数,每个线程都有独立的 requests、responses 参数
   requests  = 0
   responses = 0
 
-- 打印线程被创建的消息,打印完后,线程正式启动运行
   local msg = "thread %d created"
   print(msg:format(id))
end
 
function request()
-- 每发起一次请求 +1
   requests = requests + 1
   return wrk.request()
end
 
function response(status, headers, body)
-- 每得到一次请求的响应 +1
   responses = responses + 1
end
 
function done(summary, latency, requests)
-- 循环线程 table
   for index, thread in ipairs(threads) do
      local id        = thread:get("id")
      local requests  = thread:get("requests")
      local responses = thread:get("responses")
      local msg = "thread %d made %d requests and got %d responses"
-- 打印每个线程发起了多少个请求,得到了多少次响应
      print(msg:format(id, requests, responses))
   end
end
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值