Skip to content

Instantly share code, notes, and snippets.

@ttacon
Last active November 30, 2016 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttacon/601b12a316efbb9eb2a8c48814bc7bdc to your computer and use it in GitHub Desktop.
Save ttacon/601b12a316efbb9eb2a8c48814bc7bdc to your computer and use it in GitHub Desktop.
profile.sh - Harness script for remotely profiling node process on AWS EC2 instances
#!/bin/bash
#
# See: https://mixmax.com/blog/determining-why-that-server-is-on-fire for details.
#
# Example usage: ./profile.sh $INSTANCE_ID [$DURATION] [$PID]
# Where:
# - $INSTANCE_ID is the EC2 identifier of the instance to profile the node
# processes on.
# - $DURATION is an optional duration specified in seconds (defaults to 30).
# - $PID is a specific process ID to profile, by default we profile all `node`
# process on the remote instance.
#
# It is also assumed that you have setup your AWS credentials so that the
# `aws` cli functions correctly.
#
# This script currently assumes that you are profiling EC2 instances inside
# a private VPC and that you ahve setup a bastion tier with SSH forwarding
# appropriately. If this is not the case, simply change
# s/PrivateDnsName/PublicDnsName/.
#
##########
set -e
INSTANCE=$1
DURATION=${2:-30}
PID_MATCHER="echo $3"
if [ -z ${3+x} ]; then
PID_MATCHER='pgrep node | xargs | sed -e "s\/ \/,\/g"'
fi
echo "building perf script..."
sed -e "s/DURATION/$DURATION/g" perf.sh > perf.tmp.sh
sed -i -e "s/PID_MATCHER/$PID_MATCHER/g" perf.tmp.sh
IP=$(aws ec2 describe-instances --instance-ids $INSTANCE --query "Reservations[*].Instances[*].PrivateDnsName" --output text)
# Collect a sample from the remote node
echo "running the debug script on the remote node: $INSTANCE"
ssh ec2-user@$IP 'bash -s' < perf.tmp.sh
# Cleanup after ourselves
rm -f perf.tmp.sh perf.tmp.sh-e
# Retrieve the svg from remote node
echo "copying the flamegraph back from the remote node..."
scp ec2-user@$IP:/home/ec2-user/flame.svg .
# Open the image in the browser
echo "opening the flamegraph in Chrome..."
open flame.svg -a Google\ Chrome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment