track time for a subroutine within a shell-script
Posted: July 10th, 2008 | Author: tolleiv | Tags: bash | Comments OffThe following code-snippet is a shell-script which does the following:
- Track the time for a block of shell commands
- Check if the time was less than x seconds (the example uses 10 seconds)
- If the block run through too fast the script waits/sleeps a few seconds
- Run all this within a loop so that the block of shell-commands is executed periodically
I used the script combined with a PHP script which processes a queue. The PHP script processes 1000 elements from the queue and takes about 30 seconds for that. Since just having a cronjob per minute would be not efficient enought I used this script.
The waiting-block is necessary because now and then the queue is empty … but I think there are lot’s of situations where a script like this can be usefull:
#!/bin/bash while [ 1 -ge 0 ]; do time_begin=`date +%s` ###BLOCK 2 TRACK - BEGIN number=$RANDOM let "number %= 20" sleep $number ###BLOCK 2 TRACK - END time_end=`date +%s` total=$((time_end-time_begin)) if [[ $total -ge 10 ]]; then echo "time taken was: $number : $total" else echo "time take was too less $number : $total" sleep 10 fi done
