Http benchmarking 工具 wrk 基本使用
Intro
wrk 是一款現(xiàn)代HTTP基準(zhǔn)測試工具,能夠在單個多核CPU上運行時產(chǎn)生顯著負(fù)載。它將多線程設(shè)計與可擴展事件通知系統(tǒng)(如epoll和kqueue)結(jié)合在一起。
官方描述:
wrk is a modern HTTP benchmarking tool capable of generating significant load when run on a single multi-core CPU. It combines a multithreaded design with scalable event notification systems such as epoll and kqueue.
An optional LuaJIT script can perform HTTP request generation, response processing, and custom reporting. Details are available in SCRIPTING and several examples are located in scripts/.
wrk 使用了 epoll,使得可以通過較少的線程來實現(xiàn)較多的連接,而用 ab 測試的時候就會發(fā)現(xiàn)很難達(dá)到特別高的并發(fā),而 wrk 則利用 i/o 復(fù)用來實現(xiàn)較少的線程達(dá)到較高的并發(fā)。
Install
wrk支持大多數(shù)類UNIX系統(tǒng),不支持windows。需要操作系統(tǒng)支持 LuaJIT 和 OpenSSL,不過不用擔(dān)心,大多數(shù)類Unix系統(tǒng)都支持。安裝wrk非常簡單,只要從github上下載wrk源碼,在項目路徑下執(zhí)行 make 命令即可。
在 win10 bash 上安裝參考 :https://www.cnblogs.com/savorboard/p/wrk.html
直接在 linux 上安裝參考:https://www.cnblogs.com/jiftle/p/7158291.html
不想安裝也可以直接使用 docker ,參考 williamyeh/wrk
Use
-
直接使用安裝的 wrk,使用示例如下:
wrk -t 400 -c 4000 --timeout 10s -d 10s --latency http://localhost:12345/api/values
-
使用 docker
docker run --rm williamyeh/wrk -t 400 -c 4000 --timeout 10s -d 10s --latency http://localhost:12345/api/values
參數(shù)詳解:
在 bash 中輸入 wrk
即可獲取到詳細(xì)的參數(shù)說明:
-c, --connections(連接數(shù)): total number of HTTP connections to keep open with each thread handling N = connections/threads
-d, --duration(測試持續(xù)時間): duration of the test, e.g. 2s, 2m, 2h
-t, --threads(線程): total number of threads to use
-s, --script(腳本): LuaJIT script, see SCRIPTING
-H, --header(頭信息): HTTP header to add to request, e.g. "User-Agent: wrk"
--latency(響應(yīng)信息): print detailed latency statistics
--timeout(超時時間): record a timeout if a response is not received within this amount of time.
來對必應(yīng)做一個測試
wrk -t8 -c200 -d30s --latency "http://www.bing.com"
輸出:
Running 30s test @ http://www.bing.com
8 threads and 200 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 46.67ms 215.38ms 1.67s 95.59%
Req/Sec 7.91k 1.15k 10.26k 70.77%
Latency Distribution
50% 2.93ms
75% 3.78ms
90% 4.73ms
99% 1.35s
1790465 requests in 30.01s, 684.08MB read
Requests/sec: 59658.29
Transfer/sec: 22.79MB
結(jié)果分析:
Running 30s test @ http://www.bing.com (壓測時間30s)
8 threads and 200 connections (共8個測試線程,200個連接)
Thread Stats Avg Stdev Max +/- Stdev
(平均值) (標(biāo)準(zhǔn)差)(最大值)(正負(fù)一個標(biāo)準(zhǔn)差所占比例)
Latency 46.67ms 215.38ms 1.67s 95.59%
(延遲)
Req/Sec 7.91k 1.15k 10.26k 70.77%
(處理中的請求數(shù))
Latency Distribution (延遲分布)
50% 2.93ms
75% 3.78ms
90% 4.73ms
99% 1.35s (99分位的延遲)
1790465 requests in 30.01s, 684.08MB read (30.01秒內(nèi)共處理完成了1790465個請求,讀取了684.08MB數(shù)據(jù))
Requests/sec: 59658.29 (平均每秒處理完成59658.29個請求)
Transfer/sec: 22.79MB (平均每秒讀取數(shù)據(jù)22.79MB)
注:如果 url 不帶 &
參數(shù)可以不需要加加引號,如: "url",但是如果有多個get參數(shù)有 &
則需要加上雙引號才能請求完整的地址,否則會把 &
后面的參數(shù)丟失。