大部分人想到的网络延迟计算方法就是ping命令,其实ping命令并不是用来计算网络延迟的,它是用来判断某个ip通不通
curl
# 测试命令
curl -sSL -o /dev/null --connect-timeout 5 --write-out \
"\n--------------REQUEST TIME TABLE-----------------\n
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\n" \
https://facebook.com
- time_namelookup DNS查找IP
- time_connect TCP三次握手完成
- time_appconnect SSL三次握手完成
- time_pretransfer 客户端开始向服务器发送HTTP报文
- (服务器从报文中发现请求头中的url需要重定向,time_redirect就是重定向的时间,值0表示不需要重定向)
- time_starttransfer 服务器开始向客户端发送response数据
- total_time HTTP总耗时
总结
time_appconnect
代表tcp耗时,total_time
代表http耗时
经验之谈:网络延迟主要发生在ssl握手、重定向、数据传输,这三个阶段
Chrome
Stalled:表示请求或响应被暂停或延迟
Initial connection:TCP三次握手和SSL三次握手的总时间
TTFB(Time To First Byte):它是指从浏览器发起请求到接收到服务器第一个字节的时间间隔