httpstat 是一個用於網站效能測試的 Python 指令稿,可在終端機下測試伺服器的回應速度。

httpstat 是一個以純 Python 寫成的網站效能測試工具,這個工具只有單一個 Python 指令稿,僅需要 Python 的執行環境,不需要依賴任何其他的函數庫,不管是攜帶或使用上都很方便。

名稱:httpstat 網站效能測試工具
網站:GitHub

這個 httpstat 其實本質上就是一個 cURL 的包裝工具,所以它可以接受各種 cURL 的功能參數。(-w-D-o-s-S 例外,因為這些已經被 httpstat 內部使用了)

以下就是 httpstat 的安裝與使用方式教學,還有許多實物上的範例指令。

下載與安裝 httpstat

在 Linux 中有兩種方式可以安裝 httpstat,一種是直接下載 httpstat 的指令稿:

wget -c https://raw.githubusercontent.com/reorx/httpstat/master/httpstat.py

另外一種是使用 Python 的 pip 安裝:

sudo pip install httpstat

httpstat 使用方式

httpstat 的使用方式非常單純,只要使用 Python 執行它,並指定要測試的網站網址即可:

python httpstat.py http://www.google.com.tw/

或是

httpstat http://www.google.com.tw/

測試的結果輸出會像這樣:

httpstat-curl-statistics-tool-to-check-website-performance-20161203-1

httpstat 測試網站效能

整個網路連線所耗費的時間分為四段:

  1. DNS Lookup:DNS 名稱解析所耗費的時間。
  2. TCP Connection:實際與網頁伺服器建立 TCP 連線所耗費的時間。
  3. Server Processing:網頁伺服器在處理網頁請求所耗費的時間。
  4. Content Transfer:傳送網頁內容至瀏覽器所耗費的時間。

從這個報表我們就可以大約看出網站目前的運行情況是否正常。

我們也可以把網址的 http:// 省略,簡寫成這樣也可以:

python httpstat.py www.google.com.tw

httpstat 也可以進行 HTTP 的 POST 連線測試:

python httpstat.py httpbin.org/post -X POST --data-urlencode "x=y" -v

測試的結果如下:

httpstat-curl-statistics-tool-to-check-website-performance-20161203-2

httpstat 網站 POST 連線測試

這裡我們加上了幾個參數:

  • -X:指定 HTTP 的請求連線方式,例如 GETPOST 等。
  • --data-urlencode:指定傳送的資料。
  • -v:顯示詳細的輸出訊息。

這些參數都是 cURL 的標準參數,詳細的說明可以參考 cURL 的 man page:

man curl

httpstat 除了可以使用 cURL 的標準參數之外,還有一些環境變數的選項可以使用,我們可以從 httpstat 的使用說明查看:

python httpstat.py -h
httpstat-curl-statistics-tool-to-check-website-performance-20161203-3

httpstat 使用說明

以下是測試檔案上傳的指令,並且設定 HTTPSTAT_SHOW_SPEED 環境變數,顯示上傳與下載速度:

HTTPSTAT_SHOW_SPEED=true python httpstat.py httpbin.org/post -X POST -F name=test -F filedata=@local.jpg -v
httpstat-curl-statistics-tool-to-check-website-performance-20161203-4

httpstat 測試檔案上傳

參考資料:Tecmint