|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +NAMESPACE="unicorn-store-spring" |
| 5 | +DURATION=60 |
| 6 | +EVENT="${1:-wall}" # default: wall, you can pass cpu, alloc, lock... |
| 7 | +RPS=200 |
| 8 | + |
| 9 | +# --- Find pod and PID --- |
| 10 | +POD_NAME=$(kubectl get pods -n $NAMESPACE -o jsonpath='{.items[0].metadata.name}') |
| 11 | +PID=$(kubectl exec $POD_NAME -n $NAMESPACE -- jps | grep store-spring | awk '{print $1}') |
| 12 | + |
| 13 | +SVC_URL=$(~/java-on-aws/infrastructure/scripts/test/getsvcurl.sh eks) |
| 14 | +echo "Using pod=$POD_NAME, PID=$PID, event=$EVENT, duration=${DURATION}s, target=$SVC_URL" |
| 15 | + |
| 16 | +# --- Cleanup old profiles --- |
| 17 | +kubectl exec $POD_NAME -n $NAMESPACE -- sh -c "rm -f /tmp/profile-*.html /tmp/asprof.log || true" |
| 18 | + |
| 19 | +# --- Trap for cleanup --- |
| 20 | +cleanup() { |
| 21 | + echo "--- Caught interrupt, cleaning up ---" |
| 22 | + if [[ -n "${BENCH_PID:-}" ]]; then |
| 23 | + kill "$BENCH_PID" 2>/dev/null || true |
| 24 | + fi |
| 25 | + # Try to stop profiler gracefully |
| 26 | + kubectl exec $POD_NAME -n $NAMESPACE -- \ |
| 27 | + /async-profiler/bin/asprof stop -f /tmp/profile-interrupted.html $PID \ |
| 28 | + >/dev/null 2>&1 || true |
| 29 | + # Copy interrupted profile if it exists |
| 30 | + if kubectl exec $POD_NAME -n $NAMESPACE -- test -f /tmp/profile-interrupted.html; then |
| 31 | + kubectl cp "$NAMESPACE/$POD_NAME:/tmp/profile-interrupted.html" "./profile-${EVENT}-interrupted-$(date +%Y%m%d-%H%M%S).html" |
| 32 | + echo "⚠️ Interrupted: partial profile saved." |
| 33 | + fi |
| 34 | + exit 1 |
| 35 | +} |
| 36 | +trap cleanup INT TERM |
| 37 | + |
| 38 | +echo "--- Start benchmark in background ---" |
| 39 | +~/java-on-aws/infrastructure/scripts/test/benchmark.sh $SVC_URL $DURATION $RPS & |
| 40 | +BENCH_PID=$! |
| 41 | + |
| 42 | +echo "--- Run profiler (same user as JVM) ---" |
| 43 | +kubectl exec $POD_NAME -n $NAMESPACE -- \ |
| 44 | + /async-profiler/bin/asprof -d $DURATION -e $EVENT -f /tmp/profile-%t.html $PID \ |
| 45 | + || true |
| 46 | + |
| 47 | +# --- Wait for benchmark to finish --- |
| 48 | +wait $BENCH_PID || true |
| 49 | + |
| 50 | +echo "--- Fetch newest profile ---" |
| 51 | +PROFILE_FILE=$(kubectl exec $POD_NAME -n $NAMESPACE -- \ |
| 52 | + sh -c "ls -t /tmp/profile-*.html | head -1" | tr -d '\r') |
| 53 | +LOCAL_FILE="./profile-${EVENT}-$(date +%Y%m%d-%H%M%S).html" |
| 54 | + |
| 55 | +kubectl cp "$NAMESPACE/$POD_NAME:$PROFILE_FILE" "$LOCAL_FILE" |
| 56 | + |
| 57 | +echo "🔥 Profile saved as $LOCAL_FILE" |
| 58 | +echo "👉 Open it in your browser to view the flame graph" |
0 commit comments