心理學中第一印象效應非常重要,以後這個印象會產生主導位置影響人們的判斷。在瀏覽器接收到回車到”唰”頁面出來這個過程中就那麼幾秒鐘的時間就是客戶訪問這個網站的第一印象。
根據統計分析,有這麼一個現象:如果一個網站的頁面加載時間在5秒鐘或者更少,用戶離開(以關閉頁面爲準)的比例通常不會超過20%,而超過了五秒鐘,用戶流失的比例會按照每秒1-2%增加。一般的電腦都能在3-10秒內完成頁面的加載。因此,網站的打開速度是直接影響到用戶是否願意接受這個網站的一大要素。
通常需要關注的有如下幾個性能指標:
2.curl命令介紹
curl支持包括http,https,ftp在內的衆多協議,它還支持POST,cookie認證,從指定偏移處下載部分文件,參照頁,用戶代理字符串,擴展頭部,限速,文件大小限制,進度條等特性。下面主要介紹如何通過curl 命令獲取到網站的性能指標信息:
root@node1:~# curl -o /dev/null -s -w %{time_total}"\n" www.yy.com
命令解釋:
-o //將curl獲取到的頁面代碼重定向到/dev/null、
-s //靜默模式,若不加該參數,會有類似於如下的顯示:
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.獲取網站性能腳本
版本一
#!/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
執行結果如下:
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次取值,效率較爲低下。於是有了版本二的誕生。
版本二
#!/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
腳本執行結果:
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的幫助:
#每分鐘獲取一次數據 root@node1:~# crontab –e * * * * * curl –o /dev/null –s –w %{time_connect} www.yy.com >> /tmp/data/data_collected
計算平均值腳本
#!/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