Time Your Bash Scripts and Procedures From Inside the Code - JohnHau/mis GitHub Wiki

In general, one may use the time Bash utility (see man time for more information) to run a program, and obtain runtime duration and system resource usage summaries. But how can one time particular sections of code, directly from within the Bash source code?

Using some easy variable assignments and calculations, it is possible to achieve accurate timing metrics for Bash script executions.

In this tutorial you will learn:

How to time Bash scripts using variable assignments and calculations How to use overlapping timers to time specific sections of your scripts Examples which exemplify how specific sections of code can be timed

image

image

image

image

image

image

image

Example 3: Overlapping Timers Let’s now look at a final example which has overlapping timers and times a function.test5.sh:

#!/bin/bash my_sleep_function(){ sleep 1 } OVERALL_START="$(date +%s)" FUNCTION_START="$(date +%s)" my_sleep_function FUNCTION_END="$(date +%s)" sleep 2 OVERALL_END="$(date +%s)" echo "The function part of the code took: $[ ${FUNCTION_END} - ${FUNCTION_START} ] seconds to run" echo "The overall code took: $[ ${OVERALL_END} - ${OVERALL_START} ] seconds to run" Here we define a function my_sleep_function which simply sleeps for one second. We next set an overall start timer by using the OVERALL_START variable and again our date +%s in a subshell. Next we start another timer (the function timer based on the FUNCTION_START variable). We run the function and end immediately end the function timer by setting the FUNCTION_END variable.

We then do an additional sleep 2 and then end the overall timer by setting the OVERALL_END timer. Finally, we output the information in a nice format near the end of the script. The two echo statements are not part of the timing, but their runtime would be minimal; usually we are attempting to time various and specific sections of our code which tend to have long durations like extensive loops, external program calls, many subshells etc.

Let’s look at the out of test5.sh:

$ ./test5.sh The function part of the code took: 1 seconds to run The overall code took: 3 seconds to run

Looks good. The script correctly timed the function to 1 second, and the overall script runtime as 3 seconds, being the combination of the function call and the extra two second sleep.

Note that if the function is recursively, it may make sense to use an additional global timing variable to which the function runtime can be added unto. You could also count the number of functions calls and then in the end divide the number of function calls by using bc (ref How To Make Decimal Calculations In Bash Using Bc). In this use case, it may make be best to move the start and stop timers, as well as the function duration calculation to inside the function. It makes for cleaner and clearer code and may eliminate unnecessary code duplication.

Conclusion In this article, we looked at timing various parts of our Bash script code by using date +%s as a basis for obtaining seconds since epoch time, as well as one or more variable assignments to calculate performance timings one or more sections of the code. Using these basic building blocks, one can make complex timing measurement structures, per function, per called script or even timers which overlap (for example one per script as well as one per function etc.) by using different variables. Enjoy!

If you are interested in learning more about Bash, please see our Useful Bash Command Line Tips and Tricks series.