Skip to content

Instantly share code, notes, and snippets.

@lox
Last active November 7, 2023 17:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save lox/9152137 to your computer and use it in GitHub Desktop.
Save lox/9152137 to your computer and use it in GitHub Desktop.
A bash script to select the fastest ubuntu apt geo-mirror
#!/bin/bash -e
# 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)"
RELEASE=$(lsb_release -c -s 2>/dev/null || echo 14.04)
MIRRORS=$(curl -s http://mirrors.ubuntu.com/mirrors.txt)
TESTFILE="dists/$RELEASE/main/binary-amd64/Packages.bz2"
TIMEOUT=1
SAMPLES=3
BYTES=511999 #1mb
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 -r 0-$BYTES --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=$(mean $(test_mirror $MIRROR))
if [ "$mean" != "-nan" ] ; then
printf '%-60s %.5f\n' $MIRROR $mean
else
printf '%-60s failed, ignoring\n' $MIRROR 1>&2
fi
done;
}
# calculates the mean of all arguments
function mean() {
len=$#
echo $* | tr " " "\n" | sort -n | head -n $(((len+1)/2)) | tail -n 1
}
# 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
@lox
Copy link
Author

lox commented Feb 22, 2014

From australia:

vagrant@ubuntu-13:/vagrant/vagrant$ ./mirror-test.sh -s5
http://mirror.waia.asn.au/ubuntu/                           0.19080 σ 0.037
http://ubuntu.mirror.uber.com.au/archive/                   0.21940 σ 0.131
http://mirror.as24220.net/pub/ubuntu/                       0.23880 σ 0.047
http://mirror.netspace.net.au/pub/ubuntu/                   0.25700 σ 0.051
http://ubuntu.mirror.crucial.com.au/                        0.26900 σ 0.062
http://mirror.aarnet.edu.au/pub/ubuntu/archive/             0.26940 σ 0.174
http://mirror.overthewire.com.au/ubuntu/                    0.27420 σ 0.057
http://mirror.internode.on.net/pub/ubuntu/ubuntu/           0.28080 σ 0.154
http://ubuntu.mirror.serversaustralia.com.au/ubuntu/        0.29120 σ 0.081
http://mirror.optus.net/ubuntu/                             0.29760 σ 0.078
http://mirror.as24220.net/pub/ubuntu-archive/               0.29980 σ 0.160
http://ftp.iinet.net.au/pub/ubuntu/                         0.31360 σ 0.204
http://archive.ubuntu.com/ubuntu/                           0.82380 σ 0.052

@lox
Copy link
Author

lox commented May 21, 2014

Useful for updating the mirror your apt-cacher-ng is using

bash -c "$(curl -fsSL https://gist.githubusercontent.com/lox/9152137/raw/mirror_test.sh)" | head -n1 | awk '{ print $1 }' | sudo tee /etc/apt-cacher-ng/backends_ubuntu

@jsarenik
Copy link

Wonderful script. These days the HTTP request to http://mirrors.ubuntu.com/mirrors.txt seems to be geo-located already (according to the source IP - tested with tor).

See https://eomwandho.wordpress.com/2014/03/20/ubuntu-apt-get-select-best-server-using-apt-get-mirrors/

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