Go 每日一庫之帶 WebUI 的 HTTP Benchmark
今天給大家分享一篇關於 HTTP 壓測工具的短文。
前兩天在逛推特,無意中發現一個帶有 WebUI 和終端展示的 HTTP 壓測庫 plow[1] ,其實老早我就想找一個類似的庫,終於被我發現了,哈哈。其實這種數據統計展示也可以在業務 API 中上報的 Metrics 相關的 grafana 監控面板上呈現出來。下面我簡單介紹一下這個工具的使用。主要參考了一下 readme。
plow 介紹
plow
是採用 Golang 編寫的一個 HTTP(S) 基準測試工具。內部採用了性能出色的 fasthttp[2] 而不是 Go 的默認net/http
。
plow 是如何計算的呢,性能怎麼樣
這裏主要受 prometheus - 普羅米修斯 [3] 啓發是基於流的算法實現實時計算直方圖和分位數,具有很低的內存和 CPU 消耗。因此,基準測試幾乎沒有額外的性能開銷。
安裝與使用
可以通過以下三種方式進行安裝:
- go get
$ go get -u github.com/six-ddc/plow
- homebrew
$ brew install plow
- Docker 安裝
$ docker run --rm --net=host ghcr.io/six-ddc/plow
# docker run --rm -p 18888:18888 ghcr.io/six-ddc/plow
使用的話可以執行$ plow --help
查看更多幫助信息,支持 GET 和 POST 請求。
$ plow --help
usage: plow [<flags>] <url>
A high-performance HTTP benchmarking tool with real-time web UI and terminal
displaying
Example:
plow http://127.0.0.1:8080/ -c 20 -n 100000
plow https://httpbin.org/post -c 20 -d 5m --body @file.json -T 'application/json' -m POST
Flags:
--help Show context-sensitive help.
-c, --concurrency=1 Number of connections to run concurrently
-n, --requests=-1 Number of requests to run
-d, --duration=DURATION Duration of test, examples: -d 10s -d 3m
-i, --interval=200ms Print snapshot result every interval, use 0 to
print once at the end
--seconds Use seconds as time unit to print
--body=BODY HTTP request body, if start the body with @, the
rest should be a filename to read
--stream Specify whether to stream file specified by
'--body @file' using chunked encoding or to read
into memory
-m, --method="GET" HTTP method
-H, --header=K:V ... Custom HTTP headers
--host=HOST Host header
-T, --content=CONTENT Content-Type header
--listen="0.0.0.0:18888" Listen addr to serve Web UI
--timeout=DURATION Timeout for each http request
--dial-timeout=DURATION Timeout for dial addr
--req-timeout=DURATION Timeout for full request writing
--resp-timeout=DURATION Timeout for full response reading
--socks5=ip:port Socks5 proxy
--version Show application version.
Args:
<url> request url
例子
下面主要展示下 plow 進行 get 請求壓力測試的例子,這裏壓測圖示採用官方的 example,命令如下:
$ plow http://127.0.0.1:8080/hello -c 20 -d 15s -n 5000000
這裏主要測試的是 get 請求本地接口:建立了 20 個連接,在 15 秒內請求了 500w 次,相信大家也看懂了這裏,下面的 flag 還是解釋一下吧。
-
-c
指定連接數 (connections) -
-n
指定請求數 (number) -
-d
指定請求的時間 (duration)
接下來看下下官方的 example 數據展示,左邊是終端數據實時展示,右側是127.0.0.1:18888
的實時 WebUI 數據展示(延遲:最小延遲 - 平均延遲 - 最大延遲,每秒請求數)。
最後大家如有推薦的自己使用和了解的 HTTP 壓測工具,歡迎留言討論。
參考資料
[1]
plow: https://github.com/six-ddc/plow
[2]
fasthttp: https://github.com/valyala/fasthttp#http-client-comparison-with-nethttp
[3]
prometheus - 普羅米修斯: https://github.com/prometheus/client_golang
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/DbEtp6fsCK1aHroDDkkIMg