Detail: bench-tool source v2 (bash)

  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
#!/bin/bash

################################################################################
# bench.sh - Hace mediciones de rendimiento de cualquier URL que se le pase
# v0.1 r0sk 07.11.2011
#
# FIXME: Como se pasa el html a minúsculas para parsear las jpg|gif|png, al 
#        hacer las peticiones de esos archivos para saber cuanto pesan falla
#        porque no están en mayúsculas.
# FIXME: No se muestran las imágenes en CSS, solamente se muestran las html
# FIXME: Comprobar el funcionamiento con subdominios, creo que el "sed" que 
#        calcula el domainmane falla con subdominios y a la hora de agregar esa
#        misma variable a CSS/JS internos lo mismo.
# FIXME: El cuenteo de JS/CSS/IMG no se corresponde con el número de elementos
#        listados al final.
################################################################################

# ------------------------------------------------------------------------------
# Required software
# ------------------------------------------------------------------------------
AB=`which ab`
CURL=`which curl`
WGET=`which wget`

# ------------------------------------------------------------------------------
# go()
# ------------------------------------------------------------------------------
go()
{
	#bench1;
	bench2;
	bench3;
	bench4;
}

# ------------------------------------------------------------------------------
# Bench #1: Apache Benchmark Tool (1000 requests, 30 clients).
# ------------------------------------------------------------------------------
bench1()
{
    echo -e "=== Apache Benchmarking -n100 -c10 ==="
    ${AB} -n100 -c10 -q -k -H 'Accept-Encoding: gzip,deflate' $URL | grep 'per second'  | awk '{print "Peticiones por segundo: " $4  }'
    echo -e ""
}

# ------------------------------------------------------------------------------
# Bench #2: Curl, time to connect, transfer and total time of the request.
# ------------------------------------------------------------------------------
bench2()
{
    echo -e "=== Tiempos de conexión ==="
    ${CURL} -o /dev/null -s -w " Connect: "%{time_connect}"\n Transfer: "%{time_starttransfer}"\n Total: "%{time_total}"\n" $URL
    echo -e ""
}

# ------------------------------------------------------------------------------
# Bench #3: Wget plain and gzipped pettition.
# ------------------------------------------------------------------------------
bench3()
{
    echo -e "=== Plano y gzip ==="
    ${WGET} -q -O plano $URL
    ${WGET} -q -O gzip --header="Accept-Encoding: gzip" $URL
    du -h plano gzip
    rm plano gzip
    echo -e ""
}

