如何使用cURL一次性测量请求和响应时间?

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

我想用cURL来测量请求,响应和总时间。

我的示例请求如下所示:

 curl -X POST -d @file server:port 

而我目前在Linux中使用time命令来衡量它:

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

时间命令只测量时间,但这不是我正在寻找的。

有没有办法使用cURL来测量请求和响应时间?

从这个辉煌的博客文章… https://blog.josephscott.org/2011/10/14/timing-details-with-curl/

cURL支持请求细节的格式化输出(请参阅cURL手册页以获取详细信息 ,在-w, –write-out <format> )。 就我们的目的而言,我们只关注所提供的时间细节。

  1. 创build一个新文件,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 
  2. 发出请求:

     curl -w "@curl-format.txt" -o /dev/null -s "http://wordpress.com/" 

    或者在Windows上,这是…

     curl -w "@curl-format.txt" -o NUL -s "http://wordpress.com/" 

这是什么:

-w "@curl-format.txt"告诉cURL使用我们的格式文件
-o /dev/null将请求的输出redirect到/ dev / null
-s告诉cURL不要显示进度表
"http://wordpress.com/"是我们要求的url。 特别是如果您的url有“&”查询string参数,请使用引号

这是你回来的东西:

  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 

制作Windows快捷方式(又名BAT文件)

把这个命令放在CURLTIME.BAT(与curl.exe相同的文件夹中)

 curl -w "@%~dp0curl-format.txt" -o NUL -s %* 

然后,你可以简单地调用…

 curltime wordpress.org 

答案是:

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

所有与-w一起使用的variables都可以在man curlfind。

你可以添加到你的.bashrc等基于其他答案的快捷方式:

 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来测量响应时间 ,请使用以下命令:

 curl -o /dev/null -s -w %{time_total} http://www.google.com 

要获取不同types的时间,请使用以下命令:

 curl -o /dev/null -s -w %{time_connect}:%{time_starttransfer}:%{time_total} http://www.google.com 

来源: 用curl获得响应时间

如果你想分析或总结延迟,你可以尝试Apache的工作台:

 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) 

我做了一个友好的格式化程序,用于嗅探curl请求来帮助debugging(请参阅使用注释)。 它包含了每一个已知的输出参数,你可以用易读的格式写出来。

https://gist.github.com/manifestinteractive/ce8dec10dcb4725b8513

嘿,比Apache Bench更好,SSL问题更less

 ./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