前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【转】使用CURL检测Client侧发起的HTTP请求各阶段时间

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

作者头像
保持热爱奔赴山海
发布2019-09-17 11:38:37
1.4K0
发布2019-09-17 11:38:37
举报
文章被收录于专栏:饮水机管理员饮水机管理员

参考原文地址: https://cloud.tencent.com/developer/article/1506800

生产环境下,经常会有需要调用第三方的接口。如果没有很好地监控系统(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请求各阶段时间,简要说明

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"  https://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

注:这里的单位为秒

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

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

建立连接耗时: 0.032s

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

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

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

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-01-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
SSL 证书
腾讯云 SSL 证书(SSL Certificates)为您提供 SSL 证书的申请、管理、部署等服务,为您提供一站式 HTTPS 解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档