I have a bash script that is calculating response time for specific java methods from given log.
The script is constituted by 2 loops, one is iterating through last 5 minutes for (( i = 5; i > 0 ; i--))
and identifies the "ID's" which I need for the 2nd loop so I can match the starting and ending times. At the second loop I'm doing the calculations diff = $(($endTime - $startTime)); echo $diff
Now I want to get the average response time from all results from last 5 minutes. This can be easily done with awk if I call my script ./responseTime.sh | awk '{sum+=$1} END {print sum / NR}'
How can I calculate the avg without using this pipe (I don't know the number of results which I will get for every 5 minutes it runs)?
I do not have such log at my local machine to show you how it works. Here is my script
#!/bin/bash
logfile=/var/log/examples.txt
#loop through last 5 min
for (( i = 5; i >=0; i-- ))
do
debugDate=$(date --date="$i minutes ago" "+%b%d %H:%M")
folderId=$(grep "$debugDate" $logfile | sed -rn '/.*folderId:([^ ]*).*/ s//\1/p' ) # identify id's and store results
for j in $folderId
do
startTime=$(grep "$j" $logfile | grep "Starting" | awk '{split ($2,Time,":"); print Time[1]*3600000+Time[2]*60000+Time[3]*1000}') #get the value in ms
endTime=$(grep "$j" $logfile | grep "process finished" | awk '{split ($2,Time,":"); print Time[1]*3600000+Time[2]*60000+Time[3]*1000}')
if [ ${#startTime} -ne 8 ] || [ ${#endTime} -ne 8 ] #exclude midnight results and retries
then
continue
fi
responseTime=$(($endTime - $startTime))
echo $responseTime #test results
done
done
cat examples.txt
[Jan12 18:26:00] Initialized session for folderId:8005
[Jan12 18:26:15] Starting process [8005]
[Jan12 18:27:00] Initialized session for folderId:8004
[Jan12 18:27:17] Starting process [8004]
[Jan12 18:28:00] Initialized session for folderId:8003
[Jan12 18:28:16] Starting process [8003]
[Jan12 18:29:00] Initialized session for folderId:8002
[Jan12 18:29:15] Starting process [8002]
[Jan12 18:30:00] Initialized session for folderId:8001
[Jan12 18:30:12] Starting process [8001]
[Jan12 18:31:00] Initialized session for folderId:8000
[Jan12 18:31:16] Starting process [8000]
[Jan12 18:26:31] process finished [8005]
[Jan12 18:27:41] process finished [8004]
[Jan12 18:28:55] process finished [8003]
[Jan12 18:29:49] process finished [8002]
[Jan12 18:30:33] process finished [8001]
[Jan12 18:31:35] process finished [8000]