|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# perf-observer.sh — polls /tree and /allSteps for a running build and prints a |
| 4 | +# latency summary at the end. Use it to compare two configurations (e.g. before |
| 5 | +# vs. after a change) by running it once per build and diffing the two summaries. |
| 6 | +# |
| 7 | +# Usage: |
| 8 | +# ./perf-observer.sh JENKINS_URL JOB_NAME BUILD_NUMBER [LABEL] |
| 9 | +# |
| 10 | +# Optional env vars: |
| 11 | +# JENKINS_AUTH=user:token pass-through to curl --user |
| 12 | +# POLL_INTERVAL=3 seconds between poll rounds (default 3) |
| 13 | +# |
| 14 | +# Example: |
| 15 | +# |
| 16 | +# ./perf-observer.sh http://localhost:8080/jenkins perf 1 baseline |
| 17 | +# ./perf-observer.sh http://localhost:8080/jenkins perf 2 after-change |
| 18 | +# |
| 19 | +# Raw samples are written to /tmp/pgv-perf-<LABEL>.csv for later analysis. |
| 20 | + |
| 21 | +set -euo pipefail |
| 22 | + |
| 23 | +if [[ $# -lt 3 ]]; then |
| 24 | + echo "Usage: $0 JENKINS_URL JOB_NAME BUILD_NUMBER [LABEL]" >&2 |
| 25 | + exit 2 |
| 26 | +fi |
| 27 | + |
| 28 | +JENKINS_URL="${1%/}" |
| 29 | +JOB_NAME="$2" |
| 30 | +BUILD_NUMBER="$3" |
| 31 | +LABEL="${4:-run}" |
| 32 | +POLL_INTERVAL="${POLL_INTERVAL:-3}" |
| 33 | +CSV="/tmp/pgv-perf-${LABEL}.csv" |
| 34 | + |
| 35 | +BASE="${JENKINS_URL}/job/${JOB_NAME}/${BUILD_NUMBER}/stages" |
| 36 | +API_URL="${JENKINS_URL}/job/${JOB_NAME}/${BUILD_NUMBER}/api/json?tree=result,inProgress" |
| 37 | + |
| 38 | +AUTH=() |
| 39 | +if [[ -n "${JENKINS_AUTH:-}" ]]; then |
| 40 | + AUTH=(--user "$JENKINS_AUTH") |
| 41 | +fi |
| 42 | + |
| 43 | +echo "Observing: $BASE" |
| 44 | +echo "Label: $LABEL" |
| 45 | +echo "CSV: $CSV" |
| 46 | +echo "Interval: ${POLL_INTERVAL}s" |
| 47 | +echo |
| 48 | + |
| 49 | +echo "sample_ts,endpoint,http_code,time_ms,bytes" > "$CSV" |
| 50 | + |
| 51 | +measure() { |
| 52 | + local endpoint="$1" |
| 53 | + local url="$BASE/$endpoint" |
| 54 | + local out |
| 55 | + out=$(curl -so /dev/null --compressed -w '%{http_code},%{time_total},%{size_download}' \ |
| 56 | + "${AUTH[@]}" "$url" 2>/dev/null || echo "000,0,0") |
| 57 | + local http_code="${out%%,*}" |
| 58 | + local rest="${out#*,}" |
| 59 | + local time_s="${rest%%,*}" |
| 60 | + local bytes="${rest#*,}" |
| 61 | + local time_ms |
| 62 | + time_ms=$(awk -v t="$time_s" 'BEGIN { printf "%.1f", t * 1000 }') |
| 63 | + local ts |
| 64 | + ts=$(date +%s) |
| 65 | + echo "$ts,$endpoint,$http_code,$time_ms,$bytes" >> "$CSV" |
| 66 | + printf ' %s %-10s %s %8sms %8s bytes\n' \ |
| 67 | + "$(date +%H:%M:%S)" "$endpoint" "$http_code" "$time_ms" "$bytes" |
| 68 | +} |
| 69 | + |
| 70 | +build_state() { |
| 71 | + curl -s "${AUTH[@]}" "$API_URL" 2>/dev/null || echo "" |
| 72 | +} |
| 73 | + |
| 74 | +is_running() { |
| 75 | + local resp |
| 76 | + resp=$(build_state) |
| 77 | + [[ "$resp" == *'"inProgress":true'* ]] |
| 78 | +} |
| 79 | + |
| 80 | +echo "Waiting for build to enter in-progress state..." |
| 81 | +for _ in $(seq 1 30); do |
| 82 | + if is_running; then break; fi |
| 83 | + sleep 2 |
| 84 | +done |
| 85 | + |
| 86 | +if ! is_running; then |
| 87 | + state=$(build_state) |
| 88 | + if [[ -z "$state" ]]; then |
| 89 | + echo "ERROR: could not reach $API_URL" >&2 |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | + echo "NOTE: build is not in-progress (state: $state). Taking a few post-completion samples only." |
| 93 | +fi |
| 94 | + |
| 95 | +echo |
| 96 | +echo "Polling while build runs..." |
| 97 | +echo |
| 98 | + |
| 99 | +while is_running; do |
| 100 | + measure tree |
| 101 | + measure allSteps |
| 102 | + sleep "$POLL_INTERVAL" |
| 103 | +done |
| 104 | + |
| 105 | +echo |
| 106 | +echo "Build complete. Taking three post-completion samples..." |
| 107 | +for _ in 1 2 3; do |
| 108 | + measure tree |
| 109 | + measure allSteps |
| 110 | + sleep 1 |
| 111 | +done |
| 112 | + |
| 113 | +echo |
| 114 | +echo "================ Summary: '$LABEL' ================" |
| 115 | +python3 - "$CSV" <<'PY' |
| 116 | +import csv, sys |
| 117 | +
|
| 118 | +csv_path = sys.argv[1] |
| 119 | +samples = {} |
| 120 | +warnings = {} |
| 121 | +
|
| 122 | +with open(csv_path) as f: |
| 123 | + for row in csv.DictReader(f): |
| 124 | + ep = row["endpoint"] |
| 125 | + code = row["http_code"] |
| 126 | + try: |
| 127 | + t = float(row["time_ms"]) |
| 128 | + except ValueError: |
| 129 | + continue |
| 130 | + if code != "200": |
| 131 | + warnings[(ep, code)] = warnings.get((ep, code), 0) + 1 |
| 132 | + continue |
| 133 | + samples.setdefault(ep, []).append(t) |
| 134 | +
|
| 135 | +def pct(sorted_vals, p): |
| 136 | + if not sorted_vals: |
| 137 | + return 0.0 |
| 138 | + idx = int(round((p / 100.0) * (len(sorted_vals) - 1))) |
| 139 | + idx = max(0, min(len(sorted_vals) - 1, idx)) |
| 140 | + return sorted_vals[idx] |
| 141 | +
|
| 142 | +for ep in sorted(samples): |
| 143 | + vals = sorted(samples[ep]) |
| 144 | + n = len(vals) |
| 145 | + print(f" {ep:10s} n={n:4d} " |
| 146 | + f"min={vals[0]:8.1f}ms " |
| 147 | + f"median={pct(vals, 50):8.1f}ms " |
| 148 | + f"p95={pct(vals, 95):8.1f}ms " |
| 149 | + f"max={vals[-1]:8.1f}ms") |
| 150 | +
|
| 151 | +if warnings: |
| 152 | + print() |
| 153 | + for (ep, code), count in sorted(warnings.items()): |
| 154 | + print(f" {ep:10s} WARN {count}x non-200 ({code})") |
| 155 | +PY |
| 156 | +echo "=====================================================" |
| 157 | +echo |
| 158 | +echo "Raw samples: $CSV" |
0 commit comments