普段何気なく使っているcurlですが 便利なオプション(-w, –write-out)が意外と知られていないと思いましたので、紹介したいと思います。

curlの便利なオプション(-w,–write-out)について

curl –helpコマンドを実行してみると下記のようにFORMATを利用した出力であることが記載されています。詳細は、curl –manualで確認できます。

〜省略〜
 -w, --write-out <format> Use output FORMAT after completion
     --xattr         Store metadata in extended file attributes

curl –manualの出力結果

-w, --write-out <format>
       Make curl display information on stdout after a completed trans-
       fer. The format is a string that may contain  plain  text  mixed
       with  any  number of variables. The format can be specified as a
       literal "string", or you can have curl read the  format  from  a
       file  with  "@filename" and to tell curl to read the format from
       stdin you write "@-".

       The variables present in the output format will  be  substituted
       by  the  value or text that curl thinks fit, as described below.
       All variables are specified as %{variable_name} and to output  a
       normal  % you just write them as %%. You can output a newline by
       using \n, a carriage return with \r and a tab space with \t.

       The output will be written to standard output, but this  can  be
       switched to standard error by using %{stderr}.

       NOTE: The %-symbol is a special symbol in the win32-environment,
       where all occurrences of % must be doubled when using  this  op-
       tion.

       The variables available are:

       content_type   The  Content-Type  of  the requested document, if
                      there was any.

       filename_effective
                      The ultimate filename that curl  writes  out  to.
                      This  is only meaningful if curl is told to write
                      to a file  with  the  -O,  --remote-name  or  -o,
                      --output  option. It's most useful in combination
                      with the -J, --remote-header-name option.  (Added
                      in 7.26.0)

       ftp_entry_path The initial path curl ended up in when logging on
                      to the remote FTP server. (Added in 7.15.4)

       http_code      The numerical response code that was found in the
                      last  retrieved  HTTP(S)  or  FTP(s) transfer. In
                      7.18.2 the alias response_code was added to  show
                      the same info.

       http_connect   The numerical code that was found in the last re-
                      sponse (from a proxy) to a curl CONNECT  request.
                      (Added in 7.12.4)

       http_version   The  http  version  that  was  effectively  used.
                      (Added in 7.50.0)

       local_ip       The IP address of the local end of the  most  re-
                      cently  done  connection  - can be either IPv4 or
                      IPv6 (Added in 7.29.0)

       local_port     The local port number of the most  recently  done
                      connection (Added in 7.29.0)

       num_connects   Number  of new connects made in the recent trans-
                      fer. (Added in 7.12.3)

       num_redirects  Number of redirects that were followed in the re-
                      quest. (Added in 7.12.3)

       proxy_ssl_verify_result
                      The result of the HTTPS proxy's SSL peer certifi-
                      cate verification that was requested. 0 means the
                      verification was successful. (Added in 7.52.0)

       redirect_url   When an HTTP request was made without -L, --loca-
                      tion to follow redirects (or when --max-redir  is
                      met),  this  variable  will show the actual URL a
                      redirect would have gone to. (Added in 7.18.2)

       remote_ip      The remote IP address of the most  recently  done
                      connection - can be either IPv4 or IPv6 (Added in
                      7.29.0)

       remote_port    The remote port number of the most recently  done
                      connection (Added in 7.29.0)

       scheme         The  URL  scheme (sometimes called protocol) that
                      was effectively used (Added in 7.52.0)

       size_download  The total amount of bytes that were downloaded.

       size_header    The total amount of bytes of the downloaded head-
                      ers.

       size_request   The  total  amount of bytes that were sent in the
                      HTTP request.

       size_upload    The total amount of bytes that were uploaded.

       speed_download The average download speed that curl measured for
                      the complete download. Bytes per second.

       speed_upload   The  average  upload speed that curl measured for
                      the complete upload. Bytes per second.

       ssl_verify_result
                      The result of the SSL peer certificate  verifica-
                      tion that was requested. 0 means the verification
                      was successful. (Added in 7.19.0)

       stderr         From this point on, the  -w,  --write-out  output
                      will  be  written  to  standard  error. (Added in
                      7.63.0)

       stdout         From this point on, the  -w,  --write-out  output
                      will  be written to standard output.  This is the
                      default, but can be used  to  switch  back  after
                      switching to stderr.  (Added in 7.63.0)

       time_appconnect
                      The  time, in seconds, it took from the start un-
                      til the SSL/SSH/etc connect/handshake to the  re-
                      mote host was completed. (Added in 7.19.0)

       time_connect   The  time, in seconds, it took from the start un-
                      til the TCP connect to the remote host (or proxy)
                      was completed.

       time_namelookup
                      The  time, in seconds, it took from the start un-
                      til the name resolving was completed.

       time_pretransfer
                      The time, in seconds, it took from the start  un-
                      til  the  file  transfer was just about to begin.
                      This includes all pre-transfer commands and nego-
                      tiations that are specific to the particular pro-
                      tocol(s) involved.

       time_redirect  The time, in seconds, it took for all redirection
                      steps including name lookup, connect, pretransfer
                      and transfer before  the  final  transaction  was
                      started.  time_redirect shows the complete execu-
                      tion time for multiple  redirections.  (Added  in
                      7.12.3)

       time_starttransfer
                      The  time, in seconds, it took from the start un-
                      til the first byte was just about  to  be  trans-
                      ferred.  This  includes time_pretransfer and also
                      the time the server needed to calculate  the  re-
                      sult.

       time_total     The  total time, in seconds, that the full opera-
                      tion lasted.

       url_effective  The URL that was fetched last. This is most mean-
                      ingful  if  you've  told curl to follow location:
                      headers.

利用してみる

出力フォーマットを指定したファイル(curl_env.txt)を作成します。

url_effective\t\t: %{url_effective}\n
http_code\t\t: %{http_code}\n
http_connect\t\t: %{http_connect}\n
time_total\t\t: %{time_total}\n
time_namelookup\t\t: %{time_namelookup}\n
time_connect\t\t: %{time_connect}\n
time_appconnect\t\t: %{time_appconnect}\n
time_pretransfer\t\t: %{time_pretransfer}\n
time_redirect\t\t: %{time_redirect}\n
time_starttransfer\t\t: %{time_starttransfer}\n
size_download\t\t: %{size_download}\n
size_upload\t\t: %{size_upload}\n
size_header\t\t: %{size_header}\n
size_request\t\t: %{size_request}\n
speed_download\t\t: %{speed_download}\n
speed_upload\t\t: %{speed_upload}

フォーマットファイルは、下記のように指定します。

curl -o /dev/null https://targetsite.com -w @curl_env.txt -s 

実行結果です。この例では、time_namelookupに時間がかかっているのでDNSの名前解決で時間がかかっているということがわかります。

url_effective           : http://xxxxx
http_code               : 200
http_connect            : 000
time_total              : 5.130927
time_namelookup         : 5.096498
time_connect            : 5.096648
time_appconnect         : 0.000000
time_pretransfer                : 5.096674
time_redirect           : 0.000000
time_starttransfer              : 5.130713
size_download           : 12978
size_upload             : 0
size_header             : 314
size_request            : 88
speed_download          : 2529.000
speed_upload            : 0.000⏎

今回は、curlのwオプションを利用したボトルネック調査について記載しました。お疲れ様でした。