-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimecurl.sh
executable file
·157 lines (141 loc) · 4.26 KB
/
timecurl.sh
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
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
#
# Measure HTTP request/response times using Curl.
# (c) 2022 Josef Hammer (josefhammer.com) -- MIT License
#
# Source: https://github.com/josefhammer/timecurl
# man curl:
#
# __time_namelookup__
# The time, in seconds, it took from the start until the name resolving was completed.
#
# __time_connect__
# The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.
#
# __time_appconnect__
# The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed. (Added in 7.19.0)
#
# __time_pretransfer__
# The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands
# and negotiations that are specific to the particular protocol(s) involved.
#
# __time_redirect__
# The time, in seconds, it took for all redirection steps including name lookup, connect, pretransfer and transfer before the final
# transaction was started. time_redirect shows the complete execution time for multiple redirections. (Added in 7.12.3)
#
# __time_starttransfer__
# The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes
# time_pretransfer and also the time the server needed to calculate the result.
#
# __time_total__
# The total time, in seconds, that the full operation lasted.
#
# __num_connects__
# Number of new connects made in the recent transfer.
#
# __size_download__
# The total amount of bytes that were downloaded.
#
# __size_request__
# The total amount of bytes that were sent in the HTTP request.
NUM_REQUESTS=1
SLEEP=0
STDIN=0
ID=
URL_PATH=
# Parse command line params
#
while [[ $1 == "--loop" ]] || [[ $1 == "--sleep" ]] || [[ $1 == "--id" ]] || [[ $1 == "--stdin" ]] || [[ $1 == "--path" ]]; do
if [[ $1 == "--loop" ]]; then
shift
if [[ $1 -gt 1 ]]; then
NUM_REQUESTS=$1
fi
shift
fi
if [[ $1 == "--sleep" ]]; then
shift
SLEEP=$1
shift
fi
if [[ $1 == "--id" ]]; then
shift
ORIG_ID=$1
ID=$1
shift
fi
if [[ $1 == "--stdin" ]]; then # Shall we read the URL(s) from STDIN?
shift
STDIN=1
fi
if [[ $1 == "--path" ]]; then # fixed path to be used
shift
URL_PATH=$1
shift
fi
done
# Help message for the options
#
if [[ $# -lt 1 ]] && [[ $STDIN -eq 0 ]]
then
echo "Usage: $0 [--loop <numRequests>] [--sleep <numSeconds>] [--id <ID>] [--stdin] [--path <PATH>] [<Curl options>] [<URL>]"
echo "Pass '-L' to Curl to follow redirections."
echo ""
exit 1
fi
# Main loop
#
mainCurlLoop() {
for ((i=1; i<=$NUM_REQUESTS; i++))
do
if [[ $i -gt 1 ]]; then
echo "},{"
if [[ $SLEEP -gt 0 ]]; then
sleep "$SLEEP"
fi
fi
curl -s -o /dev/null -w @- "$@" <<EOF
"remote": "%{remote_ip}:%{remote_port}${URL_PATH}",\n
"http_code": %{http_code},\n
"num_connects": %{num_connects},\n
"id": "${ID}",\n
"local": "%{local_ip}:%{local_port}",\n
"size_request": %{size_request},\n
"size_upload": %{size_upload},\n
"size_download": %{size_download},\n
"time_namelookup": %{time_namelookup},\n
"time_pretransfer": %{time_pretransfer},\n
"time_starttransfer": %{time_starttransfer},\n
\n
"time_connect": %{time_connect},\n
"time_total": %{time_total},\n
EOF
echo " \"exit_code\": $?"
done
}
# *** MAIN ***
#
echo "[{"
if [[ $STDIN -eq 0 ]]; then
mainCurlLoop "$@"$URL_PATH
else
# read URLs from stdin (one per line; format: [<ID>] URL)
#
COUNTER=0
while read -a ADDR; do
>&2 echo "# ${ADDR[@]}" # print input on stderr for monitoring
[[ $ADDR =~ ^#.* ]] && continue # ignore comment lines starting with '#'
COUNTER=$((COUNTER+1))
if [[ $COUNTER -gt 1 ]]; then
echo "},{"
fi
if [[ ${#ADDR[@]} -gt 1 ]]; then # if two elements available: use first as ID
ID="${ADDR[0]}"
ADDR[0]=${ADDR[1]}
else
ID=$ORIG_ID
fi
mainCurlLoop "$@" "${ADDR[0]}$URL_PATH"
done
fi
echo "}]"