Created
March 25, 2025 05:45
-
-
Save vityaman/a154f07cd64a573eada1c6f59660b69e to your computer and use it in GitHub Desktop.
Perf Flamegraph Bash Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
if [ $# -eq 0 ]; then | |
echo "Usage: $0 <path-to-binary>" | |
exit 1 | |
fi | |
BINARY_PATH="$1" | |
BINARY_NAME=$(basename "$BINARY_PATH") | |
TIMESTAMP=$(date +"%Y%m%d_%H%M%S") | |
OUTPUT_DIR="/tmp/perf-flamegraphs" | |
FLAMEGRAPH_PATH="$HOME/dev/app/FlameGraph" | |
mkdir -p "$OUTPUT_DIR" | |
stackcollapse="$FLAMEGRAPH_PATH/stackcollapse-perf.pl" | |
flamegraph="$FLAMEGRAPH_PATH/flamegraph.pl" | |
PERF_DATA="$OUTPUT_DIR/perf.data.$BINARY_NAME.$TIMESTAMP" | |
PERF_FOLDED="$OUTPUT_DIR/out.perf-folded.$BINARY_NAME.$TIMESTAMP" | |
FLAMEGRAPH_OUTPUT="$OUTPUT_DIR/perf.$BINARY_NAME.$TIMESTAMP.svg" | |
echo "Profiling $BINARY_NAME..." | |
perf record -a -g -o "$PERF_DATA" -- "$BINARY_PATH" | |
echo "Generating flamegraph..." | |
perf script -i "$PERF_DATA" | "$stackcollapse" > "$PERF_FOLDED" | |
"$flamegraph" "$PERF_FOLDED" > "$FLAMEGRAPH_OUTPUT" | |
echo "Opening flamegraph in Firefox..." | |
firefox "$FLAMEGRAPH_OUTPUT" | |
echo "Flamegraph saved to: $FLAMEGRAPH_OUTPUT" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment