From 31d8d7dfa70e8da62a006ec94b47160e9cadacb8 Mon Sep 17 00:00:00 2001 From: Roman Ovchinnikov Date: Mon, 5 Jul 2010 13:23:09 +0400 Subject: [PATCH] compression merged, command line configuration extended Signed-off-by: Roman Ovchinnikov --- check_response_time.sh | 167 ++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 135 insertions(+), 32 deletions(-) diff --git a/check_response_time.sh b/check_response_time.sh index 576a754..003421e 100755 --- a/check_response_time.sh +++ b/check_response_time.sh @@ -1,49 +1,152 @@ #!/bin/bash -# (c) 2008, Roman Ovchinnikov +# (c) 2008-2010, Roman Ovchinnikov aka CoolCold # coolthecold@gmail.com + +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" -TIME_INTERVAL=1 -ARGS=2 -if [[ $# -ne "$ARGS" ]] -then - echo "Usage: `basename $0` [cookie file]" - echo "for example, static file: `basename $0` http://fe.banners.mail.ru/js/show.js cookie.txt" - echo "or dynamic file: `basename $0` http://fe.banners.mail.ru/js/1000.js cookie.txt" - echo "set cookie file to NO to disable cookieng" + +print_usage() { + echo "Usage: $PROGNAME -u [-C ] [-c] [-i ] [-I - something like http://fe.banners.mail.ru/js/show.js or http://assets0.github.com/stylesheets/bundle_common.css?2f7e714f544c24700e52868163f325fbc5239a18" + echo "-C - file which contains cookies server may require, in curl format" + echo "-c - enable compression support, so server may return gzipped content, for example" + echo "-i - interval to do requests, in seconds" + echo "-I - 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" - exit 1 - -fi +} -URL=$1 -for i in `seq 1 172800` -do -curdate=`$DATE_CMD` -if [[ "x$2" = "xNO" ]] +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 reponces on queries" +} + +if [[ $# -eq "0" ]]; then - CURL_DATA=`curl -o /dev/null -w '%{time_total} %{time_connect}\n' $URL 2>/dev/null` + #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 + +# Parse Arguments + +while getopts ":hcDSu:i:I:C:" Option; do + case $Option in + h) + print_help + exit 0 + ;; + u) + URL=${OPTARG} + ;; + c) + USE_COMPRESSION=YES + ;; + i) + INTERVAL=${OPTARG} + ;; + I) + ITERATIONS=${OPTARG} + ;; + C) + COOKIE_FILE="${OPTARG}" + ;; + D) + DUMP_HEADERS="YES" + ;; + S) + SHOW_COMMAND="YES" + ;; + *) + 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 [[ "$USE_COMPRESSION" == "YES" ]];then + COMPRESS_ARG="--compressed" else - CURL_DATA=`curl -b cookie.txt -o /dev/null -w '%{time_total} %{time_connect}\n' $URL 2>/dev/null` + COMPRESS_ARG="" fi -if [[ "x$CURL_DATA" = "x" ]] -then - #empty curl data, do we have curl installed? - echo "no data from curl, is it installed? try \"sudo apt-get install curl\" to fix this";exit 1 + +COOKIE_ARG="" +if [[ "x$COOKIE_FILE" != "xNO" && "x$COOKIE_FILE" != "x" ]];then + COOKIE_ARG="-b \"$COOKIE_FILE\"" + #COOKIE_PARAMS="$COOKIE_FILE" fi -#echo "curl data" -#echo "$CURL_DATA" -RESP_DATA=`echo "$CURL_DATA"|tail -n 1` +DUMP_ARG="" +if [[ "x$DUMP_HEADERS" == "xYES" ]];then + DUMP_ARG="-D -" +fi -#echo "resp data" -#echo "$RESP_DATA" -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" -sleep $TIME_INTERVAL +#requesting url in cycle +for i in $(seq 1 $ITERATIONS);do + curdate=$($DATE_CMD) + CURL_ARGS="-o /dev/null $COOKIE_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 + 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 + sleep $INTERVAL done + -- 1.5.6.5