-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackprof-cli.sh
More file actions
122 lines (113 loc) · 2.96 KB
/
stackprof-cli.sh
File metadata and controls
122 lines (113 loc) · 2.96 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/sh
#
# Simple Stackprof CLI
# Inspired by https://github.com/quirkey/stackprof-remote
#
load() {
if [[ "$1" != "$fileName" ]]; then
fileName="$1"
baseFileName="$(basename "$fileName")"
echo "Loaded: $fileName"
else
echo "Already loaded, nothing to do."
fi
}
printError() {
echo "$(tput setaf 1)$1$(tput sgr 0)"
}
# Init
if [[ $# -eq 0 ]] ; then
echo "Usage: $(basename "$0") file"
exit 1
fi
# enable extended pattern matching for commands
shopt -s extglob
#
# Simple Stackprof CLI
# Inspired by https://github.com/quirkey/stackprof-remote
#
# Init
if [[ $# -eq 0 ]] ; then
echo "Usage: $(basename $0) file"
exit 1
fi
# enable extended pattern matching for commands
shopt -s extglob
# Print header
echo "Use CTRL+C or 'q' to exit, 'help' for help"
load "$1"
echo
# Prompt
while true; do
read -e -p "[$baseFileName]>> " cmd
history -s "$cmd"
case "$cmd" in
# ([fF]|[fF]iles)?([:space:])*)
[fF]?("iles")?([:space:])*)
top=$(echo "$cmd" | awk '{print $2}')
if ! [[ -z $top ]]; then
limit="--limit $top"
fi
stackprof $fileName --files $limit
unset top limit
;;
[hH]?("elp"))
echo " $(tput bold)f$(tput sgr 0)iles Show files. Optionally, pass number of results."
echo " $(tput bold)l$(tput sgr 0)oad path Sets new file to work with."
echo " $(tput bold)m$(tput sgr 0)ethod grep Show details about method."
echo " $(tput bold)t$(tput sgr 0)op n Show top methods ordered by inner sample time."
echo " $(tput bold)to$(tput sgr 0)tal n Show top methods ordered by total time."
echo
echo " $(tput bold)h$(tput sgr 0)elp Help."
echo " $(tput bold)q$(tput sgr 0)uit Quit."
;;
[lL]?("oad")?([:space:])*)
newFileName=$(echo "$cmd" | awk '{print $2}')
if ! [[ -z $newFileName ]]; then
if [[ -f $newFileName ]]; then
load "$newFileName"
else
printError "File not found. File: '$newFileName'."
fi
else
printError "Load requires a file name."
fi
unset newFileName
;;
[mM]?("ethod")?([:space:])*)
name=$(echo "$cmd" | awk '{print $2}')
if ! [[ -z $name ]]; then
stackprof $fileName --method "$name"
else
printError "Method requires a method name grep expression."
fi
unset name
;;
[tT]?("op")?([:space:])*)
top=$(echo "$cmd" | awk '{print $2}')
if [[ $top == +([0-9]) ]]; then
stackprof $fileName --limit $top
else
printError "Top requires a numeric value, received '$top'."
fi
unset top
;;
[tT]o?("tal")?([:space:])*)
top=$(echo "$cmd" | awk '{print $2}')
if ! [[ -z $top ]]; then
limit="--limit $top"
fi
stackprof $fileName --sort-total $limit
unset top limit
;;
[qQ]?("uit"))
echo
echo " Bye!"
exit 0
;;
*)
printError "Unknown command '$cmd'."
;;
esac
echo
done