最新消息:

Timing Details With cURL

curl / linux命令 admin 4浏览

Jon’s recent Find the Time to First Byte Using Curl post reminded me about the additional timing details that cURL can provide.

cURL supports formatted output for the details of the request ( see the cURL manpage for details, under “-w, –write-out ” ). For our purposes we’ll focus just on the timing details that are provided.

Step one: create a new file, curl.txt, and paste in:

            time_namelookup:  %{time_namelookup}n
               time_connect:  %{time_connect}n
            time_appconnect:  %{time_appconnect}n
           time_pretransfer:  %{time_pretransfer}n
              time_redirect:  %{time_redirect}n
         time_starttransfer:  %{time_starttransfer}n
                            ----------n
                 time_total:  %{time_total}n
               content_type:  %{content_type}n
         filename_effective:  %{filename_effective}n
                  http_code:  %{http_code}n
             ftp_entry_path:  %{ftp_entry_path}n
               http_connect:  %{http_connect}n
                   local_ip:  %{local_ip}n
                 local_port:  %{local_port}n
               num_connects:  %{num_connects}n
              num_redirects:  %{num_redirects}n
               redirect_url:  %{redirect_url}n
                  remote_ip:  %{remote_ip}n
                remote_port:  %{remote_port}n
              size_download:  %{size_download}n
                size_header:  %{size_header}n
               size_request:  %{size_request}n
                size_upload:  %{size_upload}n
             speed_download:  %{speed_download}n
               speed_upload:  %{speed_upload}n
          ssl_verify_result:  %{ssl_verify_result}n
              url_effective:  %{url_effective}n

Step two, make a request:

curl -w "@curl.txt" -o /dev/null -s http://www.aikaiyuan.com/

What this does:

  • -w "@curl-format.txt" tells cURL to use our format file
  • -o /dev/null redirects the output of the request to /dev/null
  • -s tells cURL not to show a progress meter
  • http://wordpress.com/ is the URL we are requesting

And here is what you get back:

            time_namelookup:  0.007
               time_connect:  0.053
            time_appconnect:  0.000
           time_pretransfer:  0.053
              time_redirect:  0.000
         time_starttransfer:  0.630
                            ----------
                 time_total:  1.227
               content_type:  text/html; charset=UTF-8
curl: unknown --write-out variable: 'filename_effective'
         filename_effective:
                  http_code:  200
             ftp_entry_path:
               http_connect:  000
curl: unknown --write-out variable: 'local_ip'
                   local_ip:
curl: unknown --write-out variable: 'local_port'
                 local_port:
               num_connects:  1
              num_redirects:  0
               redirect_url:
curl: unknown --write-out variable: 'remote_ip'
                  remote_ip:
curl: unknown --write-out variable: 'remote_port'
                remote_port:
              size_download:  206721
                size_header:  361
               size_request:  170
                size_upload:  0
             speed_download:  168497.000
               speed_upload:  0.000
          ssl_verify_result:  0
              url_effective:  http://www.aikaiyuan.com/

Jon was looking specifically at time to first byte, which is the time_starttransfer line. The other timing details include DNS lookup, TCP connect, pre-transfer negotiations, redirects (in this case there were none), and of course the total time.

The format file for this output provides a reasonable level of flexibility, for instance you could make it CSV formatted for easy parsing. You might want to do that if you were running this as a cron job to track timing details of a specific URL.

For details on the other information that cURL can provide using -wcheck out the cURL manpage.

转载请注明:爱开源 » Timing Details With cURL

转载请注明:爱开源 » Timing Details With cURL