Skip to content

Instantly share code, notes, and snippets.

@jamesmartin
Last active December 20, 2015 16:19
Show Gist options
  • Select an option

  • Save jamesmartin/6160689 to your computer and use it in GitHub Desktop.

Select an option

Save jamesmartin/6160689 to your computer and use it in GitHub Desktop.
Calculate the duration (in seconds) of a process based on its log file entries.
timestamp() {
date -j -f "%Y-%m-%d%H:%M:%S" $* +%s
}
extract_time_field(){
echo $* | awk '{print $1$2}' | awk -F',' '{print $1}'
}
for logfile in `ls *.log`; do
starttime=$(extract_time_field $(cat $logfile | head -n1))
endtime=$(extract_time_field $(cat $logfile | tail -n1))
difference=$(expr $(timestamp $endtime) - $(timestamp $starttime))
echo "$logfile: Duration (seconds): $difference"
done
@jamesmartin
Copy link
Copy Markdown
Author

The input log files are expected to be in the following format (only the date/time stamps are important):

2013-08-03 23:01:03,452 ... first message.
Etc.
2013-08-03 23:00:01,562 ... last message.

Put all the log files in the same directory as the script and run it.

Milliseconds are discarded.

@jamesmartin
Copy link
Copy Markdown
Author

Example output:

foo.log: Duration (seconds): 79
bar.log: Duration (seconds): 4
baz.log: Duration (seconds): 33
qux.log: Duration (seconds): 74

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment