How do you time sections of a script and the script as a whole?
BeginnerConceptFunctions, exit codes, strict mode and traps
Answer
SECONDS=0
build; echo "build: ${SECONDS}s"
start=$EPOCHREALTIME # Bash 5: microsecond precision
deploy
end=$EPOCHREALTIME
echo "deploy: $(awk "BEGIN {printf \"%.2f\", $end - $start}")s"
time ./deploy.sh # real, user, sys for the whole thing
Send timings to logs or a metrics endpoint so you can see deploys getting slower over weeks.
More on Functions, exit codes, strict mode and traps
How do you define a function, pass arguments and return a value?BeginnerWhat does `local` do, and what is dynamic scoping?AdvancedWhy is `local out=$(cmd)` a problem under `set -e`?AdvancedWhat are the conventional exit codes, and what do 126, 127, 130, 137 and 143 mean?BeginnerWhat does `set -e` do, and where does it NOT cause an exit?AdvancedExplain `set -euo pipefail` line by line, and add the `-E` flag.Beginner