如何使用 cURL 一次测量请求和响应时间? [英] How do I measure request and response times at once using cURL?

查看:22
本文介绍了如何使用 cURL 一次测量请求和响应时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Web 服务,它接收 JSON 格式的数据,处理数据,然后将结果返回给请求者.

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

我想使用 cURL 测量请求、响应和总时间.

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

我的示例请求如下所示:

My example request looks like:

curl -X POST -d @file server:port

我目前在 Linux 中使用 time 命令来测量这个:

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.

有没有办法使用 cURL 来测量请求和响应时间?

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

推荐答案

来自这篇精彩的博文... https://blog.josephscott.org/2011/10/14/timing-details-with-curl/

cURL 支持请求详细信息的格式化输出(有关详细信息,请参阅 cURL 联机帮助页,在 -w 下,–write-out ).出于我们的目的,我们将只关注提供的时间细节.以下时间以为单位.

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. Times below are in seconds.

  1. 创建一个新文件 curl-format.txt,然后粘贴:

     time_namelookup:  %{time_namelookup}s

        time_connect:  %{time_connect}s

     time_appconnect:  %{time_appconnect}s

    time_pretransfer:  %{time_pretransfer}s

       time_redirect:  %{time_redirect}s

  time_starttransfer:  %{time_starttransfer}s

                     ----------

          time_total:  %{time_total}s

  • 提出请求:

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

  • 或者在 Windows 上,它是...

    Or on Windows, it's...

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


    这是做什么的:

    -w "@curl-format.txt" 告诉 cURL 使用我们的格式文件
    -o/dev/null 将请求的输出重定向到/dev/null
    -s告诉 cURL 不要显示进度表
    "http://wordpress.com/" 是我们请求的 URL.尤其是当您的 URL 包含&"时,请使用引号.查询字符串参数


    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

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


    制作 Linux/Mac 快捷方式(别名)

    alias curltime="curl -w "@$HOME/.curl-format.txt" -o /dev/null -s "
    

    然后你可以简单地调用...

    Then you can simply call...

    curltime wordpress.org
    

    感谢评论者 Pete Doyle!

    此脚本不需要单独的 .txt 文件来包含格式.

    This script does not require a separate .txt file to contain the formatting.

    在可执行路径中的某处创建一个新文件 curltime,然后粘贴:

    Create a new file, curltime, somewhere in your executable path, and paste in:

    #!/bin/bash
    
    curl -w @- -o /dev/null -s "$@" <<'EOF'
        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}
    
    EOF
    

    与别名相同的调用方式:

    Call the same way as the alias:

    curltime wordpress.org
    


    制作 Windows 快捷方式(又名 BAT 文件)

    将此命令放在 CURLTIME.BAT 中(与 curl.exe 在同一文件夹中)


    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
    

    这篇关于如何使用 cURL 一次测量请求和响应时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