Skip to content

Instantly share code, notes, and snippets.

@farrokhi
Forked from lox/mirror_test.sh
Last active November 10, 2022 05:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save farrokhi/71151176a961af88efc30aea8a998ee9 to your computer and use it in GitHub Desktop.
Save farrokhi/71151176a961af88efc30aea8a998ee9 to your computer and use it in GitHub Desktop.
A shell script to select the fastest freebsd-update mirror
#!/bin/sh
# best-mirror.sh
#
# Domesticaed for FreeBSD by Babak Farrokhi (farrokhi@FreeBSD.org)
#
# This script finds the fastest freebsd-update mirror based on
# data transfer rates from cURL.
#
# Note: I used a static list of mirrors, which is not the best way to
# get the list. You may want to try following command to get a
# list of active mirrors:
#
# dig +short -t srv _http._tcp.update.freebsd.org
#
# Originally taken from:
#
# 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="update1.freebsd.org update2.freebsd.org update3.freebsd.org update4.freebsd.org update5.freebsd.org"
TESTFILE="/to-12.0-RC1/amd64/bp/0cfaf7bd9064730d8df2839f0ecfdd1b8311946b95315917c2e6832d4b5ac0e0-35c52504b158099f3b7eb9426ef0fdf7f3d626e528298de4f8b6e89565936b3e"
TIMEOUT=10
SAMPLES=3
BYTES=300000 # 300KB
# tests the provided mirror, outputs times
test_mirror() {
if [ -z ${HTTP_PROXY} ]; then
PROXY_SETTINGS=""
else
PROXY_SETTINGS="-x ${HTTP_PROXY}"
fi
for s in $(seq 1 $SAMPLES) ; do
time=$(curl -r 0-$BYTES --max-time $TIMEOUT ${PROXY_SETTINGS} --fail --silent --output /dev/null --write-out %{time_total} ${1}${TESTFILE})
if [ $? -eq 0 ]; then
echo $time
else
echo "nan"
fi
done
}
# tests all the mirrors, outputs mirror and average time
test_all_mirrors() {
for MIRROR in $MIRRORS; do
results=$(test_mirror $MIRROR)
mean=$(mean $results)
if [ "$mean" != "nan" ] ; then
printf '%-25s %.5f\n' $MIRROR $mean
else
printf '%-25s failed, ignoring\n' $MIRROR 1>&2
fi
done
}
# calculates the mean of all arguments
mean() {
len=$#
echo $* | tr " " "\n" | sort -n | head -n $(((len+1)/2)) | tail -n 1
}
# print mirrors in order of time
test_all_mirrors | sort -n -k 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment