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

本文介绍了如何使用cURL在Linux和Windows上测量HTTP请求和响应时间的详细步骤,包括创建格式化输出文件和Windows快捷方式,以及提供不同选项以获取所需的时间指标。
摘要由CSDN通过智能技术生成

本文翻译自: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. 我有一个Web服务,它接收JSON格式的数据,处理数据,然后将结果返回给请求者。

I want to measure the request, response, and total time using cURL . 我想使用cURL测量请求,响应和总时间。

My example request looks like: 我的示例请求如下:

curl -X POST -d @file server:port

and I currently measure this using the time command in Linux: 我目前使用Linux中的time命令测量它:

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 ? 有没有办法使用cURL来衡量请求和响应时间?


#1楼

参考:https://stackoom.com/question/1EQeb/如何使用cURL一次测量请求和响应时间


#2楼

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 . -w一起使用的所有变量都可以在man curl找到。


#3楼

From this brilliant blog post... https://blog.josephscott.org/2011/10/14/timing-details-with-curl/ 从这篇精彩的博客文章... 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> ). cURL支持格式化输出以获取请求的详细信息( 有关详细信息 ,请参阅cURL联机帮助页 ,在-w, –write-out <format> )。 For our purposes we'll focus just on the timing details that are provided. 出于我们的目的,我们将仅关注所提供的时序细节。

  1. Create a new file, curl-format.txt, and paste in: 创建一个新文件curl-format.txt并粘贴到:

      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 
  2. Make a request: 发出请求:

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

    Or on Windows, it's... 或者在Windows上,它是......

     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 -w "@curl-format.txt"告诉cURL使用我们的格式文件
-o /dev/null redirects the output of the request to /dev/null -o /dev/null将请求的输出重定向到/ dev / null
-s tells cURL not to show a progress meter -s告诉cURL不要显示进度表
"http://wordpress.com/" is the URL we are requesting. "http://wordpress.com/"是我们要求的网址。 Use quotes particularly if your URL has "&" query string parameters 如果您的URL具有“&”查询字符串参数,请使用引号


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) 创建Windows快捷方式(也称为BAT文件)

Put this command in CURLTIME.BAT (in the same folder as curl.exe) 将此命令放在CURLTIME.BAT中(与curl.exe位于同一文件夹中)

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

Then you can simply call... 然后你可以简单地打电话......

curltime wordpress.org

#4楼

Option 1. To measure total time : 选项1.测量total time

curl -o /dev/null -s -w 'Total: %{time_total}s\n'  https://www.google.com

Sample output: 样本输出:

在此输入图像描述

Option 2. To get time to establish connection , TTFB: time to first byte and total time : 选项2.为了有time to establish connectionTTFB: time to first byte total timetotal time

curl -o /dev/null -s -w 'Establish Connection: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n'  https://www.google.com

Sample output: 样本输出:

在此输入图像描述

Ref: Get response time with curl 参考: 获得卷曲的响应时间


#5楼

A shortcut you can add to your .bashrc etc, based on other answers here: 您可以根据此处的其他答案添加到.bashrc等的快捷方式:

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

#6楼

I made a friendly formatter for sniffing curl requests to help with debugging ( see comments for usage ). 我做了一个友好的格式化程序来嗅探curl请求以帮助调试(参见使用注释)。 It contains's every known output parameter you can write out in an easy to read format. 它包含您可以用易于阅读的格式写出的每个已知输出参数。

https://gist.github.com/manifestinteractive/ce8dec10dcb4725b8513 https://gist.github.com/manifestinteractive/ce8dec10dcb4725b8513

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值