您的位置:首页 > 理论基础 > 计算机网络

高性能测试工具WRK的高级使用方法

2017-10-27 20:57 567 查看

WRK

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

简介:基于事件机制的高性能http压力测试工具,除了能针对单个url进行测试外,最重要的就是能够构造不同的url,不同的参数进行测试。或者发送携带body的POST请求,在有些场景下不要太帅。

说明

wrk高级的地方就是使用了luajit,在不同阶段抛出来几个回调函数,只需要针对这些函数操作即可构造想要请求。典型的回调函数有以下几个

function setup(thread) 在创建线程时调用

function init(args) 在线程启动时调用

function request() 每个请求都会调用,特别说明,此函数调用频繁,所以不要在这里写入耗时的操作,如果复杂的请求,在init函数就构造好,这里直接引用。

function delay()

function response(status, headers, body) ,频繁调用。

举例

1 发送get 请求

如果想构造不同的请求比如/1?arg=1, /2?arg=2, /3?arg=3……,并且发送GET请求,创建文件为http_get.lua , 代码如下

counter = 0
num = 0
request = function()
path = "/" .. counter .. "?arg=" .. num
counter = counter + 1
num = num + 1
return wrk.format("", path)
end


执行命令为:

wrk -c 4 -t  4 -d 10s -s http_get.lua  http://cool.test.net/v1.f4v[/code] 
结果为:

4 threads and 4 connections
Thread Stats   Avg      Stdev     Max   +/- Stdev
Latency   476.52us  306.27us   5.74ms   94.84%
Req/Sec     2.12k   171.21     2.39k    65.12%
9085 requests in 1.10s, 1.49MB read
Requests/sec:   8256.69
Transfer/sec:      1.35MB


2 发送POST请求

文件名 http_post.lua

counter = 0
request = function()
path = "/" .. counter
counter = counter + 1
headers = {TH= "httpheader"}
body = "hello world " .. counter
return wrk.format("POST", path, headers, body)
end


3 文件中指定uri

如果要测试的url在文件中呢,那么在init阶段把uri都读取到table中,在request中直接引用即可, 具体如下

uri文件为: uri.txt

/1.txt
/2.txt
/3.txt
/4.txt
/5.txt


文件名 http_file_uri.lua,代码

urimap = {}
counter = 0
function init(args)
for line in io.lines("uri.txt") do
print(line)
urimap[counter] = line
counter = counter + 1
end
counter = 0
end

request = function()
counter = counter + 1
path = urimap[counter%table.getn(urimap) + 1]
return wrk.format(nil, path)
end


效果如下:

Thread Stats   Avg      Stdev     Max   +/- Stdev
Latency   484.00us  335.83us   8.44ms   95.13%
Req/Sec     2.14k   192.78     2.59k    71.50%
85400 requests in 10.00s, 14.00MB read
Requests/sec:   8538.60
Transfer/sec:      1.40MB


服务器端收到请求

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息