wrk是一款簡單的HTTP壓測工具,託管在Github上,https://github.com/wg/wrk.
wrk 的一個很好的特性就是能用很少的線程壓出很大的併發量. 原因是它使用了一些操作系統特定的高性能 io 機制, 比如 select, epoll, kqueue 等. 其實它是複用了 redis 的 ae 異步事件驅動框架. 確切的說 ae 事件驅動框架並不是 redis 發明的, 它來至於 Tcl的解釋器 jim, 這個小巧高效的框架, 因爲被 redis 採用而更多的被大家所熟知.
安裝
git clone https://github.com/wg/wrk.git
cd wrk
make
如果編譯過程中出錯:
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
配置環境變量。由於筆者使用的是阿里雲centos7,相關依賴都已經存在了,所以可以直接使用。
開始測試一下
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 /]#
一般線程數不宜過多. 核數的2到4倍足夠了. 多了反而因爲線程切換過多造成效率降低. 因爲 wrk 不是使用每個連接一個線程的模型, 而是通過異步網絡 io 提升併發量. 所以網絡通信不會阻塞線程執行. 這也是 wrk 可以用很少的線程模擬大量網路連接的原因. 而現在很多性能工具並沒有採用這種方式, 而是採用提高線程數來實現高併發. 所以併發量一旦設的很高, 測試機自身壓力就很大. 測試效果反而下降.
參數解釋:
12 threads and 100 connections
:
總共是12個線程,100個連接(不是一個線程對應一個連接)latency
和Req/Sec
:
代表單個線程的統計數據,latency
代表延遲時間,Req/Sec
代表單個線程每秒完成的請求數,他們都具有平均值, 標準偏差, 最大值, 正負一個標準差佔比。一般我們來說我們主要關注平均值和最大值. 標準差如果太大說明樣本本身離散程度比較高. 有可能系統性能波動很大.23725 requests in 30.05s, 347.47MB read
在30秒之內總共有23725個請求,總共讀取347.47MB的數據Socket errors: connect 0, read 48, write 0, timeout 50
總共有48個讀錯誤,50個超時.Requests/sec和Transfer/sec
所有線程平均每秒鐘完成了789.57個請求,每秒鐘讀取11.56MB數據量
如果想看看響應時間的分佈,可以增加--latency
:
wrk -t12 -c100 -d30s --latency http://www.baidu.com
結果爲:
[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
說明有50%的請求在14.76ms之內,90%在545.03ms之內。
高級用法
wrk可以結合lua來做,通過wrk提供的幾個lua函數來對請求進行修改,結果輸出、設置延遲等操作。下面來看看wrk提供的幾個lua函數:
-
setup 函數
這個函數在目標 IP 地址已經解析完, 並且所有 thread 已經生成, 但是還沒有開始時被調用. 每個線程執行一次這個函數.
可以通過thread:get(name), thread:set(name, value)設置線程級別的變量. -
init 函數
每次請求發送之前被調用.
可以接受 wrk 命令行的額外參數. 通過 -- 指定. -
delay函數
這個函數返回一個數值, 在這次請求執行完以後延遲多長時間執行下一個請求. 可以對應 thinking time 的場景. -
request函數
通過這個函數可以每次請求之前修改本次請求的屬性. 返回一個字符串. 這個函數要慎用, 會影響測試端性能. -
response函數
每次請求返回以後被調用. 可以根據響應內容做特殊處理, 比如遇到特殊響應停止執行測試, 或輸出到控制檯等等.
function response(status, headers, body)
if status ~= 200 then
print(body)
wrk.thread:stop()
end
end
- done函數
在所有請求執行完以後調用, 一般用於自定義統計結果.
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官網提供的setup.lua實例:
-- 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]#
將每個線程的請求數和響應數輸出來了。其它更多使用可以參考github script目錄下的lua腳本。
總結
wrk作爲http壓測還是非常簡便的,但是要想應對更多複雜場景,就需要多熟悉lua的使用,深入瞭解wrk提供的那幾個函數。其它http壓測工具,jmeter,apache ab,siege也可以瞭解一下。
作者:jerrik
鏈接:https://www.jianshu.com/p/ac185e01cc30
來源:簡書
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。