【转】使用CURL检测Client侧发起的HTTP请求各阶段时间

生产环境下,经常会有需要调用第三方的接口。如果没有很好地监控系统(SA负责网络层的监控、研发人员负责自己的业务层的监控实现),则出问题时候大大增加了排查难度,影响到服务的SLA。


第一、HTTP请求的过程介绍

1、DNS解析域名

2、请求从Clinet路由至Server,Clinet与Server建立TCP连接

3、如果使用了HTTPS,还涉及SSL连接的建立

4、server开始准备数据 (开始逻辑计算、调后端接口、查数据库缓存等)

5、server开始传递数据 (数据准备完成,开始给client传数据)

6、数据传输完毕

7、整个过程可能还涉及多次重定向


第二、关于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请求各阶段时间,简要说明

image.png

1、TCP建立连接的耗时:CONNECT-NAMELOOKUP

2、建立TCP连接到server返回client第一个字节的时间:

STARTTRANSFER-CONNECT

3、SERVER处理数据的时间:

可以用STARTTRANSFER - PRETRANSFER计算得到

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

TOTAL-STARTTRANSFER


第五、详细说明

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





下面是我学习了上文博客,自己做的一个测试:

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"  http://blog.51cto.com/lee90

time_namelookup:0.000

time_connect:0.032

time_appconnect:0.000

time_pretransfer:0.032

time_starttransfer:0.780

time_total:0.844

time_redirect:0.000

注:这里的单位为秒


根据上面的这些数值,可以算出请求http://blog.51cto.com/lee90如下结论:

dns解析耗时: 0.000s  (一般0.000的话,说明之前不久请求过这个域名,本地已经有缓存了)

建立连接耗时: 0.032s

传输耗时:0.780-0.032=0.748s (因为传输的博文内容比较多,因此可以时间挺长的)


下面是我测试curl自己的博客的截图:

image.png


image.png


下图是我通过blackbox_exporter+grafana展示的截图吗,可以和上面的curl结果对比下:

image.png


此外,还可以使用pycurl+graphite+statsd来采集这些信息,但是不如prometheus好用,生产上我们还是推荐用prometheus来做这件事情。










本文转自 lirulei90 51CTO博客,原文链接:http://blog.51cto.com/lee90/2063096,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值