#!/bin/bash ################################################################################ # bench.sh - Hace mediciones de rendimiento de cualquier URL que se le pase # v0.1 r0sk 07.11.2011 ################################################################################ # ------------------------------------------------------------------------------ # 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 -n1000 -c30 ===" ${AB} -n1000 -c30 -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` echo " (int)" $i ${TAMANO}; else if [[ $i == http* ]]; then TAMANO=`${CURL} -sI $i | grep Content-Length | cut -d ' ' -f 2` echo " (ext)" $i ${TAMANO}; else TAMANO=`${CURL} -sI http://${DOMAIN}/$i | grep Content-Length | cut -d ' ' -f 2` echo " (int)" http://${DOMAIN}/$i $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` echo " (int)" $i $TAMANO; else if [[ $i == http* ]]; then TAMANO=`${CURL} -sI $i | grep Content-Length | cut -d ' ' -f 2` echo " (ext)" $i $TAMANO; else TAMANO=`${CURL} -sI http://${DOMAIN}/$i | grep Content-Length | cut -d ' ' -f 2` echo " (int)" http://${DOMAIN}/$i $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` echo " (int)" $i $TAMANO; else if [[ $i == http* ]]; then TAMANO=`${CURL} -sI $i | grep Content-Length | cut -d ' ' -f 2` echo " (ext)" $i $TAMANO; else TAMANO=`${CURL} -sI http://${DOMAIN}/$i | grep Content-Length | cut -d ' ' -f 2` echo " (int)" http://${DOMAIN}/$i $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