-
Notifications
You must be signed in to change notification settings - Fork 1
/
ttfb.sh
executable file
·189 lines (153 loc) · 5.82 KB
/
ttfb.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
set -eu
function help() {
tput setaf 2
echo "Usage ./ttfb.sh -f <file> [-a <agent>] [-q <query>] [-l <limit>] [-ir] | -h"
tput setaf 3
echo "Options:"
echo -e "-f \t Path to a file with URLs."
echo -e "-a \t Overwrites the default user-agent."
echo -e "-l \t Limit the number of URLs to read from a file."
echo -e "-q \t Query string to be added to each URL."
echo -e "-i \t [Flag] Attempt to invalidate cache by adding a timestamp to the URLs."
echo -e "-r \t [Flag] Reads URLs from a file in random order."
echo -e "-h \t [Flag] Help."
exit 0
}
if [[ ! $* =~ ^\-.+ ]]; then
help
fi
while getopts "f:l:q:a::rih" opt; do
case "$opt" in
f) file=${OPTARG} ;;
a) user_agent="${OPTARG}" ;;
l) limit=${OPTARG} ;;
q) query=${OPTARG} ;;
i) invalidate_cache=1 ;;
r) random=1 ;;
h) help ;;
*) help ;;
esac
done
if [[ ! -f $file ]]; then
echo "File '$file' not found"
exit 1
fi
if [[ -z ${user_agent+set} ]]; then
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36"
fi
if [[ -z ${limit+set} ]]; then
limit=$(wc -l < "$file")
else
lines_in_file=$(wc -l < "$file")
if [[ $limit -gt $lines_in_file ]]; then
printf "The limit is set to its max %d instead of %d \n" "$lines_in_file" "$limit"
limit=$lines_in_file
fi
fi
if [[ -z ${query+set} ]]; then
query=''
fi
if [[ -z ${invalidate_cache+set} ]]; then
invalidate_cache=0
fi
if [[ -z ${random+set} ]]; then
random=0
fi
function send_request() {
curl -H "user-agent: $2" \
--silent \
-o /dev/null \
-w "%{time_starttransfer} %{http_code} %{time_pretransfer} %{time_connect} %{time_namelookup}\n" \
"$1"
}
function prepare_url() {
url=$1
appendix=()
if [[ $2 -gt 0 ]]; then
appendix+=("timestamp=$(date +%s)")
fi
if [[ -n "$3" ]]; then
appendix+=("$3")
fi
if [[ ${#appendix[@]} -gt 0 ]]; then
connector=$([[ "$url" == *"?"* ]] && echo '&' || echo '?')
for p in "${appendix[@]}"
do
url="${url}${connector}${p}"
connector='&'
done
fi
echo "$url"
}
time_total=0
server_time_total=0
latency_time_total=0
visited_counter=0
non_200_counter=0
printf "\n"
function evaluate_url() {
[ $visited_counter -eq 0 ] &&
printf '"%s" "%s" "%s" "%s" "%s" "%s"\n\n' "Counter" "HTTP Code" "URL" "TTFB (ms)" "Server Time minus Latency (ms)" "Latency (ms)"
((visited_counter+=1))
url="$(prepare_url "$1" $invalidate_cache "$query")"
read -r time_starttransfer http_code time_pretransfer time_connect time_namelookup <<<"$(send_request "$url" "$user_agent")"
if [[ $http_code != '200' ]]; then
non_200_counter=$((non_200_counter + 1))
echo "$visited_counter" "$http_code" "$url" -
else
server_time=$(awk "BEGIN {print $time_starttransfer-$time_pretransfer; exit}")
server_time_total=$(awk "BEGIN {print $server_time_total+$server_time; exit}")
latency_time=$(awk "BEGIN {print $time_connect-$time_namelookup; exit}")
latency_time_total=$(awk "BEGIN {print $latency_time_total+$latency_time; exit}")
time_total=$(awk "BEGIN {print $time_total+$time_starttransfer; exit}")
server_time_average=$(awk "BEGIN {print $server_time_total/($visited_counter-$non_200_counter); exit}")
latency_time_ms="$(awk "BEGIN {print $latency_time * 1000; exit}")"
time_starttransfer_ms="$(awk "BEGIN {print $time_starttransfer * 1000; exit}")"
server_time_no_latency_ms=$(awk "BEGIN {print ($server_time-$latency_time) * 1000; exit}")
printf "%d %d %s %.2f %.2f %.2f\n" "$visited_counter" "$http_code" "$url" "$time_starttransfer_ms" "$server_time_no_latency_ms" "$latency_time_ms"
fi
}
if [[ $random -eq 1 ]]; then
random_rows=()
while IFS= read -r row ; do random_rows+=("$row"); done <<< "$(
awk -v loop="$limit" -v range="$(wc -l < "$file")" 'BEGIN {
srand()
do {
numb = 1 + int(rand() * range)
if (!(numb in prev)) {
print numb
prev[numb] = 1
count++
}
} while (count<loop)
}'
)"
for row in "${random_rows[@]}"
do
in=$(awk "NR==$row{ print; exit }" "$file")
evaluate_url "$in"
done
else
while read -r in || [ -n "$in" ]; do
[ -z "$in" ] && continue
evaluate_url "$in"
if [[ $limit -gt 0 && $visited_counter -ge $limit ]]; then
break
fi
done <"$file"
fi
printf "\n"
pages_evaluated=$(awk "BEGIN {print $visited_counter-$non_200_counter; exit}")
printf "Pages visited / evaluated / skipped: %d / %d / %d\n\n" "$visited_counter" "$pages_evaluated" "$non_200_counter"
printf "Total time elapsed: %.2f s\n" "$time_total"
if [[ $pages_evaluated -gt 0 ]]; then
latency_time_average=$(awk "BEGIN {print $latency_time_total/$pages_evaluated; exit}")
server_time_average=$(awk "BEGIN {print $server_time_total/$pages_evaluated; exit}")
printf "Avg TTFB: %.2f ms\n" "$(awk "BEGIN {print ($time_total/$pages_evaluated) * 1000; exit}")"
printf "Avg server time with latency: %.2f ms\n" "$(awk "BEGIN {print $server_time_average * 1000; exit}")"
printf "Avg network latency: %.2f ms\n" "$(awk "BEGIN {print $latency_time_average * 1000; exit}")"
printf "Avg server time minus latency: %.2f ms\n" "$(awk "BEGIN {print ($server_time_average - $latency_time_average) * 1000; exit}")"
printf "Avg server time minus latency*2: %.2f ms\n" "$(awk "BEGIN {print ($server_time_average - $latency_time_average*2) * 1000; exit}")"
fi
printf "\n"