本文转载自http://pmghong.blog.51cto.com/3221425/1390599。
1.web主要性能指标
2.curl命令介绍
1
|
root@node1:~
# curl -o /dev/null -s -w %{time_total}"\n" www.yy.com
|
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
//
最后这个值才是我们要的
|
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
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
|
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
|