Skip to content

Instantly share code, notes, and snippets.

@bugcy013
Forked from lox/mirror_test.sh
Created April 28, 2014 01:46
Show Gist options
  • Select an option

  • Save bugcy013/11359902 to your computer and use it in GitHub Desktop.

Select an option

Save bugcy013/11359902 to your computer and use it in GitHub Desktop.
#!/bin/bash
# mirror_test.sh
# benchmarks closest ubuntu mirrors and outputs them in speed order
# bash -c "$(curl -fsSL https://gist.githubusercontent.com/lox/9152137/raw/mirror_test.sh)"
MIRRORS=$(curl -s http://mirrors.ubuntu.com/mirrors.txt)
TESTFILE="dists/$(lsb_release -c -s)/main/binary-amd64/Packages.bz2"
TIMEOUT=3
SAMPLES=3
usage() {
cat << EOF
usage: $0 options
Tests out close by mirrors and prints them in speed order
OPTIONS:
-h Show this message
-s <n> Number of samples to take (default $SAMPLES)
EOF
}
# tests the provided mirror, outputs times
function test_mirror() {
for s in $(seq 1 $SAMPLES) ; do
time=$(curl --max-time $TIMEOUT --silent --output /dev/null --write-out %{time_total} ${1}${TESTFILE})
if [ "$TIME" == "0.000" ] ; then exit 1; fi
echo $time
done
}
# tests all the mirrors, outputs mirror and average time
function test_all_mirrors() {
for MIRROR in $MIRRORS ; do
mean=$(test_mirror $MIRROR | awk -vx=0 '{
delta = $1 - avg;
avg += delta / NR;
mean2 += delta * ($1 - avg);
} END { printf ("%.5f\n",avg+sqrt(mean2 / NR)); }')
if [ "$mean" != "-nan" ] ; then
printf '%-60s %.5f\n' $MIRROR $mean
else
printf '%-60s failed, ignoring\n' $MIRROR 1>&2
fi
done;
}
# parse arguments
while getopts "hs:" OPTION
do
case $OPTION in
s) SAMPLES=$OPTARG ;;
h) usage; exit 1 ;;
?) usage; exit ;;
esac
done
# print mirrors in order of time
test_all_mirrors | sort -n -k 2 | grep http
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment