Skip to content

Instantly share code, notes, and snippets.

@dansimau
Created October 13, 2011 12:52
Show Gist options
  • Save dansimau/1284163 to your computer and use it in GitHub Desktop.
Save dansimau/1284163 to your computer and use it in GitHub Desktop.
Takes a list of URLs from a file, curls them and saves the load times to a CSV file
#!/bin/bash
FILE=$1
NUM=$2
[ "$3" == "-v" ] && VERBOSE=1
usage() {
echo "Usage: $0 <file of URLs> <number of requests to do per URL> [-v]" >&2
}
die() {
echo "$1" >&2
[ "$2" != "" ] && exit $2
exit 1
}
output() {
[ "$VERBOSE" == 1 ] && echo $2 "$1"
}
[ "$FILE" == "" ] && usage && die "You must specify a file of URLs to perform the benchmark against."
[ "$NUM" == "" ] && usage && die "You must specify the number of times to perform the request for each URL."
[ ! -r "$FILE" ] && die "ERROR: File '$FILE' does not exist or is not readable."
# Gather some stats
STATS_URL_COUNT=$(wc -l "$FILE" | awk '{print $1}')
STATS_REQUEST_COUNT=$(($STATS_URL_COUNT * $2))
for url in $(cat $FILE); do
output "----"
output "URL: $url"
run=1
echo -n "\"$url\"," >> benchmark-results.csv
while [ $run -lt $(($NUM+1)) ]; do
output " Request #$run: " -n
CURL_RESPONSE=$(/usr/bin/curl --connect-timeout 5 -s -v -o/dev/null -w 'Request time:%{time_total}\n' "$url" 2>&1)
CURL_STATUS=$?
# Check curl returned OK
if [ $CURL_STATUS -gt 0 ]; then
output "curl error $CURL_STATUS"
echo -n "\"err: curl: $CURL_STATUS\"," >> benchmark-results.csv
run=$((run+1))
continue
fi
# Check that it was a 200 OK
if [ $(echo "$CURL_RESPONSE" |grep '200 OK' |wc -l |awk '{print $1}') -lt 1 ]; then
HTTP_RETURN_STATUS=$(echo "$CURL_RESPONSE" |grep -E '^HTTP')
output "$HTTP_RETURN_STATUS"
echo -n "\"err: $HTTP_RETURN_STATUS\"," >> benchmark-results.csv
run=$((run+1))
continue
fi
# Otherwise print the time it took
REQUEST_TIME=$(echo "$CURL_RESPONSE" |grep 'Request time:'|cut -d':' -f2)
output $REQUEST_TIME
echo -n "\"$REQUEST_TIME\"" >> benchmark-results.csv
if [ $run -ne $NUM ]; then
echo -n "," >> benchmark-results.csv
fi
run=$((run+1))
done
echo "" >> benchmark-results.csv
done
<?php
// Get the data
$fh = fopen("benchmark-results.csv", "r");
$c = 0;
while (($record = fgetcsv($fh, 1000, ",")) !== FALSE) {
$data[$c]->url = $record[0];
for ($i = 1; $i < (count($record)); $i++) {
$data[$c]->times[$i] = (float)$record[$i];
}
$c++;
}
fclose($fh);
foreach ($data as &$result) {
// Loop through each recorded time. Remove the lowest and highest (if there's more than 3).
if (count($result->times) > 3) {
// Sort the times (lowest to highest)
sort($result->times, SORT_NUMERIC);
// Remove first and last times
array_shift($result->times);
array_pop($result->times);
}
// Calculate and store the average of the remaining times
$result->average_time = round(array_sum($result->times) / count($result->times), 3);
echo "\"" . $result->url . "\",";
echo "\"" . $result->average_time . "\"\n";
}
?>
@iljamm
Copy link

iljamm commented Apr 30, 2020

Huge thanks for this, it was very useful in my project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment