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

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

我的请求示例如下:

curl -X POST -d @file server:port

我现在用Linux中的time命令来测量:

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

然而,time命令只测量总时间——这并不是我想要的。

有什么方法可以使用cURL来测量请求和响应时间吗?


当前回答

下面是可以使用-w的字符串,包含curl -w支持的所有选项。

{"contentType":"%{content_type}","filenameEffective":"%{filename_effective}","ftpEntryPath":"%{ftp_entry_path}","httpCode":"%{http_code}","httpConnect":"%{http_connect}","httpVersion":"%{http_version}","localIp":"%{local_ip}","localPort":"%{local_port}","numConnects":"%{num_connects}","numRedirects":"%{num_redirects}","proxySslVerifyResult":"%{proxy_ssl_verify_result}","redirectUrl":"%{redirect_url}","remoteIp":"%{remote_ip}","remotePort":"%{remote_port}","scheme":"%{scheme}","size":{"download":"%{size_download}","header":"%{size_header}","request":"%{size_request}","upload":"%{size_upload}"},"speed":{"download":"%{speed_download}","upload":"%{speed_upload}"},"sslVerifyResult":"%{ssl_verify_result}","time":{"appconnect":"%{time_appconnect}","connect":"%{time_connect}","namelookup":"%{time_namelookup}","pretransfer":"%{time_pretransfer}","redirect":"%{time_redirect}","starttransfer":"%{time_starttransfer}","total":"%{time_total}"},"urlEffective":"%{url_effective}"}

输出JSON。

其他回答

一个你可以添加到你的.bashrc etc的快捷方式,基于这里的其他答案:

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

用法:

> perf stackoverflow.com
0.521 + 0.686 = 1.290

以下是答案:

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

所有与-w一起使用的变量都可以在man curl中找到。

测量命令执行时间的通用解决方案

Linux / Mac OS / Unix

在Linux上,您可以使用time命令来测量执行一个命令所花费的时间

time curl https://en.wikipedia.org/wiki/Cat 

输出:


ect...
</body>
</html>

real    0m0.565s
user    0m0.011s
sys     0m0.024s

这里的总时间/响应时间是真实时间。

这个解决方案是通用的,应该适用于任何Unix命令

time sleep 5

输出:

real    0m5.001s
user    0m0.001s
sys 0m0.000s

视窗电源外壳

您可以使用测量命令

另一种方法是配置~/。像这样卷

-w "\n\n==== cURL measurements stats ====\ntotal: %{time_total} seconds \nsize: %{size_download} bytes \ndnslookup: %{time_namelookup} seconds \nconnect: %{time_connect} seconds \nappconnect: %{time_appconnect} seconds \nredirect: %{time_redirect} seconds \npretransfer: %{time_pretransfer} seconds \nstarttransfer: %{time_starttransfer} seconds \ndownloadspeed: %{speed_download} byte/sec \nuploadspeed: %{speed_upload} byte/sec \n\n"

所以旋度的输出是

❯❯ curl -I https://google.com
HTTP/2 301
location: https://www.google.com/
content-type: text/html; charset=UTF-8
date: Mon, 04 Mar 2019 08:02:43 GMT
expires: Wed, 03 Apr 2019 08:02:43 GMT
cache-control: public, max-age=2592000
server: gws
content-length: 220
x-xss-protection: 1; mode=block
x-frame-options: SAMEORIGIN
alt-svc: quic=":443"; ma=2592000; v="44,43,39"



==== cURL measurements stats ====
total: 0.211117 seconds
size: 0 bytes
dnslookup: 0.067179 seconds
connect: 0.098817 seconds
appconnect: 0.176232 seconds
redirect: 0.000000 seconds
pretransfer: 0.176438 seconds
starttransfer: 0.209634 seconds
downloadspeed: 0.000 byte/sec
uploadspeed: 0.000 byte/sec

下面是一个Bash单行程序,可以重复访问同一个服务器:

for i in {1..1000}; do curl -s -o /dev/null -w "%{time_total}\n" http://server/get_things; done