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

第一、HTTP请求的过程介绍

一个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请求各阶段时间,简要说明

wKioL1mVjoSRgA_ZAAGP7R3orEs850.png-wh_50

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

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

STARTTRANSFER-CONNECT

3、SERVER处理数据的时间:

可以用STARTTRANSFER - PRETRANSFER计算得到

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

TOTAL-STARTTRANSFER

 

 

第四、例子:

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

1、DNS解析耗时:0.008s

2、TCP建立连接的耗时:0.059-0.008=0.051s

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

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

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

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

5、总体的耗时:0.228s

6、整个过程没有redirect,所以redirect的耗时为0

 

 

再举一例:

wKiom1mVjomxw8_qAACQJsxBKgc809.png-wh_50

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

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

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

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

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

 

 

 

第五、详细说明

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

转载于:https://my.oschina.net/u/3656540/blog/2986659

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]是一段PHP代码,用于设置HTTP请求的header头信息。其中包括了Content-Type、Content-Length、Accept、client-id和client-secret等参数。引用\[2\]是一个CurlRequest函数,用于发送HTTP请求。该函数使用curl库来初始化浏览器、设置浏览器参数、禁止https协议验证域名和ssl安全认证证书等。如果传入了data参数,则将请求方式设置为POST,并设置POST的数据包。如果传入了header参数,则设置header头。最后,通过curl_exec函数发起请求,并返回请求回来的数据。引用\[3\]是一段关于etcd的日志信息,描述了创建静态Pod清单、等待kubelet启动控制平面、检查kubelet是否运行或健康等过程。然后提出了一个问题,即使用curl命令发送HTTP请求时报错的解决思路。 解决思路一是认为kubelet没有启动导致报错。可以尝试启动kubelet来解决问题。 #### 引用[.reference_title] - *1* *2* [PHP使用CURL设置header头传参以及设置Content-Type: application/json类型的后台数据接收...](https://blog.csdn.net/dgnxhpl41065/article/details/102211458)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [The HTTP call equal to ‘curl -sSL http://localhost:10248/healthz‘ failed with error: Get ...](https://blog.csdn.net/qq_35078688/article/details/122903242)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值