Skip to content

Commit f8c0ec0

Browse files
authored
Add a convenience wrapper for profiling and tracing Go tests. (#2213)
1 parent 957d13a commit f8c0ec0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

etc/profile-test.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
# Convenience wrapper for profiling and tracing Go tests.
3+
set -eu
4+
5+
# Create a temporary file to store the profile or trace data. Trap the exit
6+
# signals to clean it up after the script exits (typically via Ctrl-C).
7+
tmpfile=$(mktemp)
8+
trap 'rm -f "$tmpfile"' EXIT INT TERM
9+
10+
# Set the prompt and options for the select menu.
11+
PS3="Choose a profile type: "
12+
options=("cpu" "mem" "block" "mutex" "trace")
13+
14+
select choice in "${options[@]}"
15+
do
16+
case $choice in
17+
"cpu" | "mem" | "block" | "mutex")
18+
echo "Writing $choice profile to $tmpfile"
19+
20+
go test -${choice}profile ${tmpfile} "$@"
21+
go tool pprof -http=: ${tmpfile}
22+
break
23+
;;
24+
"trace")
25+
echo "Writing trace to $tmpfile"
26+
27+
go test -trace ${tmpfile} "$@"
28+
go tool trace ${tmpfile}
29+
break
30+
;;
31+
*)
32+
echo "Invalid option. Please try again."
33+
;;
34+
esac
35+
done

0 commit comments

Comments
 (0)