#!/bin/bash
HOUR=3600000
MININUT=60000
SECOND=1000
NEED_HOUR=0
#用来解析日期,并将日期转化为毫秒表示
function cal_time()
{
time=$1
hour=${time%%:*}
a=${hour:0:1}
b=${hour:1:1}
let a=$a*10
let hour=$a+$b
if [[ $2 == 0 ]]
then
NEED_HOUR=$hour
fi
if [[ $NEED_HOUR == $hour ]]
then
hour=$HOUR
else
hour=0
fi
# echo "hour=$hour"
min=${time#*:}
min=${min%:*}
a=${min:0:1}
b=${min:1:1}
let a=$a*10
let min=$a+$b
let min=$min*$MININUT
# echo "min=$min"
sec=${time##*:}
sec=${sec%.*}
a=${sec:0:1}
b=${sec:1:1}
let a=$a*10
let sec=$a+$b
let sec=$sec*$SECOND
# echo "sec=$sec"
ms=${time#*.}
a=${ms:0:1}
b=${ms:1:1}
c=${ms:2:1}
let a=$a*100
let b=$b*10
let ms=$a+$b+$c
# echo "ms=$ms"
let ms_times=`expr $hour+$min+$sec+$ms`
# echo "ms_time=$ms_times"
}
function parse_decode()
{
decode_line=$1
pid1=${decode_line#*= }
# echo "pid=$pid1"
#parse time
decode_time=${decode_line#*WARNING}
decode_time=${decode_time#*: }
decode_time=${decode_time%:*}
# echo "decode_time=$decode_time"
#parse frame index
index=${decode_line#*send }
index=${index% pkt*}
# cal_time "$decode_time" "$NEED_HOUR"
save_value "$pid1" "$decode_time"
}
function parse_encode()
{
encode_line=$1
#parse time
encode_time=${encode_line#*WARNING}
encode_time=${encode_time#*: }
encode_time=${encode_time%:*}
#parse frame index
enc_index=${encode_line#*recive }
enc_index=${enc_index% frame*}
#cal_time "$encode_time" "$NEED_HOUR"
echo "$encode_time" >> $DIR/encode.txt
arrayenc[$enc_index]=$encode_time
}
function save_value()
{
signal=0
fileindex=$1
for((i=0;i<$arrays;i++))
do
if [[ $1 == ${arraypid[$i]} ]]
then
signal=1
# fileindex=$i
#exit
i=$arrays
fi
done
if [[ $signal == 0 ]]
then
arraypid[$arrays]=$1
#fileindex=$arrays
((arrays+=1))
fi
echo "$2" >> $DIR/test_$fileindex.txt
}
function enc_dec()
{
((sum+=$2))
echo "index:$1 timeout:$2" >> $DIR/results.txt
if [[ $MAX -lt $2 ]]
then
MAX=$2
fi
if [[ $1 -eq 1 ]]
then
MIN=$2
elif [[ $MIN -gt $2 ]]
then
MIN=$2
fi
}
DIR=/tmp/test_zhang
rm -rf $DIR
mkdir -p $DIR
test_file=$1
arraypid=(0)
arrays=1
str1=pid
str2=encode
arrayenc=(0)
while read rows
do
if [[ $rows =~ $str1 ]]
then
parse_decode "$rows"
elif [[ $rows =~ $str2 ]]
then
parse_encode "$rows"
fi
done < $test_file
#do read pid file and get the result of decode to encode timeout
for((i=1;i<arrays;i++))
do
# echo "need read pid file $DIR/test_${arraypid[$i]}.txt"
j=1
sum=0
MAX=0
MIN=0
while read rows
do
cal_time "${arrayenc[$j]}" "$NEED_HOUR"
encode_time=$ms_times
cal_time "$rows" "$NEED_HOUR"
decode_time=$ms_times
let result=$encode_time-$decode_time
enc_dec "$j" "$result"
((j+=1))
done < $DIR/test_${arraypid[$i]}.txt
let sum=$sum/$((j-=1))
echo "The pid ${arraypid[$i]} from decode to encode timeout is in file $DIR/results.txt"
echo "The average timeout:$sum The MAX timeout:$MAX The MIN timeout:$MIN "
echo ""
done
Linux shell 解析日志文件
最新推荐文章于 2023-04-16 22:12:17 发布