LoginSignup
3
2

More than 1 year has passed since last update.

curlコマンドの--write-out オプションでレスポンスタイムを表示

Posted at

curlコマンドの-w--write-out) オプションでレスポンスタイムを表示してみたので、その内容を紹介します。

1. 環境

  • CentOS Linux release 8.5.2111
  • curl 7.61.1
[root@centos8 ~]# cat /etc/redhat-release
CentOS Linux release 8.5.2111
[root@centos8 ~]#
[root@centos8 ~]# curl --version
curl 7.61.1 (x86_64-redhat-linux-gnu) libcurl/7.61.1 OpenSSL/1.1.1k zlib/1.2.11 brotli/1.0.6 libidn2/2.2.0 libpsl/0.20.2 (+libidn2/2.2.0) libssh/0.9.4/openssl/zlib nghttp2/1.33.0
Release-Date: 2018-09-05
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz brotli TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL
[root@centos8 ~]#

2. -w--write-out)オプション

-w--write-out)オプションはフォーマットとして文字列を指定します。文字列の中に%{variable_name}を埋め込むと、その部分に値が展開されます。
variable_nameの内容についてはman curlで確認できます。また、「curl - How To Use」にも記載があります。

その中で、レスポンスタイムに関係があるvariable_nameは以下となります。

変数名 内容
time_namelookup DNSの名前解決が完了するまでにかかった時間(秒単位)
time_connect TCP等のコネクションの確立が完了するまでにかかった時間(秒単位)
time_appconnect TLS等のハンドシェイクが完了するまでにかかった時間(秒単位)
time_pretransfer データ転送が開始されるまでにかかった時間(秒単位)
time_starttransfer サーバーからレスポンスとして最初のバイトが転送されるまでにかかった時間(秒単位)
time_total 全体の処理にかかった時間(秒単位)
http_code HTTPステータスコード

3. レスポンスタイムを表示

まずは以下のコマンドでcurlコマンドを実行して見ます。
curl https://qiita.com/

実行結果
[root@centos8 ~]# curl https://qiita.com/
<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8" /><title>Qiita</title><meta content="Qiitaは、エンジニアに関する知識を記録・共有するためのサービスです。 プログラミングに関するTips、ノウハウ、メモを簡単に記録 &amp;amp; 公開することができます。" name="description" /><meta content="width=device-width,initial-scale=1,shrink-to-fit=no,viewport-fit=cover" name="viewport" /><meta content="#55c500" name="theme-color" /><meta content="XWpkTG32-_C4joZoJ_UsmDUi-zaH-hcrjF6ZC_FoFbk" name="google-site-verification" /><meta content="telephone=no" name="format-detection" /><link rel="canonical" href="https://qiita.com/" /><link href="/manifest.json" rel="manifest" /><link href="/opensearch.xml" rel="search" title="Qiita" type="application/opensearchdescription+xml" /><link as="script" href="https://www.googletagservices.com/tag/js/gpt.js" rel="preload" /><link href="https://securepubads.g.doubleclick.net" rel="preconnect" /><script async="" src="https://www.googletagservices.com/tag/js/gpt.js"></script><meta name="csrf-param" content="authenticity_token" />

~~ 省略 ~~

</div><div id="dataContainer" style="display: none;" data-config="{&quot;actionPath&quot;:&quot;public/home#index&quot;,&quot;settings&quot;:{&quot;analyticsTrackingId&quot;:&quot;UA-24675221-12&quot;,&quot;assetsMap&quot;:{},&quot;csrfToken&quot;:&quot;oXri3277ogmm0aCc3pcZ12iafWdJuvhPhTcSMYq4f3WdlEIPVeEeDOYCRl7Z2gWIlET0rgF3Qe4S7YBIu4Hw2g==&quot;,&quot;locale&quot;:&quot;en&quot;},&quot;currentUser&quot;:null}" /></body></html><script type="application/json" data-js-react-on-rails-store="AppStoreWithReactOnRails">{"snackbar":{"type":"","body":"","isActive":false}}</script>[root@centos8 ~]#

では実際に-wオプションで time_total 以下の情報を出力しています。

