Skip to content

Instantly share code, notes, and snippets.

@gjoseph92
Last active June 6, 2023 13:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gjoseph92/7bfed4d5c372c619af03f9d22e260353 to your computer and use it in GitHub Desktop.
Save gjoseph92/7bfed4d5c372c619af03f9d22e260353 to your computer and use it in GitHub Desktop.
Share speedscope profiles in GitHub issues

An easy way to share speedscope profiles in GitHub issues

Speedscope is an excellent profile viewer. Maybe you're recorded a profile with py-spy and you want to show it to other people in a GitHub issue. The speedscope app can render profiles from a URL, but where do you store the profile?

You can do this easily with gists, githack, and a little script:

Installation:

  1. Install the GitHub CLI
  2. Add gistscope.sh to somewhere on your $PATH (and chmod +x it)

Use:

Record your profile(s). (Tip: give them meaningful filenames.)

$ gh gist create --public profile1.json profile2.json
- Creating gist with multiple files
✓ Created gist profile2.json
https://gist.github.com/e43edda6735bff455343559535acf973

Open the gist in your browser, and for each file, right-click on the Raw button and "Copy link", then paste the raw link as an input to the gistscope command:

$ gistscope https://gist.github.com/gjoseph92/e43edda6735bff455343559535acf973/raw/435de16c7e083dcdf28e6fc9163f9fada4f88ac7/profile1.json https://gist.github.com/gjoseph92/e43edda6735bff455343559535acf973/raw/5f95d64913a1dfdfdb8fc5b65413eabe23113aa9/profile2.json
* [`profile1.json`](https://speedscope.app#profileURL=https%3a%2f%2fgistcdn.githack.com%2fgjoseph92%2fe43edda6735bff455343559535acf973%2fraw%2f435de16c7e083dcdf28e6fc9163f9fada4f88ac7%2fprofile1.json)
* [`profile2.json`](https://speedscope.app#profileURL=https%3a%2f%2fgistcdn.githack.com%2fgjoseph92%2fe43edda6735bff455343559535acf973%2fraw%2f5f95d64913a1dfdfdb8fc5b65413eabe23113aa9%2fprofile2.json)

It'll give you a markdown-formatted list of links that anyone can click on to view your profile in speedscope.app. The profile data is served from your gist over the GitHack CDN (allowing CORS).

macOS tip to copy the output directly to the clipboard:

$ gistscope <input1> <input2> | pbcopy

Obviously the script could be improved to get the raw links for you from the gist, saving you the browser step, but the GitHub CLI doesn't make this easy. So it didn't seem Worth The Time.

#!/bin/bash
# Get links to speedscope profiles uploaded to GitHub gists
# $ gistscope GIST_RAW_URL_1 GIST_RAW_URL_2 ...
set -e
# https://stackoverflow.com/a/10660730
rawurlencode() {
local string="${1}"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
ENCODED="${encoded}"
}
for GIST_RAW_URL in "$@"; do
FILENAME="${GIST_RAW_URL##*/}"
rawurlencode "${GIST_RAW_URL/gist.github.com/gistcdn.githack.com}"
URL="https://speedscope.app#profileURL=$ENCODED"
echo "* [\`$FILENAME\`]($URL)"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment