获取网站性能指标shell脚本

来源:互联网 发布:程序员培训全日制吗 编辑:程序博客网 时间:2024/04/30 02:19

本文转载自http://pmghong.blog.51cto.com/3221425/1390599。


1.web主要性能指标

   心理学中第一印象效应非常重要,以后这个印象会产生主导位置影响人们的判断。在浏览器接收到回车到”唰”页面出来这个过程中就那么几秒钟的时间就是客户访问这个网站的第一印象。

   根据统计分析,有这么一个现象:如果一个网站的页面加载时间在5秒钟或者更少,用户离开(以关闭页面为准)的比例通常不会超过20%,而超过了五秒钟,用户流失的比例会按照每秒1-2%增加。一般的电脑都能在3-10秒内完成页面的加载。因此,网站的打开速度是直接影响到用户是否愿意接受这个网站的一大要素。

通常需要关注的有如下几个性能指标:

wKiom1M-YNHQJrYAAAVNYW9-ZpY075.jpg


2.curl命令介绍

     curl支持包括httphttpsftp在内的众多协议,它还支持POSTcookie认证,从指定偏移处下载部分文件,参照页,用户代理字符串,扩展头部,限速,文件大小限制,进度条等特性。下面主要介绍如何通过curl 命令获取到网站的性能指标信息:

1
root@node1:~# curl -o /dev/null -s -w %{time_total}"\n" www.yy.com


命令解释:

-o   //curl获取到的页面代码重定向到/dev/null

-s    //静默模式,若不加该参数,会有类似于如下的显示:


1
2
3
4
5
root@node1:~# curl -o /dev/null -w %{time_namelookup}"\n" www.yy.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  148k    0  148k    0     0   503k      0 --:--:-- --:--:-- --:--:--  536k
0.006   //最后这个值才是我们要的


-w %{option}    //指定要获取的指标

可获取的指标,如下所示:

time_total           //完成请求所用的时间

time_namelookup    //解析完成的时间

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

time_pretransfer      //链接建立完成准备响应时间

time_redirect         //重定向完成时间

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

http_code            //http返回类似404,200,500

size_download        //下载网页或文件大小

size_upload          //上传文件大小

size_header          //响应头

size_request          //发送请求参数大小

speed_download      //传输速度

speed_upload         //平均上传速度

content_type          //下载文件类型. (Added in 7.9.5)


3.获取网站性能脚本

版本一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
usage="
Usage: $0 [options...] <url>\n
Options:\n
 -h          Show this help message.\n
 -u <url>    The url to request.\n
 -n <num>    The numbers to request\n
"
                                                                  
if [ $# -lt 4 ];then
    echo -e $usage
    exit 1
fi
                                                                  
num=10
                                                                  
while getopts "n:h:u" arg
do
    case $arg in
        n)
            num=$2
            if [ $num -lt 1 ];then
                num=1
            fi
            ;;
        h)
            echo -e $usage
            exit 1
            ;;
        u)
            url=$4
            ;;
        *)
            echo "Unkown argument"
            exit 1
            ;;
     esac
done
                                                                  
echo "Request url:" ${url}
echo "Request number:" ${num}
echo "------Average Value------"
                                                                  
[ -d "/tmp/data" ] || mkdir -p /tmp/data
options="time_total time_connect time_redirect time_namelookup time_pretransfer time_starttransfer"
for option in $options
do
    for ((i=1;i<=${num};i++))
    do
        curl -o /dev/null -s -w %{$option}"\n" $url >> /tmp/data/$option
    done
                                                                  
    avg=`awk 'BEGIN{sum=0;NR=0}{sum+=$1}END{print sum/NR}' /tmp/data/$option`
    echo ${option} " = " ${avg}
    cat /dev/null /tmp/data/$option
done


执行结果如下:

1
2
3
4
5
6
7
8
9
10
root@node1:/tmp/shell# ./url.sh -n 5 -u www.yy.com
Request url: www.yy.com
Request number: 5
------Average Value------
time_total  =  0.541
time_connect  =  0.0296
time_redirect  =  0
time_namelookup  =  0.0054
time_pretransfer  =  0.0268
time_starttransfer  =  0.0452