curl -w"time_total: %{time_total}\n" https://qiita.com/

実行結果
[root@centos8 ~]# curl -w"time_total: %{time_total}\n" https://qiita.com/
<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8" /><title>Qiita</title><meta content="Qiitaは、エンジニアに関する知識を記録・共有するためのサービスです。 プログラミングに関するTips、ノウハウ、メモを簡単に記録 &amp;amp; 公開することができます。" name="description" /><meta content="width=device-width,initial-scale=1,shrink-to-fit=no,viewport-fit=cover" name="viewport" /><meta content="#55c500" name="theme-color" /><meta content="XWpkTG32-_C4joZoJ_UsmDUi-zaH-hcrjF6ZC_FoFbk" name="google-site-verification" /><meta content="telephone=no" name="format-detection" /><link rel="canonical" href="https://qiita.com/" /><link href="/manifest.json" rel="manifest" /><link href="/opensearch.xml" rel="search" title="Qiita" type="application/opensearchdescription+xml" /><link as="script" href="https://www.googletagservices.com/tag/js/gpt.js" rel="preload" /><link href="https://securepubads.g.doubleclick.net" rel="preconnect" /><script async="" src="https://www.googletagservices.com/tag/js/gpt.js"></script><meta name="csrf-param" content="authenticity_token" />

~~ 省略 ~~

</div><div id="dataContainer" style="display: none;" data-config="{&quot;actionPath&quot;:&quot;public/home#index&quot;,&quot;settings&quot;:{&quot;analyticsTrackingId&quot;:&quot;UA-24675221-12&quot;,&quot;assetsMap&quot;:{},&quot;csrfToken&quot;:&quot;soZ43VXMJS6CAoX0CDhUH0d7nOVokt8AkC1yfG0/auQuUmty+7BkWorYvsH4IQBT1a0cDMj92W2zNvyp3i95Ow==&quot;,&quot;locale&quot;:&quot;en&quot;},&quot;currentUser&quot;:null}" /></body></html><script type="application/json" data-js-react-on-rails-store="AppStoreWithReactOnRails">{"snackbar":{"type":"","body":"","isActive":false}}</script>time_total: 1.082811
[root@centos8 ~]#

レスポンスボディを表示したくない場合、-oオプションで/dev/nullを指定します。

curl -w"time_total: %{time_total}\n" -o /dev/null https://qiita.com/

実行結果
[root@centos8 ~]# curl -w"time_total: %{time_total}\n" -o /dev/null https://qiita.com/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  182k    0  182k    0     0   634k      0 --:--:-- --:--:-- --:--:--  632k
time_total: 0.287346
[root@centos8 ~]#

進捗を表示したくない場合、-sオプションを指定します。(進捗は表示したくないがエラーは表示したい場合、-sSオプションを指定します。)

curl -w"time_total: %{time_total}\n" -o /dev/null -s https://qiita.com/

実行結果
[root@centos8 ~]# curl -w"time_total: %{time_total}\n" -o /dev/null -s https://qiita.com/
time_total: 0.403769
[root@centos8 ~]#

最後に以下のパラメータを出力してみます。

  • http_code
  • time_namelookup
  • time_connect
  • time_appconnect
  • time_pretransfer
  • time_starttransfer
  • time_total

curl -w"http_code: %{http_code}\ntime_namelookup: %{time_namelookup}\ntime_connect: %{time_connect}\ntime_appconnect: %{time_appconnect}\ntime_pretransfer: %{time_pretransfer}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" -o /dev/null -s https://qiita.com/
\nは改行です。)

実行結果
[root@centos8 ~]# curl -w"http_code: %{http_code}\ntime_namelookup: %{time_namelookup}\ntime_connect: %{time_connect}\ntime_appconnect: %{time_appconnect}\ntime_pretransfer: %{time_pretransfer}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" -o /dev/null -s https://qiita.com/
http_code: 200
time_namelookup: 0.013797
time_connect: 0.020571
time_appconnect: 0.053383
time_pretransfer: 0.053800
time_starttransfer: 0.234969
time_total: 0.248941
[root@centos8 ~]#

4. 参考

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2