-
Notifications
You must be signed in to change notification settings - Fork 1
/
debian-apt-speed.sh
59 lines (45 loc) · 1.62 KB
/
debian-apt-speed.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
if [ "$(uname -s)" != "Linux" ]; then
echo "Platform not supported. Must be run on Linux. Aborting."
exit 1
fi
function dependency_check {
for comm in "$@"
do
command -v "$comm" >/dev/null 2>&1 || { echo >&2 "'$comm' is required to be installed."; MISSING_DEPENDENCY=true; }
done
if [ -n "$MISSING_DEPENDENCY" ]; then
echo "Aborting."
exit 1
fi
}
dependency_check "curl" "grep" "bc" "ping" "tail" "awk" "cut" "sort" "head"
HTML_MIRRORS=$(curl -sL https://www.debian.org/mirror/list-full)
readarray -t MIRRORS < <(echo "$HTML_MIRRORS" | grep -s -P "Packages over HTTP: <tt><a " | grep -s -o -P "https?://[^\"<]+/")
if [ -z "${MIRRORS}" ] || [[ ! "${MIRRORS[0]}" =~ ^https?:// ]]; then
echo "Could not fetch mirror list."
exit 1
fi
NUM_MIRRORS=${#MIRRORS[@]}
declare -A RESULT
echo
echo "Testing mirrors for speed..."
echo
for i in "${!MIRRORS[@]}"; do
MIRROR_NUM=$((i+1))
MIRROR=${MIRRORS[$i]}
# Download the first 102 400 bytes with 2 second timeout
SPEED_BPS=$(curl --max-time 2 -r 0-102400 -s -w %{speed_download} -o /dev/null "${MIRROR}/ls-lR.gz")
SPEED_KBPS=$(echo "$SPEED_BPS / 1024" | bc)
LATENCY_URL=$(echo ${MIRROR} | awk -F[/:] '{print $4}')
LATENCY=$(ping -q -c 1 -W 1 $LATENCY_URL | tail -1 | awk '{print $4}' | cut -d '/' -f 2)
echo "[$MIRROR_NUM/$NUM_MIRRORS] ${MIRROR} --> $SPEED_KBPS KB/s - $LATENCY ms"
RESULT["${MIRROR}"]="$SPEED_KBPS $LATENCY"
done
# Sort mirrors by speed and get the top 5
echo
echo "Top 5 fastest mirrors"
echo
for MIRROR in "${!RESULT[@]}"; do
echo "$MIRROR ${RESULT[$MIRROR]}"
done | sort -rn -k2 | head -5