脚本设计思路

(1)通过命令后面带的参数获得统计的次数、统计的URL

(2)通过变量option定义需要获取的性能指标项

(3)通过curl命令获取每个性能指标的值,并将其保存在/tmp/data目录下

(4)通过awk计算平均值,输出结果


该版本缺点:执行时间较长,由于设计逻辑问题,导致逐个指标取N次,实际执行过程中,若有6个指标,则需要执行60次取值,效率较为低下。于是有了版本二的诞生。


版本二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
usage="
Usage: $0 [options...] <url>\n
Options:\n
 -h          Show this help message.\n
 -u <url>    The url to request.\n
 -n <num>    The numbers to request\n
"
    echo -e $usage
    exit 1
fi
                            
num=10
                            
while getopts "n:h:u" arg
do
    case $arg in
        n)
            num=$2
            if [ $num -lt 1 ];then
                num=1
            fi
            ;;
        h)
            echo -e $usage
            exit 1
            ;;
        u)
            url=$4
            ;;
        *)
            echo "Unkown argument"
            exit 1
            ;;
     esac
done
                            
echo "Request url:" ${url}
echo "Request number:" ${num}
echo "------Average Value------"
                            
#定义输出文件、需要获取的指标
outputfile="/tmp/data/requestdata.txt"
[ -d "/tmp/data" ] || mkdir -p /tmp/data
options="%{time_total} %{time_connect} %{time_redirect} %{time_namelookup} %{time_pretransfer} %{time_starttransfer}\n"
                            
#执行n次curl,获取n个数据
for ((i=1;i<=${num};i++))
do
    curl -o /dev/null -s -w "$options" $url >> $outputfile
done
                            
#计算平均值并输出
awk 'BEGIN{tt=0;tc=0;tr=0;tn=0;tp=0;ts=0}{tt+=$1;tc+=$2;tr+=$3;tn+=$4;tp+=$5;ts+=$6}\
END{print \
" time_total = "tt/NR"\n",\
"time_connect = "tc/NR"\n",\
"time_redirect = "tr/NR"\n",\
"time_namelookup = "tn/NR"\n",\
"time_pretransfer = "tp/NR"\n",\
"time_starttransfer = "ts/NR"\n"}' $outputfile
cat /dev/null > $outputfile


脚本执行结果:

1
2
3
4
5
6
7
8
9
10
root@node1:/tmp/shell# ./url.sh -n 10 -u www.yy.com
Request url: www.yy.com
Request number: 10
------Average Value------
 time_total = 0.2569
 time_connect = 0.0279
 time_redirect = 0
 time_namelookup = 0.0058
 time_pretransfer = 0.0279
 time_starttransfer = 0.0569


注:该脚本改进了设计逻辑,在执行curl命令的时候,一次性获取N个指标的值。如此一来,需要10组数据,便只要进行10次取值,提高了执行效率。


版本三

   由于网站可能做了keepalive,或者DNS缓存等等,通过curl一次性获取多组数据实际上有可能数据并不那么准确,较好的办法是每间隔一段时间去取一次值,一段时间之后再运行脚本获取平均值。定时获取执行curl命令需要借助crontab的帮助:

1
2
3
#每分钟获取一次数据
root@node1:~# crontab –e
* * * * *  curl –o /dev/null –s –w %{time_connect} www.yy.com >> /tmp/data/data_collected


计算平均值脚本

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
outputfile="/tmp/data/data_collected"
awk 'BEGIN{tt=0;tc=0;tr=0;tn=0;tp=0;ts=0}{tt+=$1;tc+=$2;tr+=$3;tn+=$4;tp+=$5;ts+=$6}\
END{print \
" time_total = "tt/NR"\n",\
"time_connect = "tc/NR"\n",\
"time_redirect = "tr/NR"\n",\
"time_namelookup = "tn/NR"\n",\
"time_pretransfer = "tp/NR"\n",\
"time_starttransfer = "ts/NR"\n"}' $outputfile
cat /dev/null > $outputfile

0 0
原创粉丝点击