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

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

我的请求示例如下:

curl -X POST -d @file server:port

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

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

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

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


当前回答

从这篇精彩的博客文章…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变量中,以便能够从任何文件夹中使用该命令。)

其他回答

选项1:测量总时间:

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

样例输出:

Total: 0.441094s

选项2:获取建立连接的时间,第一个字节的时间(TTFB)和总时间:

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

样例输出:

Establish Connection: 0.020033s
TTFB: 0.417907s
Total: 0.435486s

参考:得到响应时间与卷曲

如果你想分析或总结延迟,你可以尝试apache bench:

ab -n [number of samples] [url]

例如:

ab -n 100 http://www.google.com/

它将显示:

This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.google.com (be patient).....done


Server Software:        gws
Server Hostname:        www.google.com
Server Port:            80

Document Path:          /
Document Length:        12419 bytes

Concurrency Level:      1
Time taken for tests:   10.700 seconds
Complete requests:      100
Failed requests:        97
   (Connect: 0, Receive: 0, Length: 97, Exceptions: 0)
Total transferred:      1331107 bytes
HTML transferred:       1268293 bytes
Requests per second:    9.35 [#/sec] (mean)
Time per request:       107.004 [ms] (mean)
Time per request:       107.004 [ms] (mean, across all concurrent requests)
Transfer rate:          121.48 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       20   22   0.8     22      26
Processing:    59   85 108.7     68     911
Waiting:       59   85 108.7     67     910
Total:         80  107 108.8     90     932

Percentage of the requests served within a certain time (ms)
  50%     90
  66%     91
  75%     93
  80%     95
  90%    105
  95%    111
  98%    773
  99%    932
 100%    932 (longest request)

以下内容是受西蒙的回答启发。它是自包含的(不需要单独的格式文件),这使得它非常适合包含在.bashrc中。

curl_time() {
    curl -so /dev/null -w "\
   namelookup:  %{time_namelookup}s\n\
      connect:  %{time_connect}s\n\
   appconnect:  %{time_appconnect}s\n\
  pretransfer:  %{time_pretransfer}s\n\
     redirect:  %{time_redirect}s\n\
starttransfer:  %{time_starttransfer}s\n\
-------------------------\n\
        total:  %{time_total}s\n" "$@"
}

此外,它应该适用于curl通常接受的所有参数,因为“$@”只是传递了它们。例如,你可以这样做:

curl_time -X POST -H "Content-Type: application/json" -d '{"key": "val"}' https://postman-echo.com/post

输出:

   namelookup:  0,125000s
      connect:  0,250000s
   appconnect:  0,609000s
  pretransfer:  0,609000s
     redirect:  0,000000s
starttransfer:  0,719000s
-------------------------
        total:  0,719000s

您可以使用curl -v——trace-time向跟踪/详细输出添加时间戳。这必须在详细模式或跟踪模式下完成。

下面是可以使用-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。