太长不看版

curl -s -w "%{time_total}\n" -o /dev/null http://www.github.com/
0.106368

可以看到返回是0.106368秒。解释一下这几个参数:

  • -s, --silent: 让 curl 保持静默模式,不会输出进度条
  • -w "%{time_total\n}":输出请求的总时间
  • -o /dev/null: 这个参数用来隐藏 response 的内容

原理

这里是curl关于-w参数的文档;输出curl请求的具体时间,其实是用到了-w参数;这个参数提供了很多请求中的信息,作为变量用于格式化输出(详见下面的可用的--write-out变量表格)。

使用方式

这里是使用它的三种方式:

  • 通过纯文本传递
    curl -w "formatted string" http://example.com/
    
  • 通过文件读取
    curl -w "formatted string" http://example.com/
    
  • 通过stdin读取(跟读取文件类似,不过-在这里代表stdin
    curl -w @- http://example.com/
    

在由你自己定义的输出格式中,可以使用%{variable_name}的方式来访问可用的变量;如果要输出%字符,需要转义成%%;您还可以使用 、回车符和制表符空格来输出换行符。

回看上面的例子:

curl -w "%{time_total}\n" http://www.github.com/

这里就是简单地输出了一个time_total的变量值。

如果要输出HTTP Response Header

在字符串中使用%header{name} ,其中 name 是不区分大小写的标头名称(不带尾随冒号)。

curl -w "Server: %header{server}\n" http://example.com

cURL -w支持的所有变量

某些变量在老的curl版本中不可用,请注意。

VariableDescription 描述
certsOutputs the certificate chain from the most recent TLS handshake - with details. (Introduced in 7.88.0)输出最近 TLS 握手的证书链 - 包含详细信息。 (7.88.0中引入)
content_typeContent-Type of the requested document, if there was any.所请求文档的内容类型(如果有)。
errormsgError message from the transfer. Empty if no error occurred. (Introduced in 7.75.0)来自传输的错误消息。如果没有发生错误则为空。 (7.75.0 中引入)
exitcodeNumerical exit code from the transfer. 0 if no error occurred. (Introduced in 7.75.0)传输的数字退出代码。如果没有发生错误则为 0。 (7.75.0 中引入)
filename_effectiveThe ultimate filename that curl writes out to. Practical if curl is told to write to a file with the --remote-name or --output option. It’s most useful in combination with the --remote-header-name option.curl 写入的最终文件名。如果告诉curl 使用 --remote-name--output 选项写入文件,则实用。它与 --remote-header-name 选项结合使用时最有用。
ftp_entry_pathThe initial path curl ended up in when logging on to the remote FTP server.登录远程 FTP 服务器时,curl 的初始路径结束。
http_codeThe former variable name for what is now known as response_code.以前的变量名称现在称为 response_code
http_connectthe numerical code that was found in the last response (from a proxy) to a curl CONNECT request.在对curl CONNECT 请求的最后响应(来自代理)中找到的数字代码。
http_versionThe HTTP version that was used.使用的 HTTP 版本。
jsonall write-out variables as a single JSON object. (Introduced in 7.72.0)所有写出变量作为单个 JSON 对象。 (7.72.0中引入)
local_ipIP address of the local end of the most recently used connection - can be either IPv4 or IPv6最近使用的连接的本地端的 IP 地址 - 可以是 IPv4 或 IPv6
local_portLocal port number of the most recently used connection最近使用的连接的本地端口号
methodHTTP method the most recent request used最近请求使用的 HTTP 方法
num_certsNumber of the certificates in the most recent TLS handshake. (Introduced in 7.88.0)最近 TLS 握手中的证书数量。 (7.88.0中引入)
num_connectsNumber of new connects made in the recent transfer.最近传输中建立的新连接数。
num_headersNumber of response headers in the last response上次响应中响应头的数量
num_redirectsNumber of redirects that were followed in the request.请求中遵循的重定向数。
onerrorIf the transfer ended with an error, show the rest of the string, otherwise stop here. (Introduced in 7.75.0)如果传输因错误而结束,则显示字符串的其余部分,否则在此停止。 (7.75.0 中引入)
proxy_ssl_verify_resultThe result of the SSL peer certificate verification that was requested when communicating with a proxy. 0 means the verification was successful.与代理通信时请求的 SSL 对等证书验证的结果。 0表示验证成功。
redirect_urlThe actual URL a redirect would take you to when an HTTP request was made without -L to follow redirects.当发出 HTTP 请求而没有 -L 遵循重定向时,重定向将带您到达的实际 URL。
remote_ipThe remote IP address of the most recently used connection — can be either IPv4 or IPv6.最近使用的连接的远程 IP 地址 — 可以是 IPv4 或 IPv6。
remote_portThe remote port number of the most recently made connection.最近建立的连接的远程端口号。
response_codeThe numerical response code that was found in the last transfer.在上次传输中找到的数字响应代码。
schemescheme used in the previous URL上一个 URL 中使用的方案
size_downloadTotal number of bytes that were downloaded.下载的总字节数。
size_headerTotal number of bytes of the downloaded headers.下载标头的总字节数。
size_requestTotal number of bytes that were sent in the HTTP request.HTTP 请求中发送的字节总数。
size_uploadTotal number of bytes that were uploaded.上传的总字节数。
speed_downloadAverage download speed that curl measured for the complete download in bytes per second.curl 测量的完整下载的平均下载速度(以字节/秒为单位)。
speed_uploadAverage upload speed that curl measured for the complete upload in bytes per second.curl 测量的完整上传的平均上传速度(以字节/秒为单位)。
ssl_verify_resultthe result of the SSL peer certificate verification that was requested. 0 means the verification was successful.所请求的 SSL 对等证书验证的结果。 0表示验证成功。
stderrMakes the rest of the output get written to stderr.使输出的其余部分写入 stderr。
stdoutmakes the rest of the output get written to stdout.使输出的其余部分写入标准输出。
time_appconnectThe time in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed.从开始到完成与远程主机的 SSL/SSH/etc 连接/握手所花费的时间(以秒为单位)。
time_connectThe time in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.从开始到完成与远程主机(或代理)的 TCP 连接所花费的时间(以秒为单位)。
time_namelookupThe time in seconds, it took from the start until the name resolving was completed.从开始到名称解析完成所花费的时间(以秒为单位)。
time_pretransferThe time in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.从开始到文件传输即将开始所花费的时间(以秒为单位)。这包括特定于所涉及的特定协议的所有预传输命令和协商。
time_redirectThe time in seconds, it took for all redirection steps including name lookup, connect, pre-transfer and transfer before the final transaction was started. time_redirect the complete execution time for multiple redirections.以秒为单位的时间,在最终事务开始之前,所有重定向步骤(包括名称查找、连接、预传输和传输)所花费的时间。 time_redirect 多次重定向的完整执行时间。
time_starttransferThe time in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.从开始到第一个字节即将传输所花费的时间(以秒为单位)。这包括 time_pretransfer 以及服务器计算结果所需的时间。
time_totalThe total time in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.完整操作持续的总时间(以秒为单位)。时间将以毫秒分辨率显示。
urlThe URL used in the transfer. (Introduced in 7.75.0)传输中使用的 URL。 (7.75.0 中引入)
url_effectiveThe URL that was fetched last. This is particularly meaningful if you have told curl to follow Location: headers (with -L).最后获取的 URL。如果您告诉curl 遵循 Location: 标头(使用 -L ),这尤其有意义。
urlnum0-based numerical index of the URL used in the transfer. (Introduced in 7.75.0)传输中使用的 URL 从 0 开始的数字索引。 (7.75.0 中引入)

进阶版

这个版本是为了一次性输出所有的时间格式;

将下面的内容写入到curl-fmt.txt文件中:

\n
            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

curl-fmt.txt的目录下执行:

curl -s -w "@curl-fmt.txt" -o /dev/null http://www.baidu.com/

curl输出结果:

            time_namelookup:  0.012421
               time_connect:  0.024995
            time_appconnect:  0.000000
           time_pretransfer:  0.025187
              time_redirect:  0.000000
         time_starttransfer:  0.041807
                            ----------
                 time_total:  0.041905