# ------------------------------------------------------------------------------
# Bench #4: Nº of javascript and css files
# ------------------------------------------------------------------------------
bench4()
{
    echo -e "=== Javascript/CSS info ==="
    ${WGET} -q -O plano $URL

    cat plano | awk '{print tolower($0)}' | sed s/src/\\nsrc/g | sed s/href/\\nhref/g | sed s/\'/\"/g > plano2

    JS=`cat plano2 | grep -P "src=[^*]*\.js" | awk -F 'src="' '{ print $2 }' | awk -F '"' '{ print $1 }' | sort | uniq`
    CSS=`cat plano2 | grep -P "href=[^*]*\.css" | awk -F 'href="' '{ print $2 }' | awk -F '"' '{ print $1 }' | sort | uniq`
    IMG=`cat plano2 | grep -P "src=[^*]*\.(jpg|gif|png)" | awk -F 'src="' '{ print $2 }' | awk -F '"' '{ print $1 }' | sort | uniq`

    NUMJS=`cat plano2 | grep -P "src=[^*]*\.js" | sort | uniq | wc -l`
    NUMCSS=`cat plano2 | grep -P "href=[^*]*\.css" | sort | uniq | wc -l`
    NUMIMG=`cat plano2 | grep -P "src=[^*]*\.(jpg|gif|png)" | sort | uniq | wc -l`
    
    SIZETOT=0
    echo "JS: ${NUMJS} file(s)"
    for i in ${JS}; do
        if echo ${i} | grep -q "${DOMAIN}"; then
            TAMANO=`${CURL} -sI $i | grep Content-Length | cut -d ' ' -f 2`
            TIEMPO=`${CURL} -o /dev/null -s -w %{time_total} $i`
            echo " (intA)" $i ${TIEMPO} ${TAMANO};
        else
            if [[ $i == http* ]]; then
                TAMANO=`${CURL} -sI $i | grep Content-Length | cut -d ' ' -f 2`
                TIEMPO=`${CURL} -o /dev/null -s -w %{time_total} $i`
                echo " (ext)" $i ${TIEMPO} ${TAMANO};
            else
                TAMANO=`${CURL} -sI http://${DOMAIN}/$i | grep Content-Length | cut -d ' ' -f 2`
                TIEMPO=`${CURL} -o /dev/null -s -w %{time_total} http://${DOMAIN}/$i`
                echo " (intB) http://${DOMAIN}/$i" ${TIEMPO} ${TAMANO}
            fi
        fi
    done

    echo -e ""

    SIZETOT=0
    echo "CSS: ${NUMCSS} file(s)"
    for i in ${CSS}; do
        if echo ${i} | grep -q "${DOMAIN}"; then
            TAMANO=`${CURL} -sI $i | grep Content-Length | cut -d ' ' -f 2`
            TIEMPO=`${CURL} -o /dev/null -s -w %{time_total} $i`
            echo " (intA)" $i ${TIEMPO} ${TAMANO};
        else
            if [[ $i == http* ]]; then
                TAMANO=`${CURL} -sI $i | grep Content-Length | cut -d ' ' -f 2`
                TIEMPO=`${CURL} -o /dev/null -s -w %{time_total} $i`
                echo " (ext)" $i ${TIEMPO} ${TAMANO};
            else
                TAMANO=`${CURL} -sI http://${DOMAIN}/$i | grep Content-Length | cut -d ' ' -f 2`
                TIEMPO=`${CURL} -o /dev/null -s -w %{time_total} http://${DOMAIN}/$i`
                echo " (intB) http://${DOMAIN}/$i" ${TIEMPO} ${TAMANO}
            fi
        fi
    done

    echo -e ""

    SIZETOT=0
    echo "IMG: ${NUMIMG} file(s)"
    for i in ${IMG}; do
        if echo ${i} | grep -q "${DOMAIN}"; then
            TAMANO=`${CURL} -sI $i | grep Content-Length | cut -d ' ' -f 2`
            TIEMPO=`${CURL} -o /dev/null -s -w %{time_total} $i`
            echo " (intA)" $i ${TIEMPO} ${TAMANO};
        else
            if [[ $i == http* ]]; then
                TAMANO=`${CURL} -sI $i | grep Content-Length | cut -d ' ' -f 2`
                TIEMPO=`${CURL} -o /dev/null -s -w %{time_total} $i`
                echo " (ext)" $i ${TIEMPO} ${TAMANO};
            else
                TAMANO=`${CURL} -sI http://${DOMAIN}/$i | grep Content-Length | cut -d ' ' -f 2`
                TIEMPO=`${CURL} -o /dev/null -s -w %{time_total} http://${DOMAIN}/$i`
                echo " (intB) http://${DOMAIN}/$i" ${TIEMPO} ${TAMANO}
            fi
        fi
    done


    rm plano
    rm plano2
    echo -e ""
}


# ------------------------------------------------------------------------------
# Ayuda del comando
# ------------------------------------------------------------------------------
showHelp()
{
	echo -e "[bench.sh] - Hace mediciones de rendimiento de cualquier URL que se le pase"
	echo -e "como parámetro. Está basado en ab, curl y wget. Los resultados pueden variar"
	echo -e "dependiendo de la latencia de red, el estado del host, etc..."
	
	echo -e "\nModo de empleo: bench.sh [OPCIONES]"
	echo -e "\t--url o -u http://www.domain.com/test-url/\tPetición a analizar"
	echo -e "\t--help o -h\t\t\t\t\tPara ver este menú de ayuda\n"
	
	echo -e "Ejemplos:"
	echo -e "\t $ isp.sh -u http://www.domain.com/"
}

# ------------------------------------------------------------------------------
# Main
# ------------------------------------------------------------------------------
while [ $# -ne 0 ]; do
        case "$1" in
                --help|-h) 		showHelp; exit 0;;
                --url|-u) 	    URL="$2";
                                DOMAIN=`echo ${URL} | sed -e "s/[^/]*\/\/\([^@]*@\)\?\([^:/]*\).*/\2/"`;
                                echo -e "=== Testing ${URL} (${DOMAIN}) ==="
                                go;
                                exit 0;;
                *) 				shift;
        esac
done

[raw] - Pasted by: r0sk on bash on Nov. 7, 2011