curl查看各阶段耗时

如何使用

curl命令提供了-w参数来输出请求的各个阶段耗时情况。

1
2
3
4
5
6
7
8
9
#cat curl-format.txt
time_namelookup: %{time_namelookup}\n
time_connect: %{time_connect}\n
time_appconnect: %{time_appconnect}\n
time_redirect: %{time_redirect}\n
time_pretransfer: %{time_pretransfer}\n
time_starttransfer: %{time_starttransfer}\n
----------\n
time_total: %{time_total}\n

  • time_namelookup: DNS解析时间
  • time_connect:tcp建联时间
  • time_appconnect:SSL/SSH 等上层协议建立连接的时间,比如 connect/handshake 的时间。注意,即使curl语句中带有-k参数(忽略证书校验),这一项时间仍然存在,因为-k只是忽略证书的校验,本身SSL握手仍然存在。
  • time_redirect:从开始到最后一个请求事务的时间
  • time_pretransfer:从请求开始到开始传输的时间
  • time_total:这次请求花费的全部时间

下面来看一个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
lizaibin@lizaibindeMacBook-Pro:/tmp$ curl -w "@curl-format.txt" -o /dev/null -s "biglee.top"
time_namelookup: 4.545886
time_connect: 4.772797
time_appconnect: 0.000000
time_redirect: 0.000000
time_pretransfer: 4.773164
time_starttransfer: 5.396703
----------
time_total: 6.868380
lizaibin@lizaibindeMacBook-Pro:/tmp$ curl -w "@curl-format.txt" -o /dev/null -s "biglee.top"
time_namelookup: 0.005272
time_connect: 0.223869
time_appconnect: 0.000000
time_redirect: 0.000000
time_pretransfer: 0.224186
time_starttransfer: 0.529396
----------
time_total: 0.837033
lizaibin@lizaibindeMacBook-Pro:/tmp$ ping biglee.top
PING sni.github.map.fastly.net (185.199.109.153): 56 data bytes
64 bytes from 185.199.109.153: icmp_seq=0 ttl=50 time=220.691 ms
64 bytes from 185.199.109.153: icmp_seq=1 ttl=50 time=228.045 ms
64 bytes from 185.199.109.153: icmp_seq=2 ttl=50 time=247.959 ms
64 bytes from 185.199.109.153: icmp_seq=3 ttl=50 time=267.425 ms

两次相同的请求, 因为dns有了缓存,因此第二次请求的DNS时间大幅缩小。另外,tcp建联时间基本等于一个RTT。

再来看一个https的例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
lizaibin@lizaibindeMacBook-Pro:/tmp$ curl -w "@curl-format.txt" -o /dev/null  -s "https://www.163.com"
time_namelookup: 0.004464
time_connect: 0.014537
time_appconnect: 0.053920
time_redirect: 0.000000
time_pretransfer: 0.053983
time_starttransfer: 0.067289
----------
time_total: 0.142331
lizaibin@lizaibindeMacBook-Pro:/tmp$ curl -w "@curl-format.txt" -o /dev/null -k -s "https://www.163.com"
time_namelookup: 0.005278
time_connect: 0.017175
time_appconnect: 0.064152
time_redirect: 0.000000
time_pretransfer: 0.064268
time_starttransfer: 0.075927
----------
time_total: 0.148018

第二次带了-k参数,但是time_appconnect并没有消失,因为仍热需要SSL握手,这是我以前一直误会的地方,以为-k就没有SSL时间了。

几个典型的时间计算

  • TCP建联时间:time_connect - time_namelookup
  • 服务器处理时间: time_starttransfer - time_pretransfer
  • 内容传输or下载时间: time_total - time_starttransfer
  • SSL时间: time_appconnect - time_connect

httpstat——一个美化版本

使用效果如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
lizaibin@lizaibindeMacBook-Pro:~/lzbworkspace/blog$ httpstat www.163.com/get
Connected to 115.231.22.125:80 from 30.43.140.87:53017

HTTP/1.1 302 Moved Temporarily
Date: Mon, 03 Sep 2018 11:47:24 GMT
Server: nginx
Content-Type: text/html
Transfer-Encoding: chunked
Location: http://www.163.com
Expires: Mon, 03 Sep 2018 11:49:24 GMT
Cache-Control: max-age=120
X-Via: 1.1 PSzjwzdx11am82:6 (Cdn Cache Server V2.0), 1.1 PSzjhzdx4ay110:2 (Cdn Cache Server V2.0)
Connection: keep-alive

Body stored in: /var/folders/7j/j78lgmp11g77x6tmhcbwkv6w0000gp/T/tmphy_7oml3

DNS Lookup TCP Connection Server Processing Content Transfer
[ 33ms | 8ms | 94ms | 1ms ]
| | | |
namelookup:33ms | | |
connect:41ms | |
starttransfer:135ms |
total:136ms
```

httpstat是用python写的一个curl的包装版本,git地址:https://github.com/reorx/httpstat
安装方法

pip install httpstat
`