AWK script to calculate the average from a list of response times

This week I am doing some performance testing on some RedHat Linux boxes. The developer has put some additional log calls so that we know the response time for a call to a particular webservice. It puts a log entry into the log file in the following format

2012-09-24 10:31:24,793 INFO [pool-2-thread-6]: Your WS elapsed (ms): 2011

I wanted a script that would calculate the average from the entries in the log file, now I could wait to the end of the test and put the data back into excel for analysis but I wanted to get an idea earlier in the test. Luckly we can use the grep and awk commands which are part of the operating system to do this for us.

cat yourlogfile.log | grep elapsed| \awk '{sum+=$10} END {print "Count: ",NR;print "Avg :",sum/NR}'

The above line produces output simliar to the below:
Count : 264
Avg : 1857.9

What the code above does it first list out the content of the log file cat yourlogfile.log which is piped to the grep elapsed command that pulls out all the lines with the word elapsed. Next it is sent to awk which adds the values of the 10th column (the response time) to the variable sum {sum+=$10}. At the end of the input stream it print out the Count and Average END {print "Count: ",NR;print "Avg :",sum/NR}. NR being recongnised in awk as the number of records present.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s