我有一个web服务,它接收JSON格式的数据,处理数据,然后将结果返回给请求者。
我想使用cURL测量请求、响应和总时间。
我的请求示例如下:
curl -X POST -d @file server:port
我现在用Linux中的time命令来测量:
time curl -X POST -d @file server:port
然而,time命令只测量总时间——这并不是我想要的。
有什么方法可以使用cURL来测量请求和响应时间吗?
我有一个web服务,它接收JSON格式的数据,处理数据,然后将结果返回给请求者。
我想使用cURL测量请求、响应和总时间。
我的请求示例如下:
curl -X POST -d @file server:port
我现在用Linux中的time命令来测量:
time curl -X POST -d @file server:port
然而,time命令只测量总时间——这并不是我想要的。
有什么方法可以使用cURL来测量请求和响应时间吗?
当前回答
Hey比Apache Bench好,有更少的SSL问题
./hey https://google.com -more
Summary:
Total: 3.0960 secs
Slowest: 1.6052 secs
Fastest: 0.4063 secs
Average: 0.6773 secs
Requests/sec: 64.5992
Response time histogram:
0.406 [1] |
0.526 [142] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
0.646 [1] |
0.766 [6] |∎∎
0.886 [0] |
1.006 [0] |
1.126 [0] |
1.246 [12] |∎∎∎
1.365 [32] |∎∎∎∎∎∎∎∎∎
1.485 [5] |∎
1.605 [1] |
Latency distribution:
10% in 0.4265 secs
25% in 0.4505 secs
50% in 0.4838 secs
75% in 1.2181 secs
90% in 1.2869 secs
95% in 1.3384 secs
99% in 1.4085 secs
Details (average, fastest, slowest):
DNS+dialup: 0.1150 secs, 0.0000 secs, 0.4849 secs
DNS-lookup: 0.0032 secs, 0.0000 secs, 0.0319 secs
req write: 0.0001 secs, 0.0000 secs, 0.0007 secs
resp wait: 0.2068 secs, 0.1690 secs, 0.4906 secs
resp read: 0.0117 secs, 0.0011 secs, 0.2375 secs
Status code distribution:
[200] 200 responses
参考文献
https://github.com/rakyll/hey
其他回答
下面是可以使用-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。
从这篇精彩的博客文章…https://blog.josephscott.org/2011/10/14/timing-details-with-curl/
cURL支持请求的详细信息的格式化输出(详细信息请参见cURL手册,在-w, -write-out <format>下)。出于我们的目的,我们将只关注所提供的时间细节。以下时间以秒为单位。
Create a new file, curl-format.txt, and paste in: time_namelookup: %{time_namelookup}s\n time_connect: %{time_connect}s\n time_appconnect: %{time_appconnect}s\n time_pretransfer: %{time_pretransfer}s\n time_redirect: %{time_redirect}s\n time_starttransfer: %{time_starttransfer}s\n ----------\n time_total: %{time_total}s\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/"
它的作用:
-w "@curl-format.txt"告诉cURL使用我们的格式文件 -o /dev/null将请求的输出重定向到/dev/null - s 告诉cURL不要显示进度表 “http://wordpress.com/”是 我们正在请求的URL。使用引号,特别是当你的URL有“&”查询字符串参数时
这就是你得到的结果:
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 "
那么你可以简单地打电话给…
curltime wordpress.org
感谢评论者Pete Doyle!
编写一个Linux/Mac独立脚本
这个脚本不需要一个单独的.txt文件来包含格式化。
创建一个新的文件,curltime,在你的可执行路径的某个地方,并粘贴:
#!/bin/bash
curl -w @- -o /dev/null -s "$@" <<'EOF'
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
EOF
然后像别名一样调用它:
curltime wordpress.org
制作一个Windows快捷方式(即BAT文件)
在与curl.exe和curl-format.txt相同的文件夹中创建一个名为curltime.bat的新文本文件,并粘贴到下面一行:
curl -w "@%~dp0curl-format.txt" -o NUL -s %*
然后在命令行中,你可以简单地调用:
curltime wordpress.org
(确保该文件夹列在Windows PATH变量中,以便能够从任何文件夹中使用该命令。)
Hey比Apache Bench好,有更少的SSL问题
./hey https://google.com -more
Summary:
Total: 3.0960 secs
Slowest: 1.6052 secs
Fastest: 0.4063 secs
Average: 0.6773 secs
Requests/sec: 64.5992
Response time histogram:
0.406 [1] |
0.526 [142] |∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎∎
0.646 [1] |
0.766 [6] |∎∎
0.886 [0] |
1.006 [0] |
1.126 [0] |
1.246 [12] |∎∎∎
1.365 [32] |∎∎∎∎∎∎∎∎∎
1.485 [5] |∎
1.605 [1] |
Latency distribution:
10% in 0.4265 secs
25% in 0.4505 secs
50% in 0.4838 secs
75% in 1.2181 secs
90% in 1.2869 secs
95% in 1.3384 secs
99% in 1.4085 secs
Details (average, fastest, slowest):
DNS+dialup: 0.1150 secs, 0.0000 secs, 0.4849 secs
DNS-lookup: 0.0032 secs, 0.0000 secs, 0.0319 secs
req write: 0.0001 secs, 0.0000 secs, 0.0007 secs
resp wait: 0.2068 secs, 0.1690 secs, 0.4906 secs
resp read: 0.0117 secs, 0.0011 secs, 0.2375 secs
Status code distribution:
[200] 200 responses
参考文献
https://github.com/rakyll/hey
以下是答案:
curl -X POST -d @file server:port -w %{time_connect}:%{time_starttransfer}:%{time_total}
所有与-w一起使用的变量都可以在man curl中找到。
下面是一个Bash单行程序,可以重复访问同一个服务器:
for i in {1..1000}; do curl -s -o /dev/null -w "%{time_total}\n" http://server/get_things; done