-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile-shell-script.sh
More file actions
executable file
·76 lines (63 loc) · 1.49 KB
/
profile-shell-script.sh
File metadata and controls
executable file
·76 lines (63 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env bash
OPTIND=1
SHOW_COLOR=false
DELETE_LOGS=false
REDISPLAY=false
THRESHOLD="0.0005"
while getopts "cDrt:" opt; do
case "$opt" in
c)
SHOW_COLOR=true
;;
D)
DELETE_LOGS=true
;;
r)
REDISPLAY=true
;;
t)
THRESHOLD=$OPTARG
;;
*)
;;
esac
done
shift $((OPTIND-1))
if $DELETE_LOGS; then
rm /tmp/sample-time*
exit 0
fi
if $SHOW_COLOR; then
COLOR_CMD="awk -v threshold=$THRESHOLD "
# shellcheck disable=SC2016
COLOR_CMD+=''\''{if ($1 > threshold) print "\033[1;31m" $0; else print "\033[0m" $0; fi;}'\'''
else
COLOR_CMD='cat'
fi
SCRIPT="$1"
shift
if ! $REDISPLAY; then
# Profiling
exec 3>&2 2> >(tee /tmp/sample-time.$$.log |
sed -u 's/^.*$/now/' |
date -f - +%s.%N >/tmp/sample-time.$$.tim)
set -x
# shellcheck source=/dev/null
source "$SCRIPT"
# Profiling
set +x
exec 2>&3 3>&-
fi
readarray -n 2 FILE_NAMES< <(find /tmp -type f -name "sample-time*" -printf '%T@ %p\n' 2>/dev/null \
| sort -n | tail -2 | cut -f2- -d" "); \
LOG_FILE="${FILE_NAMES[0]//[$'\t\r\n']}" \
TIME_FILE="${FILE_NAMES[1]//[$'\t\r\n']}"
paste <(
while read -r tim ;do
crt=000000000$((${tim//.}-10#0$last))
printf "%12.9f\n" ${crt:0:${#crt}-9}.${crt:${#crt}-9}
last=${tim//.}
done < <(tail -n +2 "$TIME_FILE")
) <(head -n -1 "$LOG_FILE") | eval $COLOR_CMD
printf "\033[0m"
echo "Total" "$(bc -l <<< "$(tail -n 1 "$TIME_FILE") - $(head -n 1 "$TIME_FILE")")"