Skip to content

Commit c51555c

Browse files
committedMar 18, 2020
fix
1 parent 2e0aeec commit c51555c

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed
 

‎shell/docker_hub_speed_test.sh

+39-5
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,40 @@ mirrors=(
3333
# function
3434
######################################################################################################
3535

36+
function shutdown() {
37+
tput cnorm # reset cursor
38+
ps -ef | grep "$0" | awk '{print $2}' | xargs kill -9 '{}' >/dev/null 2>&1
39+
}
40+
41+
trap shutdown EXIT
42+
43+
function spinner() {
44+
# make sure we use non-unicode character type locale
45+
# (that way it works for any locale as long as the font supports the characters)
46+
local LC_CTYPE=C
47+
speed_test "$@" &
48+
sleep 1
49+
50+
local pid=$(ps -ef | grep -E '[w]get.*-4O /dev/null -T300' | awk '{print $2}') # Process Id of the previous running command
51+
52+
local spin='◐◓◑◒'
53+
local charwidth=3
54+
55+
local i=0
56+
tput civis # cursor invisible
57+
while kill -0 $pid 2>/dev/null; do
58+
local i=$(((i + $charwidth) % ${#spin}))
59+
printf "%s" "${spin:$i:$charwidth}"
60+
61+
echo -en "\033[1D"
62+
sleep .1
63+
done
64+
tput cnorm
65+
wait
66+
}
67+
3668
speed_test() {
37-
local output=$(LANG=C wget --header="$3" -4O /dev/null -T300 "$1" 2>&1)
69+
local output=$(LANG=C wget ${3:+"--header="}"$3" -4O /dev/null -T300 "$1" 2>&1)
3870
local speed=$(printf '%s' "$output" | awk '/\/dev\/null/ {speed=$3 $4} END {gsub(/\(|\)/,"",speed); print speed}')
3971
local ipaddress=$(printf '%s' "$output" | awk -F'|' '/Connecting to .*\|([^\|]+)\|/ {print $2}'| tail -1)
4072
local time=$(printf '%s' "$output" | awk -F= '/100% / {print $2}')
@@ -62,7 +94,7 @@ echo -e "\n\nDocker Hub mirror site speed test"
6294

6395
echo -e "\n[Mirror Site]"
6496
for mirror in ${!mirrors[*]}; do
65-
printf "${PLAIN}%-14s${GREEN}%-20s${PLAIN}\n" ${mirror} ": ${mirrors[$mirror]}"
97+
printf "${PLAIN}%-14s${GREEN}%-20s${PLAIN}\n" ${mirror} ": ${mirrors[$mirror]}"
6698
done
6799
printf "${PLAIN}%-14s${GREEN}%-20s${PLAIN}\n" "docker" ": https://registry-1.docker.io"
68100

@@ -71,7 +103,7 @@ echo -e "Test Image : ${YELLOW}${image_name}:${image_tag}${PLAIN}"
71103

72104
docker_token=$(curl -fsSL "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${image_name}:pull" | awk '-F"' '{print $4}')
73105
image_manifests=$(curl -fsSL -H "Authorization: Bearer ${docker_token}" "https://registry-1.docker.io/v2/${image_name}/manifests/${image_tag}" | awk -F'"' '/"blobSum":/ {print $4}')
74-
image_layer=$( echo $image_manifests | tr ' ' '\n' | sort -u| head -1 )
106+
image_layer=$(echo $image_manifests | tr ' ' '\n' | sort -u| head -1)
75107
echo -e "Download layer : ${YELLOW}${image_layer}${PLAIN}\n"
76108

77109
printf "%-14s%-20s%-14s%-20s%-14s\n" "Site Name" "IPv4 address" "File Size" "Download Time" "Download Speed"
@@ -80,7 +112,9 @@ for mirror in ${!mirrors[*]}; do
80112
image_manifests=$(curl -s "${mirror}/v2/library/${image_name}/manifests/${image_tag}" | awk -F'"' '/"blobSum":/ {print $4}')
81113
image_layer=$( echo $resp | tr ' ' '\n' | sort -u | head -1)
82114
fi
83-
speed_test "${mirrors[$mirror]}/v2/${image_name}/blobs/${image_layer}" ${mirror}
115+
spinner "${mirrors[$mirror]}/v2/${image_name}/blobs/${image_layer}" ${mirror}
84116
done
85-
speed_test "https://registry-1.docker.io/v2/${image_name}/blobs/${image_layer}" "docker" "Authorization: Bearer $docker_token"
117+
118+
spinner "https://registry-1.docker.io/v2/${image_name}/blobs/${image_layer}" "docker" "Authorization: Bearer $docker_token"
86119
echo
120+

0 commit comments

Comments
 (0)