httpstatでcurlのレスポンスタイムをわかりやすく表示する
インストール
pipでインストールできるので下記コマンドを実行。
$ pip install httpstat
$ brew install httpstat
今回試すのはPythonバージョンだが、Goバージョンもある。
GitHub - davecheney/httpstat: It's like curl -v, with colours.
$ httpstat --help
Usage: httpstat URL [CURL_OPTIONS]
httpstat -h | --help
httpstat --version
Arguments:
URL url to request, could be with or without `http(s)://` prefix
Options:
CURL_OPTIONS any curl supported options, except for -w -D -o -S -s,
which are already used internally.
-h --help show this screen.
--version show version.
Environments:
HTTPSTAT_SHOW_BODY Set to `true` to show resposne body in the output,
note that body length is limited to 1023 bytes, will be
truncated if exceeds. Default is `false`.
HTTPSTAT_SHOW_IP By default httpstat shows remote and local IP/port address.
Set to `false` to disable this feature. Default is `true`.
HTTPSTAT_SHOW_SPEED Set to `true` to show download and upload speed.
Default is `false`.
HTTPSTAT_SAVE_BODY By default httpstat stores body in a tmp file,
set to `false` to disable this feature. Default is `true`
HTTPSTAT_CURL_BIN Indicate the curl bin path to use. Default is `curl`
from current shell $PATH.
HTTPSTAT_DEBUG Set to `true` to see debugging logs. Default is `false`使い方
使い方は簡単でURLを指定して実行すればよい。
$ httpstat httpbin.org/get
httpstatはcurlをラップしているのでcurlのオプションを指定することが可能。
ただし、-w, -D, -o, -s, -Sオプションについてはhttpstat内部で使用しているので指定できない。
$ httpstat httpbin.org/post -X POST --data-urlencode "a=b" -v

環境変数
ヘルプに書いてあるように下記環境変数を設定することで挙動が変わる。
| HTTPSTAT_SHOW_BODY | デフォルトはfalse。trueにするとレスポンスボディを表示 |
| HTTPSTAT_SHOW_IP | デフォルトはtrue。trueにするとリモート/ローカルのIPアドレス/ポートを表示 |
| HTTPSTAT_SHOW_SPEED | デフォルトはfalse。trueにするとダウンロード/アップロードスピードを表示 |
| HTTPSTAT_SAVE_BODY | デフォルトはtrue。trueにするとレスポンスボディをtmpファイルに保存 |
| HTTPSTAT_CURL_BIN | デフォルトは$PATH。curlコマンドのパスを指定 |
| HTTPSTAT_DEBUG | デフォルトはfalse。trueにするとデバッグログを表示 |
curlコマンドで実行する場合
httpstatはcurlコマンドをラップしたものなのでcurlで何をしているかを調べてみる。
ソースコードを見ると、
cmd_core = [curl_bin, '-w', curl_format, '-D', headerf.name, '-o', bodyf.name, '-s', '-S'] cmd = cmd_core + curl_args + [url]
のようになっており、実際にコマンドを出力した結果が下記。
curl -w '{ "time_namelookup": %{time_namelookup}, "time_connect": %{time_connect}, "time_appconnect": %{time_appconnect}, "time_pretransfer": %{time_pretransfer}, "time_redirect": %{time_redirect}, "time_starttransfer": %{time_starttransfer}, "time_total": %{time_total}, "speed_download": %{speed_download}, "speed_upload": %{speed_upload}, "remote_ip": "%{remote_ip}", "remote_port": "%{remote_port}", "local_ip": "%{local_ip}", "local_port": "%{local_port}" }' -D /tmp/tmps06u93gu -o /tmp/tmp9nzcic0w -s -S https://google.co.jp
-wオプションで出力フォーマットを指定している。
-w, --write-out FORMAT What to output after completion
実行結果は以下のようになり、この数値から処理時間を算出しているようだ。
{ "time_namelookup": 0.131, "time_connect": 0.455, "time_appconnect": 0.642, "time_pretransfer": 0.643, "time_redirect": 0.000, "time_starttransfer": 0.721, "time_total": 0.721, "speed_download": 307.000, "speed_upload": 0.000, "remote_ip": "172.217.24.131", "remote_port": "443", "local_ip": "10.0.2.15", "local_port": "49698" }%
各項目の意味は下記ブログを参照。
