從變量消除頁面內容:
當我想你的命令,myduration
包含頁面的HTML內容,在我使用的URL我的測試加上時間值。通過添加-s
來抑制進度條並將-o /dev/null
添加到curl
的選項中,我可以將重定向移除到/dev/null
,並且只有在myduration
中保存的時間。
由於myduration
值很可能是短,就可以使用該技術ire_and_curses
顯示哪會經常產生零,其結果是將小於1,你是爲測試(請注意,您的日誌消息說但是「6秒」)。
更高的分辨率:
如果你想有一個更精細的分辨率測試,可以使用的技術像這樣乘以1000 myduration
:
mult1000() {
local floor=${1%.*}
[[ $floor = "0" ]] && floor=''
local frac='0000'
[[ $floor != $1 ]] && frac=${1#*.}$frac
echo ${floor}${frac:0:3}
}
編輯:這個版本的mult1000
正確處理了諸如「0.234」,「1」,「2」,「3.5」, 和「6.789」的值。對於小數點後三位以上的值,無論值爲多少(「1.1119」變爲「1.111」),額外數字都會舍入而不捨入。
您與我上面使用mult1000
(用我自己的例子時間)提到的更改腳本:
myduration=$(curl -s -o /dev/null http://192.168.50.1/mantisbt/view.php?id=1 -w %{time_total}); [[ $(mult1000 $myduration) -gt 3500 ]] && echo "`date +'%y%m%d%H%M%S'` took more than 3.5 seconds to load the page http://192.168.50.1/mantisbt/view.php?id=1 " >> /home/shantanu/speed_report.txt
這被分成多行(和簡化的),使其在這個答案更易讀這裏:
myduration=$(curl -s -o /dev/null http://example.com -w %{time_total})
[[ $(mult1000 $myduration) -gt 3500 ]] &&
echo "It took more than 3.5 seconds to load thttp://example.com" >> report.txt
上限函數實際上是錯誤的,因爲
./int.sh 12.0
將返回13.0根據上限函數的定義返回12(最小整數不小於x) – dm76 2014-12-12 14:45:22@ dm76 - 是的,該邊界情況是一個好的觀點。 – 2014-12-12 16:11:14