[实践OK]相当有用之频率测试 curl 时间 CURL一行时间参数/curl获取网络时间访问多次测试网页响应时间的shell脚本,需要Curl支持,Curl测试访问时间,DNS解析时间。

jackxiang 2013-6-6 12:02 | |
背景:一天突然发现访问网站特别的慢,怎么办?原因是啥,不知道,此时,祭出CURL,加上相关的数据输出,特别是对于DNS慢这种问题最难查,居然被我碰到了,time_namelookup 这个参数很重要,DNS解析时间,从请求开始到DNS解析完毕所用时间(记得关掉 Linux 的 nscd 的服务测试),当然,查dns更为直观的是用curl的-H参数(http://jackxiang.com/post/4423/),不用dns解析时的一个指标,再就是用dns解析的一个指标,一对比就出来了,该命令在windows下使用也没问题。



DNS解析时的时间:

5.011:5.011:5.020:5.020:
不用DNS相当于写死Host解析时的办法:

0.003:0.003:0.026:0.026

time_namelookup时间5.011太长了,说明DNS有问题,一查果然有问题,由于此内网DNS解析了单位办工机器,还解析线上服务器内部域名,压力太大导致,为此,新加了一台dns服务器,减轻压力,解析的速度上去了,也就好了。

相关参数解释:
time_connect  建立到服务器的 TCP 连接所用的时间
time_starttransfer  在发出请求之后,Web 服务器返回数据的第一个字节所用的时间
time_total  完成请求所用的时间
time_namelookup  DNS解析时间,从请求开始到DNS解析完毕所用时间(记得关掉 Linux 的 nscd 的服务测试)
speed_download  下载速度,单位-字节每秒。(注意它是字节)
[/codes]

来个实例:

加上字节:



time函数实现时间:



加上Shell的for循环,一般主要是打印时间:




粘贴好像会报错,直接给个DownLoad:

sh curlExecuteTime.sh  -n 12 "http://localhost"
**春(**春) 11:11:56
time_connect: 0.001
time_namelookup: 0
time_total: 0.01419
**春(**春) 11:12:18
这个结果按说是没问题的吧
向东(向东) 11:12:34
没问题,你要想获取接口返回修改下这儿:curl -o /dev/null -s -w \
向东(向东) 11:12:42
它定向到null了。
向东(向东) 11:21:16
c=`curl -o /tmp/$i.txt -s -w \
这样就可以在tmp下生成1.txt 2.txt了,呵呵。
DownLoad:




零:可以直接用shell实现,用vim列模式/行模式进行批量输入,当然也可以用shell的for循环,如出现301错误需要输出头-i,如下:


一:PHP下的Curl获取时间 :


二:Shell下的curl原理:
curl -o /dev/null -s -w '连接时间:%{time_connect},传输时间:%{time_starttransfer}:%{time_total}' "http://jackxiang.com"

POST示例(不用-o写入文件,直接看):

用法及返回结果如下:
lvyaozu@lvyaozu-desktop:~/shell$ ./requesturl.sh -n20 http://www.baidu.com
Request url: http://www.baidu.com
Request number: 20
Request Failed: 0
——Average Value——
time_connect: 0.1614
time_total: 0.1693
time_namelookup: 0.0088
————————-
请求参数:
-n 指定发送多少次请求,默认为10
返回结果为 curl -w 选项返回值,以下摘自curl官方文档:
http://curl.haxx.se/docs/manpage.html
time_connect The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.
time_total The total time, in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.
time_namelookup The time, in seconds, it took from the start until the name resolving was completed.
代码如下:


我的实践如下:
sh curlExecuteTime.sh  -n 12 "http://jackxiang.com/getAlbumsByUId?appkey=100&userid=21240168&sortby=hot"
Request url: hhttp://jackxiang.com/getAlbumsByUId?appkey=100&userid=21240168&sortby=hot
Request number: 12
Request Failed: 0
------Average Value------
time_connect: 0.001
time_namelookup: 0.001
time_total: 0.0760833
-------------------------

来自:http://renxiangzyq.iteye.com/blog/782773



1、开启gzip请求
curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte

2、监控网页的响应时间
curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" "http://www.kklinux.com"

3. 监控站点可用性
curl -o /dev/null -s -w %{http_code} "http://www.kklinux.com"

4、以http1.0协议请求(默认为http1.1)
curl -0 ..............


监控站点首页下载时间:

curl -o /dev/null -s -w ‘%{time_total}’ http://www.miotour.com

curl -o /dev/null -s -w ‘%{http_code}’ http://www.miotour.com

curl -o /dev/null -s -w %{http_code}:%{time_connect}:%{time_starttransfer}:%{time_total} http://www.miotour.com

结果:2.547

-s 静默输出;没有-s的话就是下面的情况,这是在脚本等情况下不需要的信息。

[ec2-user@ip-10-122-250-19 ~]$ curl -o /dev/null  -w ‘%{time_total}’ http://www.miotour.com
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
100 67770    0 67770    0     0  19228      0 –:–:–  0:00:03 –:–:– 20705
结果:3.524
监控首页各项时间指标:

curl -o /dev/null -s -w ‘%{time_connect}:%{time_starttransfer}:%{time_total}’ http://www.miotour.com

结果:                                                0.244:                             1.044:                         2.672

时间指标解释 :

time_connect    建立到服务器的 TCP 连接所用的时间

time_starttransfer    在发出请求之后,Web 服务器返回数据的第一个字节所用的时间

time_total   完成请求所用的时间

在 发出请求之后,Web 服务器处理请求并开始发回数据所用的时间是

(time_starttransfer)1.044 - (time_connect)0.244 = 0.8 秒

客户机从服务器下载数据所用的时间是

(time_total)2.672 - (time_starttransfer)1.044 = 1.682 秒

指定特定主机IP地址访问网站

curl -x 61.135.169.105:80 http://www.baidu.com

curl -x 61.135.169.125:80 http://www.baidu.com


摘录来自:http://blog.163.com/dingding_jacky/blog/static/166912787201242440274/

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/6421/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2019-3-26 13:51
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]