curl获取网站的响应时间

curl获取网站的响应时间html




0.问题浏览器


总有人反馈说打开某个页面比较慢,缓存

可是他们并不清楚到底慢在哪里。服务器

一样的问题,网络

在咱们访问外部业务的接口时,app

每每也是感受很慢,运维

但就是不清楚慢在哪里。curl




1.分析iphone


当咱们打开一个网页,ide

对于一个不须要考虑技术的人,

想象的画面应该是,

咱们经过地址栏问浏览器要个东西,

而后浏览器经过咱们提供的地址去对应的服务器取对应的文件,

而后根据服务器的回应将内容显示给咱们。


从一个运维人员的视角,

可能就不能这么简单粗暴地想象了,

不然可能永远也没法确认问题究竟出如今哪里了。

wKiom1kX67SyNSqHAAKGkcagTis841.png-wh_50



简单点来归纳下,

1.当咱们打开浏览器,在地址栏输入www.qq.com时,

浏览器首先会根据浏览器缓存,电脑缓存,电脑hosts文件,DNS服务来解析www.qq.com对应的网络地址是多少,这个过程会根据解析地方的不一样而耗时不一样。


使用浏览器缓存和电脑缓存,

返回的结果是最快的,可是可能不是最新的,也多是错误的。

经过hosts文件解析域名的优先级是高于DNS服务的解析的,

咱们也能够在hosts中修改某些域名的解析结果。


2.浏览器得到域名相应IP后,开始与服务器进行三次握手进行链接。

3.浏览器与服务器创建链接后向其发起服务请求

4.服务器内部处理完请求后,将处理结果发送给浏览器

5.浏览器解析服务器发送的结果,而后展现给用户


wKioL1kX68iS9lg3AAOA_bEGVE0404.png-wh_50


上面简单描述了浏览器在与服务器进行服务请求的过程,

其中任何一个环节耗时太久都将影响到用户访问网站的速度。



2.Curl获取相关数据


使用cur  -w/--write-out l获取相关数据


几个比较关键的参数:


url_effective 最后获取的URL。

http_code 上一次HTTP(S)或FTP(S)操做返回的响应码。在7.18.2版加入的response_code显示一样的信息。

http_connect 在最后一次对cURL的CONNECT请求的响应(从代理)中发现的数值代码。 (在7.12.4版加入)

time_total 所有操做耗费的时间,单位为秒。精确到毫秒。

time_namelookup 从开始到域名解析完成耗费的时间,单位为秒。

time_connect TCP链接远程主机(或代理服务器)所耗时间,单位为秒。

time_appconnect SSL/SSH/等与远程主机链接/握手完成花费的时间,单位为秒。(在7.19.0 版加入)

time_pretransfer 从开始到文件将要传输前花费的时间,单位为秒。包括指定的协议全部预传输命令和negotiations。

time_redirect 全部重定向步骤的时间,包域名解析、链接、预传输和最后事务开始前的传输,单位为秒。time_redirect显示多重重定向的完整执行时间。(在7.12.3版加入 )

time_starttransfer  从开始到第一个字节将被传输前耗费的时间,单位为秒。

这包括time_pretransfer和服务器须要的运算结果的时间。

size_download下载的总字节数。

size_upload上传的总字节数。

size_header 下载的header的总字节数。

size_request 发送的HTTP请求的总字节数。

speed_download curl成功下载的平均下载速度。

speed_upload curl成功上传的平均上传速度。





3.获取请求的耗时信息



针对特定的请求,经过curl能够获取到各个环节的耗时状况,

好比域名解析的耗时,创建链接的耗时,服务端处理的耗时,总的耗时。



国内某合做方素材


curl -o /dev/null -s -w "http_code: %{http_code}\nhttp_connect:%{http_connect}\ncontent_type:%{content_type}\ntime_dns:%{time_namelookup}\ntime_redirect:%{time_redirect}\ntime_pretransfer:%{time_pretransfer}\ntime_connect:%{time_connect}\ntime_starttransfer:%{time_starttransfer}\ntime_total:%{time_total}:\nspeed_download:%{speed_download}\n " "http://di.adsame.com/flv_test/20161107143341_231.jpg";


http_code: 200

http_connect:000

content_type:p_w_picpath/jpeg

time_dns:0.005

time_redirect:0.000

time_pretransfer:0.013

time_connect:0.013

time_starttransfer:0.021

time_total:0.029:

speed_download:441815.000


苹果官网图片素材


curl -o /dev/null -s -w "http_code: %{http_code}\nhttp_connect:%{http_connect}\ncontent_type:%{content_type}\ntime_dns:%{time_namelookup}\ntime_redirect:%{time_redirect}\ntime_pretransfer:%{time_pretransfer}\ntime_connect:%{time_connect}\ntime_starttransfer:%{time_starttransfer}\ntime_total:%{time_total}:\nspeed_download:%{speed_download}\n " "http://p_w_picpaths.apple.com/v/iphone-7/d/p_w_picpaths/overview/design_hero_large.jpg";


http_code: 200

http_connect:000

content_type:p_w_picpath/jpeg

time_dns:0.449

time_redirect:0.000

time_pretransfer:0.456

time_connect:0.456

time_starttransfer:0.465

time_total:0.493:

speed_download:419293.000

 


微博短域名连接


curl -o /dev/null -s -w "http_code: %{http_code}\nhttp_connect:%{http_connect}\ncontent_type:%{content_type}\ntime_dns:%{time_namelookup}\ntime_redirect:%{time_redirect}\ntime_pretransfer:%{time_pretransfer}\ntime_connect:%{time_connect}\ntime_starttransfer:%{time_starttransfer}\ntime_total:%{time_total}:\nspeed_download:%{speed_download}\n "  -L "http://t.cn/Rip1YJA"


http_code: 200

http_connect:000

content_type:text/html;charset=GBK

time_dns:0.210

time_redirect:0.079

time_pretransfer:0.240

time_connect:0.240

time_starttransfer:0.290

time_total:0.369:

speed_download:4349.000

 


4.其余


目前只能用curl获取到指定请求的静态页面,仍是没法像浏览器那样将页面中的连接都请求到,

这样就没法真实反应浏览器中访问页面的耗时状况。

不过当用户反映访问某个页面慢的时候,

经过curl访问对应的地址基本上能够肯定,

单个请求主要耗时在哪里。

相关文章
相关标签/搜索