-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_timing_breakdown.sh
More file actions
executable file
·147 lines (119 loc) · 4.16 KB
/
http_timing_breakdown.sh
File metadata and controls
executable file
·147 lines (119 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env bash
set -euo pipefail
# http_timing_breakdown.sh
# Measures HTTP timing phases (DNS/connect/TLS/TTFB/total) over repeated requests.
usage() {
cat <<'EOF'
Usage: ./http_timing_breakdown.sh --url URL [options]
Options:
--url URL Target URL (required)
--attempts N Number of requests (default: 5)
--timeout SEC Per-request timeout (default: 10)
--insecure Allow insecure TLS certs (curl -k)
--warn-total MS Warn threshold for total request time in ms (default: 1500)
--warn-ttfb MS Warn threshold for time-to-first-byte in ms (default: 800)
--strict Exit non-zero if thresholds are exceeded
-h, --help Show help
Exit codes:
0 = no threshold warnings (or warnings in non-strict mode)
2 = threshold warning in strict mode
EOF
}
URL=""
ATTEMPTS=5
TIMEOUT=10
INSECURE=false
WARN_TOTAL_MS=1500
WARN_TTFB_MS=800
STRICT=false
while [[ $# -gt 0 ]]; do
case "$1" in
--url) URL="$2"; shift 2 ;;
--attempts) ATTEMPTS="$2"; shift 2 ;;
--timeout) TIMEOUT="$2"; shift 2 ;;
--insecure) INSECURE=true; shift ;;
--warn-total) WARN_TOTAL_MS="$2"; shift 2 ;;
--warn-ttfb) WARN_TTFB_MS="$2"; shift 2 ;;
--strict) STRICT=true; shift ;;
-h|--help) usage; exit 0 ;;
*) echo "Unknown option: $1"; usage; exit 1 ;;
esac
done
if [[ -z "$URL" ]]; then
echo "--url is required"
usage
exit 1
fi
if ! command -v curl >/dev/null 2>&1; then
echo "curl command not found"
exit 1
fi
echo "HTTP timing breakdown timestamp: $(date -Iseconds)"
echo "url=$URL attempts=$ATTEMPTS timeout=${TIMEOUT}s strict_mode=$STRICT"
echo "warn_total_ms=$WARN_TOTAL_MS warn_ttfb_ms=$WARN_TTFB_MS"
echo
curl_insecure_flag=()
if [[ "$INSECURE" == true ]]; then
curl_insecure_flag=("-k")
fi
echo "attempt,status,dns_ms,connect_ms,tls_ms,ttfb_ms,total_ms"
warn_count=0
success_count=0
fail_count=0
total_ms_values=()
ttfb_ms_values=()
for ((i=1; i<=ATTEMPTS; i++)); do
line=$(curl -sS -o /dev/null \
--max-time "$TIMEOUT" \
"${curl_insecure_flag[@]}" \
-w "%{http_code},%{time_namelookup},%{time_connect},%{time_appconnect},%{time_starttransfer},%{time_total}" \
"$URL" 2>/dev/null || true)
if [[ -z "$line" ]]; then
echo "$i,000,NA,NA,NA,NA,NA"
((fail_count+=1))
continue
fi
IFS=',' read -r status dns_s connect_s tls_s ttfb_s total_s <<< "$line"
dns_ms=$(awk -v n="$dns_s" 'BEGIN {printf "%.0f", n*1000}')
connect_ms=$(awk -v n="$connect_s" 'BEGIN {printf "%.0f", n*1000}')
tls_ms=$(awk -v n="$tls_s" 'BEGIN {printf "%.0f", n*1000}')
ttfb_ms=$(awk -v n="$ttfb_s" 'BEGIN {printf "%.0f", n*1000}')
total_ms=$(awk -v n="$total_s" 'BEGIN {printf "%.0f", n*1000}')
echo "$i,$status,$dns_ms,$connect_ms,$tls_ms,$ttfb_ms,$total_ms"
if [[ "$status" == "000" ]]; then
((fail_count+=1))
continue
fi
((success_count+=1))
total_ms_values+=("$total_ms")
ttfb_ms_values+=("$ttfb_ms")
if [[ "$total_ms" -gt "$WARN_TOTAL_MS" || "$ttfb_ms" -gt "$WARN_TTFB_MS" ]]; then
((warn_count+=1))
fi
done
echo
if [[ "$success_count" -gt 0 ]]; then
total_stats=$(printf '%s\n' "${total_ms_values[@]}" | awk '
NR==1{min=$1;max=$1;sum=$1;next}
{if($1<min)min=$1; if($1>max)max=$1; sum+=$1}
END{avg=sum/NR; printf "%d %.2f %d", min, avg, max}
')
ttfb_stats=$(printf '%s\n' "${ttfb_ms_values[@]}" | awk '
NR==1{min=$1;max=$1;sum=$1;next}
{if($1<min)min=$1; if($1>max)max=$1; sum+=$1}
END{avg=sum/NR; printf "%d %.2f %d", min, avg, max}
')
total_min=$(printf '%s' "$total_stats" | awk '{print $1}')
total_avg=$(printf '%s' "$total_stats" | awk '{print $2}')
total_max=$(printf '%s' "$total_stats" | awk '{print $3}')
ttfb_min=$(printf '%s' "$ttfb_stats" | awk '{print $1}')
ttfb_avg=$(printf '%s' "$ttfb_stats" | awk '{print $2}')
ttfb_max=$(printf '%s' "$ttfb_stats" | awk '{print $3}')
echo "Summary: success=$success_count fail=$fail_count warn=$warn_count total_ms(min/avg/max)=${total_min}/${total_avg}/${total_max} ttfb_ms(min/avg/max)=${ttfb_min}/${ttfb_avg}/${ttfb_max}"
else
echo "Summary: success=0 fail=$fail_count warn=$warn_count"
fi
if [[ "$STRICT" == true && "$warn_count" -gt 0 ]]; then
exit 2
fi
exit 0