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

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

我的请求示例如下:

curl -X POST -d @file server:port

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

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

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

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


当前回答

以下是答案:

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

所有与-w一起使用的变量都可以在man curl中找到。

其他回答

选项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)

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

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

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

以下内容是受西蒙的回答启发。它是自包含的(不需要单独的格式文件),这使得它非常适合包含在.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