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

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

我的请求示例如下:

curl -X POST -d @file server:port

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

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

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

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


当前回答

我做了一个友好的格式化器来嗅探curl请求,以帮助调试(参见注释了解用法)。它包含了您可以以易于阅读的格式写出的所有已知输出参数。

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

其他回答

我做了一个友好的格式化器来嗅探curl请求,以帮助调试(参见注释了解用法)。它包含了您可以以易于阅读的格式写出的所有已知输出参数。

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

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

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

视窗电源外壳

您可以使用测量命令

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

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

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

-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

如果你想分析或总结延迟,你可以尝试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)