#!/bin/bash # Runs ApacheBench a preset number of times and exports the results as a CSV # # Bryant Casteel # http://bethings.postplatinum.com/tag/ab2csv/ # 2010-12-05 # configuration MIN_CONNECTIONS=1 MAX_CONNECTIONS=10 SAMPLES=10 TEMP_FILE="`basename "$0" '.sh'`.tmp" HEADER_FILE="`basename "$0" '.sh'`.head.dat" DATA_FILE="`basename "$0" '.sh'`.body.dat" OUT_FILE="`basename "$0" '.sh'`.csv" rm "$OUT_FILE" # test each URL headers_sent=0 for url in $*; do connections=$MIN_CONNECTIONS interval=1 while [ $connections -le $MAX_CONNECTIONS ]; do # perform the test hits=$[$connections * $SAMPLES] ab -n $hits -c $connections "$url" > "$TEMP_FILE" # parse the results first_col=1 grep '^\([a-zA-Z0-9]\+\| [^ ]\)\+: \+\([^ ]\+\| [^ ]\)\+$' "$TEMP_FILE" | while read line; do field=`echo "$line" | cut -d: -f1` value=`echo "$line" | cut -c25-` # check for numeric values echo "$value" | grep -q '^[0-9]' if [[ $? -eq 0 ]]; then field="$field"`echo "$value" | sed 's/^[0-9\.]\+//'` value=`echo "$value" | sed 's/^\([0-9\.]\+\).*$/\1/'` fi # output headers if [[ $headers_sent -eq 0 ]]; then if [[ $first_col -ne 1 ]]; then echo -n ',' >> "$HEADER_FILE" fi echo -n '"'"$field"'"' >> "$HEADER_FILE" fi # output values if [[ $first_col -eq 1 ]]; then first_col=0 else echo -n ',' >> "$DATA_FILE" fi echo -n '"'"$value"'"' >> "$DATA_FILE" done # end the output if [[ $headers_sent -eq 0 ]]; then headers_sent=1 echo >> "$HEADER_FILE" fi echo >> "$DATA_FILE" # increment the connections by powers of 10 if [[ $[$connections / ($interval * 10)] -ge 1 ]]; then # we've gone up a power of 10 interval=$[$interval * 10] fi connections=$[$connections + $interval] done done # combine the output cat "$HEADER_FILE" "$DATA_FILE" > "$OUT_FILE" # remove the temp files rm "$TEMP_FILE" "$HEADER_FILE" "$DATA_FILE"