forked from sashka/tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_response_time
executable file
·173 lines (148 loc) · 4.62 KB
/
check_response_time
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
#!/bin/bash
# (c) 2008-2010, Roman Ovchinnikov aka CoolCold
PROGNAME=$(basename $0)
#setting defaults
DEFAULT_INTERVAL=1
DEFAULT_ITERATIONS=$(( 60 * 60 * 2)) # 2 hours
DEFAULT_COMPRESSION=NO
DEFAULT_COOKIE=NO
DEFAULT_DUMP=NO
DATE_CMD="date +%s"
print_usage() {
echo "Usage: $PROGNAME <url> [-C <cookie file>] [-c] [-H <value>] [-i <interval>] [-I <iterations] [-D] [-S]"
echo ""
echo "<url> - something like http://fe.banners.mail.ru/js/show.js or http://assets0.github.com/stylesheets/bundle_common.css?2f7e714f544c24700e52868163f325fbc5239a18"
echo "-C <cookie file> - file which contains cookies server may require, in curl format"
echo "-c - enable compression support, so server may return gzipped content, for example"
echo "-H <value> - send header \"Host: <value>\" - may be useful to ask server about certain vhost
for example in case when you have several servers like srv1.host.ru, srv2.host.ru...which
servers the same domain www.host.ru, you can specify url as 'http://srv2.host.ru/test.js'
and set header to 'www.host.ru'"
echo "-i <interval> - interval to do requests, in seconds"
echo "-I <iterations> - how much requests should be taken"
echo "-D - enable dump of headers for each request to stderr"
echo "-S - show command being executed on stderr (command should be exact, but if in doubt, use bash -x /path/to/script)"
echo "---"
echo "Hint: use smth like 'cat show_flash.nginx_banners.txt|cut -f 2 -d \" \" |cut -f 2 -d \":\"|sort -n|uniq -c' to view largest times sets"
}
print_help() {
echo "$PROGNAME"
echo ""
print_usage
echo ""
echo "This script tryes to connect to url and writeout connection/total times operation took."
echo "It may be useful while testing how fast your webserver reponses on queries"
}
if [[ $# -eq "0" ]]; then
#no arguments were specified...very, very bad
print_help
exit 0
fi
#applying defaults
INTERVAL=$DEFAULT_INTERVAL
ITERATIONS=$DEFAULT_ITERATIONS
USE_COMPRESSION=$DEFAULT_COMPRESSION
COOKIE_FILE=$DEFAULT_COOKIE
URL=""
DUMP_HEADERS=$DEFAULT_DUMP
SHOW_COMMAND=NO
HOST_HEADER=NO
#prechecking url cuz params need to be shifter before using getopts
#looks like getopts doesn't like mixed param specification style
URL="$1"
#deleting $1 with url from params list to make getopts happy
shift
# Parse Arguments
while getopts ":hcDSi:I:C:H:" Option; do
case $Option in
h)
print_help
exit 0
;;
c)
USE_COMPRESSION=YES
;;
i)
INTERVAL=${OPTARG}
;;
I)
ITERATIONS=${OPTARG}
;;
C)
COOKIE_FILE="${OPTARG}"
;;
D)
DUMP_HEADERS="YES"
;;
S)
SHOW_COMMAND="YES"
;;
H)
HOST_HEADER="${OPTARG}"
;;
*)
print_help
exit 0
;;
esac
done
shift $(($OPTIND - 1))
#validating && translating
if [[ "x$URL" == "x" ]]; then
echo "URL must be specified!"
print_usage
exit 1
fi
if [[ "x$USE_COMPRESSION" == "xYES" ]]; then
COMPRESS_ARG="--compressed"
else
COMPRESS_ARG=""
fi
HOST_HEADER_ARG=""
if [[ "x$HOST_HEADER" != "xNO" ]]; then
HOST_HEADER_ARG="-H \"Host: $HOST_HEADER\""
fi
COOKIE_ARG=""
if [[ "x$COOKIE_FILE" != "xNO" && "x$COOKIE_FILE" != "x" ]]; then
COOKIE_ARG="-b \"$COOKIE_FILE\""
#COOKIE_PARAMS="$COOKIE_FILE"
fi
DUMP_ARG=""
if [[ "x$DUMP_HEADERS" == "xYES" ]]; then
DUMP_ARG="-D -"
fi
#requesting url in cycle
for i in $(seq 1 $ITERATIONS); do
curdate=$($DATE_CMD)
CURL_ARGS="-o /dev/null $COOKIE_ARG $HOST_HEADER_ARG $COMPRESS_ARG $DUMP_ARG -w '%{time_total} %{time_connect}\n' $URL"
if [[ "x$SHOW_COMMAND" == "xYES" ]]; then
echo "command is: curl $CURL_ARGS 2>/dev/null" 1>&2
fi
#CURL_DATA=$(eval curl "-o /dev/null $COOKIE_ARG $COMPRESS_ARG $DUMP_ARG -w '%{time_total} %{time_connect}\n' $URL" 2>/dev/null)
CURL_DATA=$(eval curl "$CURL_ARGS" 2>/dev/null)
EXT_CODE=$?
if [[ $EXT_CODE -ne 0 ]]; then
if [[ $EXT_CODE -eq 127 ]]; then #command not found
echo "is curl installed? try \"sudo apt-get install curl\" to fix this";exit 1
else
echo "curl exited with error:$EXT_CODE"
exit $EXT_CODE
fi
fi
if [[ "x$CURL_DATA" = "x" ]]; then
#empty curl data, way strange
echo "" >/dev/null #just placeholder for now
else
RESP_DATA=`echo "$CURL_DATA"|tail -n 1`
if [[ "x$DUMP_ARG" != "x" ]]; then
#let's cut and show headers to stderr
HEADERS=$(echo "$CURL_DATA"|head -n "-1")
echo "$HEADERS" 1>&2
fi
TOTAL_TIME=`echo $RESP_DATA|awk '{print $1}'`
CONNECT_TIME=`echo $RESP_DATA|awk '{print $2}'`
echo "time:$curdate ttime:$TOTAL_TIME conntime:$CONNECT_TIME"
fi
if [ $i -ne $ITERATIONS ];then sleep $INTERVAL;fi
done