使用CURL检测Clinet侧发起的HTTP请求各阶段时间

第1、HTTP请求的过程介绍html

一个HTTP请求,涉及多个阶段python

一、DNS解析域名git

二、请求从Clinet路由至Server,Clinet与Server创建TCP链接github

三、若是使用了HTTPS,还涉及SSL链接的创建数据库

四、server开始准备数据后端

开始逻辑计算、调后端接口、查数据库缓存等缓存

五、server开始传递数据服务器

数据准备完成,开始给client传数据cookie

六、数据传输完毕app

七、整个过程可能还涉及屡次重定向

 

 

第2、关于CURL的介绍

CURL是利用URL语法在命令行方式下工做的开源数据传输工具。

支持:DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, cookies, user+password authentication (Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos), file transfer resume, proxy tunneling 等

最新版的curl稳定版为7.55.1(截止20170817)

源代码:https://github.com/curl/curl

 

 

 

第三:用CURL检测Clinet侧发起的HTTP请求各阶段时间,简要说明

wKioL1mVjoSRgA_ZAAGP7R3orEs850.png-wh_50

一、TCP创建链接的耗时:CONNECT-NAMELOOKUP

二、创建TCP链接到server返回client第一个字节的时间:

STARTTRANSFER-CONNECT

三、SERVER处理数据的时间:

能够用STARTTRANSFER - PRETRANSFER计算获得

四、CLIENT接收数据的耗时(开始接收至接收完成):

TOTAL-STARTTRANSFER

 

 

第4、例子:

curl -o /dev/null -s -w time_namelookup:"\t"%{time_namelookup}"\n"time_connect:"\t\t"%{time_connect}"\n"time_appconnect:"\t"%{time_appconnect}"\n"time_pretransfer:"\t"%{time_pretransfer}"\n"time_starttransfer:"\t"%{time_starttransfer}"\n"time_total:"\t\t"%{time_total}"\n"time_redirect:"\t\t"%{time_redirect}"\n"  https://www.yqb.com/

wKioL1mVjobzldy3AAEPmtIEJNQ613.png-wh_50

一、DNS解析耗时:0.008s

二、TCP创建链接的耗时:0.059-0.008=0.051s

三、SSL握手完成耗时:0.228-0.059=0.169s

169ms,多了一层SSL仍是很耗时的

四、server处理数据的时间:0.287-0.228=0.059

59ms,说明服务器处理很快

五、整体的耗时:0.228s

六、整个过程没有redirect,因此redirect的耗时为0

 

 

再举一例:

wKiom1mVjomxw8_qAACQJsxBKgc809.png-wh_50

服务器处理数据的耗时:2.305-0.014=2.291s

这就说明server端的性能是值得研究的。

对于server端而言,有须要分析它的耗时:

防火墙->负载均衡->应用->缓存和DB

须要深刻去分析这个时间消耗在哪一个环节,有针对性的优化。

 

 

 

第5、详细说明

NAMELOOKUP:从开始计算,域名解析完成的耗时

CURLINFO_NAMELOOKUP_TIME. The time it took from the start until the name resolving was completed.

CONNECT:从开始计算,TCP创建完成的耗时

CURLINFO_CONNECT_TIME. The time it took from the start until the connect to the remote host (or proxy) was completed.

APPCONNECT:从开始计算,应用层(SSL,在TCP之上的应用层)链接/握手完成的耗时

CURLINFO_APPCONNECT_TIME. The time it took from the start until the SSL connect/handshake with the remote host was completed. (Added in in 7.19.0)

PRETRANSFER:从开始计算,准备开始传输数据的耗时

CURLINFO_PRETRANSFER_TIME. The time it took from the start until the file transfer is just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.

STARTTRANSFER:从开始计算,开始传输数据的耗时(libcurl接收到第一个字节)

CURLINFO_STARTTRANSFER_TIME. The time it took from the start until the first byte is received by libcurl.

TOTAL:总的耗时

CURLINFO_TOTAL_TIME. Total time of the previous request.

REDIRECT:整个过程重定向的耗时,若是整个过程没有重定向,这个时间为0

CURLINFO_REDIRECT_TIME. The time it took for all redirection steps include name lookup, connect, pretransfer and transfer before final transaction was started. So, this is zero if no redirection took place.

另:python也有一个pycurl模块,你们能够尝试。


 

参考:

https://curl.haxx.se/libcurl/c/curl_easy_getinfo.html

相关文章
相关标签/搜索