|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +BOOT="false" |
| 5 | +POSITIONAL=() |
| 6 | +for arg in "$@"; do |
| 7 | + if [[ "$arg" == "--boot" ]]; then |
| 8 | + BOOT="true" |
| 9 | + else |
| 10 | + POSITIONAL+=("$arg") |
| 11 | + fi |
| 12 | +done |
| 13 | +set -- "${POSITIONAL[@]}" |
| 14 | + |
| 15 | +SPAN="${1:?span name required}" |
| 16 | +RUNS="${2:-10}" |
| 17 | +PLATFORM="${3:-ios}" |
| 18 | +APP_ID="${APP_ID:-com.expensify.chat.dev}" |
| 19 | +LOG_PID="" |
| 20 | +FLOW_ENV_ARGS=() |
| 21 | +RESET_FLOW="" |
| 22 | + |
| 23 | +if [[ "$PLATFORM" != "ios" && "$PLATFORM" != "android" ]]; then |
| 24 | + echo "Platform must be 'ios' or 'android'." >&2 |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | + |
| 28 | +if ! [[ "$RUNS" =~ ^[0-9]+$ ]] || [[ "$RUNS" -lt 1 ]]; then |
| 29 | + echo "Runs must be a positive integer." >&2 |
| 30 | + exit 1 |
| 31 | +fi |
| 32 | + |
| 33 | +REPO="$(git rev-parse --show-toplevel)" |
| 34 | +FLOWS_DIR="$REPO/.claude/skills/agent-device/flows" |
| 35 | +FLOW="" |
| 36 | +while IFS= read -r -d '' candidate; do |
| 37 | + if grep -q "^# @tag[[:space:]]\+sentry-${SPAN}\$" "$candidate" 2>/dev/null; then |
| 38 | + FLOW="$candidate" |
| 39 | + break |
| 40 | + fi |
| 41 | +done < <(find "$FLOWS_DIR" -name '*.ad' -type f -print0 2>/dev/null) |
| 42 | + |
| 43 | +if [[ -z "$FLOW" ]]; then |
| 44 | + echo "No flow declares '@tag sentry-$SPAN'. Available:" >&2 |
| 45 | + find "$FLOWS_DIR" -name '*.ad' -type f -exec grep -h '^# @tag[[:space:]]\+sentry-' {} + 2>/dev/null | sed 's/.*sentry-//' | sort -u >&2 |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | + |
| 49 | +RESET_DECL=$(grep -E '^# @reset[[:space:]]+' "$FLOW" | sed -E 's/^# @reset[[:space:]]+//' | head -1 || true) |
| 50 | +if [[ -n "$RESET_DECL" ]]; then |
| 51 | + if [[ "$RESET_DECL" = /* ]]; then |
| 52 | + RESET_FLOW="$RESET_DECL" |
| 53 | + else |
| 54 | + RESET_FLOW="$REPO/$RESET_DECL" |
| 55 | + fi |
| 56 | + |
| 57 | + if [[ ! -f "$RESET_FLOW" ]]; then |
| 58 | + echo "Reset flow does not exist: $RESET_FLOW" >&2 |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | +fi |
| 62 | + |
| 63 | +TMP_DIR="$(mktemp -d)" |
| 64 | +DURATIONS_FILE="$TMP_DIR/durations.txt" |
| 65 | + |
| 66 | +cleanup() { |
| 67 | + if [[ -n "$LOG_PID" ]]; then |
| 68 | + kill "$LOG_PID" 2>/dev/null || true |
| 69 | + fi |
| 70 | + rm -rf "$TMP_DIR" |
| 71 | +} |
| 72 | +trap cleanup EXIT |
| 73 | + |
| 74 | +# Flow `# @param KEY description` headers: only AD_<KEY> overrides are passed via `-e KEY=VALUE`. |
| 75 | +append_flow_env_from_ad_vars() { |
| 76 | + local param_lines line key env_key |
| 77 | + param_lines=$(grep -E '^# @param[[:space:]]+[A-Za-z_][A-Za-z0-9_]*' "$FLOW" || true) |
| 78 | + if [[ -z "$param_lines" ]]; then |
| 79 | + return |
| 80 | + fi |
| 81 | + |
| 82 | + while IFS= read -r line; do |
| 83 | + key=$(echo "$line" | sed -E 's/^# @param[[:space:]]+([A-Za-z_][A-Za-z0-9_]*).*/\1/') |
| 84 | + env_key="AD_${key}" |
| 85 | + if [[ -n "${!env_key:-}" ]]; then |
| 86 | + FLOW_ENV_ARGS+=("-e" "$key=${!env_key}") |
| 87 | + echo "Replay param: $key from $env_key" >&2 |
| 88 | + fi |
| 89 | + done <<< "$param_lines" |
| 90 | +} |
| 91 | + |
| 92 | +start_log() { |
| 93 | + local out="$1" |
| 94 | + if [[ "$PLATFORM" == "ios" ]]; then |
| 95 | + xcrun simctl spawn booted log stream --level debug \ |
| 96 | + --predicate "eventMessage CONTAINS \"[Sentry][$SPAN] Ending span\"" > "$out" & |
| 97 | + else |
| 98 | + adb logcat -c |
| 99 | + adb logcat '*:D' > "$out" & |
| 100 | + fi |
| 101 | + echo $! |
| 102 | +} |
| 103 | + |
| 104 | +reset_if_needed() { |
| 105 | + if [[ -n "$RESET_FLOW" ]]; then |
| 106 | + echo "Resetting with: $RESET_FLOW" >&2 |
| 107 | + agent-device replay "$RESET_FLOW" >&2 |
| 108 | + return |
| 109 | + fi |
| 110 | + |
| 111 | + # No @reset declared: relaunch the app so the next replay starts from the flow's @pre state |
| 112 | + # instead of the previous run's @post state (Codex review r3191676565). |
| 113 | + agent-device open "$APP_ID" --platform "$PLATFORM" --relaunch >&2 |
| 114 | + sleep 5 |
| 115 | + if [[ "$PLATFORM" == "android" ]]; then |
| 116 | + wait_until_android_ui_ready |
| 117 | + fi |
| 118 | +} |
| 119 | + |
| 120 | +# After --relaunch on Android, UIAutomator often returns Snapshot: 0 nodes briefly; warmup replay then fails @pre. |
| 121 | +wait_until_android_ui_ready() { |
| 122 | + local snapshot |
| 123 | + for _ in $(seq 1 60); do |
| 124 | + snapshot="$(agent-device snapshot 2>/dev/null || true)" |
| 125 | + if [[ "$snapshot" == *'"Home"'* && "$snapshot" == *'"Inbox'* ]]; then |
| 126 | + return |
| 127 | + fi |
| 128 | + sleep 1 |
| 129 | + done |
| 130 | + |
| 131 | + echo "Warning: Android UI not ready after 60s (no Home/Inbox tabs); proceeding anyway." >&2 |
| 132 | +} |
| 133 | + |
| 134 | +measure_current_branch() { |
| 135 | + local raw="$TMP_DIR/capture.log" |
| 136 | + |
| 137 | + if [[ "$BOOT" == "true" ]]; then |
| 138 | + echo "Booting $PLATFORM target (agent-device boot)..." >&2 |
| 139 | + agent-device boot --platform "$PLATFORM" >&2 |
| 140 | + fi |
| 141 | + |
| 142 | + agent-device open "$APP_ID" --platform "$PLATFORM" --relaunch >&2 |
| 143 | + sleep 5 |
| 144 | + if [[ "$PLATFORM" == "android" ]]; then |
| 145 | + wait_until_android_ui_ready |
| 146 | + fi |
| 147 | + |
| 148 | + LOG_PID=$(start_log "$raw") |
| 149 | + agent-device replay "$FLOW" ${FLOW_ENV_ARGS[@]+"${FLOW_ENV_ARGS[@]}"} >&2 # warmup |
| 150 | + reset_if_needed |
| 151 | + sleep 1 |
| 152 | + |
| 153 | + for i in $(seq 1 "$RUNS"); do |
| 154 | + echo "Run $i/$RUNS" >&2 |
| 155 | + agent-device replay "$FLOW" ${FLOW_ENV_ARGS[@]+"${FLOW_ENV_ARGS[@]}"} >&2 |
| 156 | + reset_if_needed |
| 157 | + sleep 1 |
| 158 | + done |
| 159 | + |
| 160 | + kill "$LOG_PID" 2>/dev/null || true |
| 161 | + LOG_PID="" |
| 162 | + |
| 163 | + # Last N numeric durations: assumes one "[Sentry][<span>] Ending span (Nms)" line per measured replay (see SKILL.md Contract). |
| 164 | + grep "\\[Sentry\\]\\[$SPAN\\] Ending span" "$raw" | grep -oE "Ending span \(([0-9]+)ms\)" | grep -oE '[0-9]+' | tail -n "$RUNS" || true |
| 165 | +} |
| 166 | + |
| 167 | +echo "Using flow: $FLOW" >&2 |
| 168 | +if [[ -n "$RESET_FLOW" ]]; then |
| 169 | + echo "Using reset flow: $RESET_FLOW" >&2 |
| 170 | +fi |
| 171 | +append_flow_env_from_ad_vars |
| 172 | +measure_current_branch > "$DURATIONS_FILE" |
| 173 | + |
| 174 | +awk ' |
| 175 | + { b[++bn]=$1; bs+=$1; if(!bmin||$1<bmin)bmin=$1; if($1>bmax)bmax=$1 } |
| 176 | + END { |
| 177 | + if (!bn) { print "No captured runs."; exit 1 } |
| 178 | + ba=bs/bn |
| 179 | + printf "| Metric | value |\n" |
| 180 | + printf "|--------|-------|\n" |
| 181 | + printf "| Runs | %5d |\n", bn |
| 182 | + printf "| Avg | %4.0fms |\n", ba |
| 183 | + printf "| Min | %4dms |\n", bmin |
| 184 | + printf "| Max | %4dms |\n", bmax |
| 185 | + printf "\nSamples: " |
| 186 | + for (i=1; i<=bn; i++) printf "%sms%s", b[i], (i<bn ? ", " : "\n") |
| 187 | + }' "$DURATIONS_FILE" |
0 commit comments