wrk是一款簡(jiǎn)單的HTTP壓測(cè)工具,托管在Github上,https://github.com/wg/wrk.
wrk 的一個(gè)很好的特性就是能用很少的線程壓出很大的并發(fā)量. 原因是它使用了一些操作系統(tǒng)特定的高性能 io 機(jī)制, 比如 select, epoll, kqueue 等. 其實(shí)它是復(fù)用了 redis 的 ae 異步事件驅(qū)動(dòng)框架. 確切的說(shuō) ae 事件驅(qū)動(dòng)框架并不是 redis 發(fā)明的, 它來(lái)至于 Tcl的解釋器 jim, 這個(gè)小巧高效的框架, 因?yàn)楸?redis 采用而更多的被大家所熟知.
安裝
git clone https://github.com/wg/wrk.git
cd wrk
make
如果編譯過(guò)程中出錯(cuò):
src/wrk.h:11:25: fatal error: openssl/ssl.h: No such file or directory
#include <openssl/ssl.h>
則需要安裝openssl
,使用sudo apt-get install libssl-dev
或 sudo yum install openssl-devel
安裝即可,最后編輯etc/profile
配置環(huán)境變量。由于筆者使用的是阿里云centos7,相關(guān)依賴都已經(jīng)存在了,所以可以直接使用椿每。
開(kāi)始測(cè)試一下
wrk -t12 -c100 -d30s http://www.baidu.com
這段腳本的輸出是:
[root@jerrik /]# wrk -t12 -c100 -d30s http://www.baidu.com
Running 30s test @ http://www.baidu.com
12 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 211.76ms 304.92ms 1.97s 88.17%
Req/Sec 72.93 68.72 797.00 90.97%
23725 requests in 30.05s, 347.47MB read
Socket errors: connect 0, read 48, write 0, timeout 50
Requests/sec: 789.57
Transfer/sec: 11.56MB
[root@jerrik /]#
一般線程數(shù)不宜過(guò)多. 核數(shù)的2到4倍足夠了. 多了反而因?yàn)榫€程切換過(guò)多造成效率降低. 因?yàn)?wrk 不是使用每個(gè)連接一個(gè)線程的模型, 而是通過(guò)異步網(wǎng)絡(luò) io 提升并發(fā)量. 所以網(wǎng)絡(luò)通信不會(huì)阻塞線程執(zhí)行. 這也是 wrk 可以用很少的線程模擬大量網(wǎng)路連接的原因. 而現(xiàn)在很多性能工具并沒(méi)有采用這種方式, 而是采用提高線程數(shù)來(lái)實(shí)現(xiàn)高并發(fā). 所以并發(fā)量一旦設(shè)的很高, 測(cè)試機(jī)自身壓力就很大. 測(cè)試效果反而下降.
參數(shù)解釋:
-
12 threads and 100 connections
:
總共是12個(gè)線程,100個(gè)連接(不是一個(gè)線程對(duì)應(yīng)一個(gè)連接) -
latency
和Req/Sec
:
代表單個(gè)線程的統(tǒng)計(jì)數(shù)據(jù),latency
代表延遲時(shí)間,Req/Sec
代表單個(gè)線程每秒完成的請(qǐng)求數(shù)教硫,他們都具有平均值, 標(biāo)準(zhǔn)偏差, 最大值, 正負(fù)一個(gè)標(biāo)準(zhǔn)差占比败许。一般我們來(lái)說(shuō)我們主要關(guān)注平均值和最大值. 標(biāo)準(zhǔn)差如果太大說(shuō)明樣本本身離散程度比較高. 有可能系統(tǒng)性能波動(dòng)很大. -
23725 requests in 30.05s, 347.47MB read
在30秒之內(nèi)總共有23725個(gè)請(qǐng)求,總共讀取347.47MB的數(shù)據(jù) -
Socket errors: connect 0, read 48, write 0, timeout 50
總共有48個(gè)讀錯(cuò)誤,50個(gè)超時(shí). -
Requests/sec和Transfer/sec
所有線程平均每秒鐘完成了789.57個(gè)請(qǐng)求,每秒鐘讀取11.56MB數(shù)據(jù)量
如果想看看響應(yīng)時(shí)間的分布,可以增加--latency
:
wrk -t12 -c100 -d30s --latency http://www.baidu.com
結(jié)果為:
[root@jerrik ~]# wrk -t12 -c100 -d30s --latency http://www.baidu.com
Running 30s test @ http://www.baidu.com
12 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 204.30ms 287.90ms 1.97s 88.61%
Req/Sec 71.43 67.59 810.00 89.77%
Latency Distribution
50% 14.76ms
75% 296.79ms
90% 545.03ms
99% 1.40s
23676 requests in 30.03s, 346.84MB read
Socket errors: connect 0, read 42, write 0, timeout 46
Requests/sec: 788.29
Transfer/sec: 11.55MB
說(shuō)明有50%的請(qǐng)求在14.76ms之內(nèi),90%在545.03ms之內(nèi)赶么。
高級(jí)用法
wrk可以結(jié)合lua來(lái)做,通過(guò)wrk提供的幾個(gè)lua函數(shù)來(lái)對(duì)請(qǐng)求進(jìn)行修改,結(jié)果輸出、設(shè)置延遲等操作抢蚀。下面來(lái)看看wrk提供的幾個(gè)lua函數(shù):
setup 函數(shù)
這個(gè)函數(shù)在目標(biāo) IP 地址已經(jīng)解析完, 并且所有 thread 已經(jīng)生成, 但是還沒(méi)有開(kāi)始時(shí)被調(diào)用. 每個(gè)線程執(zhí)行一次這個(gè)函數(shù).
可以通過(guò)thread:get(name), thread:set(name, value)設(shè)置線程級(jí)別的變量.init 函數(shù)
每次請(qǐng)求發(fā)送之前被調(diào)用.
可以接受 wrk 命令行的額外參數(shù). 通過(guò) -- 指定.delay函數(shù)
這個(gè)函數(shù)返回一個(gè)數(shù)值, 在這次請(qǐng)求執(zhí)行完以后延遲多長(zhǎng)時(shí)間執(zhí)行下一個(gè)請(qǐng)求. 可以對(duì)應(yīng) thinking time 的場(chǎng)景.request函數(shù)
通過(guò)這個(gè)函數(shù)可以每次請(qǐng)求之前修改本次請(qǐng)求的屬性. 返回一個(gè)字符串. 這個(gè)函數(shù)要慎用, 會(huì)影響測(cè)試端性能.response函數(shù)
每次請(qǐng)求返回以后被調(diào)用. 可以根據(jù)響應(yīng)內(nèi)容做特殊處理, 比如遇到特殊響應(yīng)停止執(zhí)行測(cè)試, 或輸出到控制臺(tái)等等.
function response(status, headers, body)
if status ~= 200 then
print(body)
wrk.thread:stop()
end
end
- done函數(shù)
在所有請(qǐng)求執(zhí)行完以后調(diào)用, 一般用于自定義統(tǒng)計(jì)結(jié)果.
done = function(summary, latency, requests)
io.write("------------------------------\n")
for _, p in pairs({ 50, 90, 99, 99.999 }) do
n = latency:percentile(p)
io.write(string.format("%g%%,%d\n", p, n))
end
end
wrk官網(wǎng)提供的setup.lua實(shí)例:
-- example script that demonstrates use of setup() to pass
-- data to and from the threads
local counter = 1
local threads = {}
function setup(thread)
thread:set("id", counter)
table.insert(threads, thread)
counter = counter + 1
end
function init(args)
requests = 0
responses = 0
local msg = "thread %d created"
print(msg:format(id))
end
function request()
requests = requests + 1
return wrk.request()
end
function response(status, headers, body)
responses = responses + 1
end
function done(summary, latency, requests)
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
使用setup.lua:
[root@jerrik wrk]# wrk -t 4 -c 100 -d 20s --latency -s scripts/setup.lua https://www.baidu.com
thread 1 created
thread 2 created
thread 3 created
thread 4 created
Running 20s test @ https://www.baidu.com
4 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 251.75ms 336.19ms 2.00s 86.89%
Req/Sec 138.51 69.90 690.00 71.23%
Latency Distribution
50% 215.74ms
75% 401.87ms
90% 664.84ms
99% 1.54s
11021 requests in 20.02s, 162.82MB read
Socket errors: connect 0, read 3, write 0, timeout 50
Requests/sec: 550.62
Transfer/sec: 8.13MB
thread 1 made 2945 requests and got 2919 responses
thread 2 made 2831 requests and got 2807 responses
thread 3 made 2772 requests and got 2747 responses
thread 4 made 2573 requests and got 2548 responses
[root@jerrik wrk]#
將每個(gè)線程的請(qǐng)求數(shù)和響應(yīng)數(shù)輸出來(lái)了地沮。其它更多使用可以參考github script目錄下的lua腳本。
總結(jié)
wrk作為http壓測(cè)還是非常簡(jiǎn)便的,但是要想應(yīng)對(duì)更多復(fù)雜場(chǎng)景,就需要多熟悉lua的使用,深入了解wrk提供的那幾個(gè)函數(shù)嘉汰。其它http壓測(cè)工具,jmeter,apache ab,siege也可以了解一下丹禀。
作者:jerrik
鏈接:http://www.reibang.com/p/ac185e01cc30