How do I measure request and response times at once using cURL?

I have a web service that receives data in JSON format, processes the data, and then returns the result to the requester.

I want to measure the request, response, and total time using cURL .

My example request looks like:

curl -X POST -d @file server:port

and I currently measure this using the time command in Linux:

time curl -X POST -d @file server:port

The time command only measures total time, though - which isn't quite what I am looking for.

Is there any way to measure request and response times using cURL ?


From this brilliant blog post... https://blog.josephscott.org/2011/10/14/timing-details-with-curl/

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

  • Create a new file, curl-format.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
    
  • Make a request:

    curl -w "@curl-format.txt" -o /dev/null -s "http://wordpress.com/"
    

    Or on Windows, it's...

    curl -w "@curl-format.txt" -o NUL -s "http://wordpress.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. Use quotes particularly if your URL has "&" query string parameters


    And here is what you get back:

       time_namelookup:  0.001
          time_connect:  0.037
       time_appconnect:  0.000
      time_pretransfer:  0.037
         time_redirect:  0.000
    time_starttransfer:  0.092
                       ----------
            time_total:  0.164
    


    Make a Windows shortcut (aka BAT file)

    Put this command in CURLTIME.BAT (in the same folder as curl.exe)

    curl -w "@%~dp0curl-format.txt" -o NUL -s %*
    

    Then you can simply call...

    curltime wordpress.org
    

    Here is the answer:

    curl -X POST -d @file server:port -w %{time_connect}:%{time_starttransfer}:%{time_total}
    

    All of the variables used with -w can be found in man curl .


    A shortcut you can add to your .bashrc etc, based on other answers here:

    function perf {
      curl -o /dev/null -s -w "%{time_connect} + %{time_starttransfer} = %{time_total}n" "$1"
    }
    

    Usage:

    > perf stackoverflow.com
    0.521 + 0.686 = 1.290
    
    链接地址: http://www.djcxy.com/p/48790.html

    上一篇: 使用php和cURL支持多部分表单数据

    下一篇: 如何使用cURL一次性测量请求和响应时间?