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
1 changed files with 30 additions and 27 deletions

57
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='
{
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 \ paste \
<(cut -f1 ${pid}.timings) \ <(cut -f1 $$.timings) \
<(cat /tmp/bash_profile-${pid}.log \ <(cat /tmp/bash-profile-$$.log | \
| sed '/\] *=/c [[assignement]]' \ sed 's/^+\+ //' | \
| sed -n 's/++* \([^ ]*\).*/\1/p' \ sed 's/^\(local\s\|declare\|\S\+=\).*/[[assignment]]/') | \
| sed '/=/c [[assignment]]') \ tail -n +2 | \
| > ${pid}.acc awk "$accumulator" awk '{
cumtime[$2] += $1
count[$2] += 1
}
END {
printf("cumtime\tcalls\tavg.time\tcommand\n")
for (key in cumtime) {
printf("%f\t%d\t%f\t%s\n", cumtime[key], count[key],
cumtime[key] / count[key], key)
}
}' | \
sort -k1gr >$$.acc_timings
rm /tmp/bash_profile-${pid}.{tim,log} rm /tmp/bash-profile-$$.{tim,log}
echo ${pid}.timings
echo $$.timings $$.acc_timings >&2