本文翻译自: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. 出于我们的目的,我们将仅关注所提供的时序细节。
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
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 connection
, TTFB: time to first byte
total time
和total 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