From 6a2d6ac2a56e524314e129894fc5e07ac66b0e4e Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Sat, 20 Feb 2016 14:22:37 +0100 Subject: [PATCH] add and improve @procrat's profile_bash --- local/bin/profile_bash.sh | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 local/bin/profile_bash.sh diff --git a/local/bin/profile_bash.sh b/local/bin/profile_bash.sh new file mode 100755 index 0000000..df46157 --- /dev/null +++ b/local/bin/profile_bash.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +# +# Assumes file descriptor 3 is not messed with in the profiled script. + +pid=$$ +exec 3>&2 2> >(tee /tmp/bash_profile-${pid}.log | \ + sed -u 's/^.*$/now/' | \ + date -f - +%s.%N >/tmp/bash_profile-${pid}.tim) +set -x + +. "$@" + +set +x +exec 2>&3 3>&- + +paste <( + while read tim ;do + crt=000000000$((${tim//.}-10#0$last)) + printf "%12.9f\n" ${crt:0:${#crt}-9}.${crt:${#crt}-9} + last=${tim//.} + done < /tmp/bash_profile-${pid}.tim +) /tmp/bash_profile-${pid}.log > ${pid}.timings + +accumulator=' +{ + sum[$2] += $1 + count[$2] += 1 +} +END { + printf("command\ttotal\ttimes\taverage\n") + for (key in sum) { + printf("%s\t%f\t%d\t%f\n", key, sum[key], count[key], sum[key] / count[key]) + } +} +' + +paste \ + <(cut -f1 ${pid}.timings) \ + <(cat /tmp/bash_profile-${pid}.log \ + | sed '/\] *=/c [[assignement]]' \ + | sed -n 's/++* \([^ ]*\).*/\1/p' \ + | sed '/=/c [[assignment]]') \ + | > ${pid}.acc awk "$accumulator" + +rm /tmp/bash_profile-${pid}.{tim,log} +echo ${pid}.timings