Performance profiling tools for shell scripts

I'm attempting to speed up a collection of scripts that invoke subshells and do all sorts of things. I was wonder if there are any tools available to time the execution of a shell script and its nested shells and report on which parts of the script are the most expensive.

For example, if I had a script like the following.

#!/bin/bash

echo "hello"
echo $(date)
echo "goodbye"

I would like to know how long each of the three lines took. time will only only give me total time for the script. bash -x is interesting but does not include timestamps or other timing information.

Answers


You can set PS4 to show the time and line number. Doing this doesn't require installing any utilities and works without redirecting stderr to stdout.

For this script:

#!/bin/bash -x
# Note the -x flag above, it is required for this to work
PS4='$(date "+%s.%N ($LINENO) + ")'
for i in {0..2}
do
    echo $i
done
sleep 1
echo done

The output looks like:

+ PS4='$(date "+%s.%N ($LINENO) + ")'
1291311776.108610290 (3) + for i in '{0..2}'
1291311776.120680354 (5) + echo 0
0
1291311776.133917546 (3) + for i in '{0..2}'
1291311776.146386339 (5) + echo 1
1
1291311776.158646585 (3) + for i in '{0..2}'
1291311776.171003138 (5) + echo 2
2
1291311776.183450114 (7) + sleep 1
1291311777.203053652 (8) + echo done
done

This assumes GNU date, but you can change the output specification to anything you like or whatever matches the version of date that you use.

Note: If you have an existing script that you want to do this with without modifying it, you can do this:

PS4='$(date "+%s.%N ($LINENO) + ")' bash -x scriptname

Need Your Help

Dynamically changing variables within object dependent on DOM

javascript dom object memory

I would like to have an object that returns status true/false based on the presence of an element in the DOM. I can't get the variable to actually maintain a state other than the state of the loaded

For iOS can you force SMS over MMS programmatically

ios sms mms

I have a iOS app that is using the standard MFMessageComposeViewController to send a text with URL in it to people. Looking at the iPhone settings you can turn on MMS. Is there a way to force sen...