really apply @procrat's tweaks of my accumulation

This commit is contained in:
Felix Van der Jeugt 2016-02-22 09:11:07 +01:00
parent 1bc6bf9b54
commit db3b68c22e

49
local/bin/profile_bash.sh Executable file → Normal file
View File

@ -1,11 +1,15 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# #
# Assumes file descriptor 3 is not messed with in the profiled script. # Writes profiling info (resp. accumulated info) to $$.timings (resp.
# $$.acc_timings) in working directory. The concrete filenames of both files
# are written to stderr.
#
# This script assumes file descriptor 3 is not messed with in the profiled
# script, nor with the xtrace option of bash.
pid=$$ exec 3>&2 2> >(tee /tmp/bash-profile-$$.log | \
exec 3>&2 2> >(tee /tmp/bash_profile-${pid}.log | \
sed -u 's/^.*$/now/' | \ sed -u 's/^.*$/now/' | \
date -f - +%s.%N >/tmp/bash_profile-${pid}.tim) date -f - +%s.%N >/tmp/bash-profile-$$.tim)
set -x set -x
. "$@" . "$@"
@ -18,29 +22,28 @@ paste <(
crt=000000000$((${tim//.}-10#0$last)) crt=000000000$((${tim//.}-10#0$last))
printf "%12.9f\n" ${crt:0:${#crt}-9}.${crt:${#crt}-9} printf "%12.9f\n" ${crt:0:${#crt}-9}.${crt:${#crt}-9}
last=${tim//.} last=${tim//.}
done < /tmp/bash_profile-${pid}.tim done < /tmp/bash-profile-$$.tim
) /tmp/bash_profile-${pid}.log > ${pid}.timings ) /tmp/bash-profile-$$.log > $$.timings
accumulator=' paste \
{ <(cut -f1 $$.timings) \
sum[$2] += $1 <(cat /tmp/bash-profile-$$.log | \
sed 's/^+\+ //' | \
sed 's/^\(local\s\|declare\|\S\+=\).*/[[assignment]]/') | \
tail -n +2 | \
awk '{
cumtime[$2] += $1
count[$2] += 1 count[$2] += 1
} }
END { END {
printf("command\ttotal\ttimes\taverage\n") printf("cumtime\tcalls\tavg.time\tcommand\n")
for (key in sum) { for (key in cumtime) {
printf("%s\t%f\t%d\t%f\n", key, sum[key], count[key], sum[key] / count[key]) printf("%f\t%d\t%f\t%s\n", cumtime[key], count[key],
cumtime[key] / count[key], key)
} }
} }' | \
' sort -k1gr >$$.acc_timings
paste \ rm /tmp/bash-profile-$$.{tim,log}
<(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 $$.timings $$.acc_timings >&2
echo ${pid}.timings