Skip to content

Instantly share code, notes, and snippets.

@T3sT3ro
Created September 18, 2022 21:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save T3sT3ro/72927cac8a3d1f74715748ffd6c56613 to your computer and use it in GitHub Desktop.
Save T3sT3ro/72927cac8a3d1f74715748ffd6c56613 to your computer and use it in GitHub Desktop.
bash profiling utility - use it as a custom rcfile to get accurate timings for the whole .bashrc loading process
# Based on https://stackoverflow.com/a/20855353/5555799
# Store this file in ~/.bashrc.profiler and add this alias to your .bashrc:
# alias profilebashstartup='exec bash --rcfile ~/.bashrc.profiler'
TRACEFILE=$(mktemp /tmp/trace.XXX)
TIMINGFILE=$(mktemp /tmp/timing.XXX)
STARTTIME=$(date +%s.%N)
exec 3>&2 2> >( tee $TRACEFILE | sed -u 's/^.*$/now/' | date -f - +%s.%N >$TIMINGFILE)
set -x
source ~/.bashrc
set +x
exec 2>&3 3>&-
BASHTRACELOG=$(mktemp /tmp/bashtracelog.XXX)
paste <(awk "{printf \"%f\t%f\",\$1,\$1-$STARTTIME; \$1=\"\"; print}" $TIMINGFILE) $TRACEFILE > $BASHTRACELOG
rm $TRACEFILE $TIMINGFILE
echo "Bash trace log saved to $BASHTRACELOG"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment