本脚本主要实现实验过程中求实验执行的时间差,进行数据的分析,以下为shell 脚本
starttime=`date +'%Y-%m-%d %H:%M:%S'`
ipfs add go-ipfs_v0.10.0_linux-amd64.tar.gz
ipfs add go1.11.5.linux-amd64.tar.gz
endtime=`date +'%Y-%m-%d %H:%M:%S'`
echo $starttime $endtime
start_seconds=$(date --date="$starttime" +%s);
end_seconds=$(date --date="$endtime" +%s);
echo "本次运行时间: "$((end_seconds-start_seconds))"s"
function getTiming(){
start=$1
end=$2
start_s=$(echo $start | cut -d '.' -f 1)
start_ns=$(echo $start | cut -d '.' -f 2)
end_s=$(echo $end | cut -d '.' -f 1)
end_ns=$(echo $end | cut -d '.' -f 2)
time=$(( ( 10#$end_s - 10#$start_s ) * 1000 + ( 10#$end_ns / 1000000 - 10#$start_ns / 1000000 ) ))
echo "$time ms"
}
start=$(date +%s.%N)
ipfs get QmWrZAyfLFZM4GWCiRrRh4ZXAiFQFzuLxABcFstAxWD4Sx
end=$(date +%s.%N)
result=$(getTiming $start $end)
echo $start $end
echo $result
完善版本:从文件获取执行指令,执行后将结果输出到文件
function getTiming(){
start=$1
end=$2
start_s=$(echo $start | cut -d '.' -f 1)
start_ns=$(echo $start | cut -d '.' -f 2)
end_s=$(echo $end | cut -d '.' -f 1)
end_ns=$(echo $end | cut -d '.' -f 2)
time=$(( ( 10#$end_s - 10#$start_s ) * 1000 + ( 10#$end_ns / 1000000 - 10#$start_ns / 1000000 ) ))
echo "$time"
}
sum=0
num=0
avg=0
while read line
do
start=$(date +%s.%N)
echo $line | sh>>result.txt
end=$(date +%s.%N)
result=$(getTiming $start $end)
echo $start $end
echo "上传所需时间:$result ms">>result.txt
sum=`expr $sum + $result`
num=`expr $num + 1`
done < my.txt
echo "总时间:$sum">>result.txt
echo "总执行次数:$num">>result.txt
avg=`awk -v x=$sum -v y=$num 'BEGIN{printf "%.2f\n",x / y}'`
echo "平均时间:$avg">>result.txt