|
#!/usr/bin/env bash |
|
|
|
# Docker Mirror Benchmark Script |
|
# Author: Mr. Coder |
|
# Description: Tests ping, HTTP latency and pull time for given Docker registry mirrors. |
|
|
|
set -euo pipefail |
|
|
|
IMAGE="alpine:latest" |
|
TMP_DIR="$(mktemp -d)" |
|
LOG_FILE="$TMP_DIR/benchmark_$(date +%s).log" |
|
|
|
# Usage instructions |
|
usage() { |
|
echo "Usage: $0 <mirror1> <mirror2> ..." |
|
echo "Example:" |
|
echo " $0 https://focker.ir https://dockerhub.ir" |
|
exit 1 |
|
} |
|
|
|
# Check required CLI tools |
|
check_dependencies() { |
|
for cmd in curl docker ping; do |
|
if ! command -v "$cmd" &>/dev/null; then |
|
echo "β Required command '$cmd' not found. Please install it." |
|
exit 1 |
|
fi |
|
done |
|
} |
|
|
|
# Extract hostname from URL |
|
extract_hostname() { |
|
local url="$1" |
|
echo "$url" | awk -F/ '{print $3}' |
|
} |
|
|
|
# Ping host and return success/fail |
|
ping_host() { |
|
local host="$1" |
|
if ping -c 1 -W 1 "$host" &>/dev/null; then |
|
echo "OK" |
|
else |
|
echo "FAIL" |
|
fi |
|
} |
|
|
|
# Measure HTTP latency via curl |
|
measure_latency() { |
|
local mirror="$1" |
|
local url="${mirror%/}/v2/" |
|
curl -o /dev/null -s -w "%{time_starttransfer}" "$url" || echo "fail" |
|
} |
|
|
|
# Docker pull using registry mirror |
|
pull_image() { |
|
local mirror="$1" |
|
local start end duration status |
|
|
|
start=$(date +%s) |
|
if docker pull --registry-mirror="$mirror" "$IMAGE" &> "$TMP_DIR/pull.log"; then |
|
end=$(date +%s) |
|
duration=$((end - start)) |
|
echo "$duration OK" |
|
else |
|
echo "N/A FAIL" |
|
fi |
|
} |
|
|
|
# Benchmark single mirror |
|
benchmark_mirror() { |
|
local mirror="$1" |
|
local host latency pull_result pull_time pull_status |
|
|
|
host="$(extract_hostname "$mirror")" |
|
echo "π Testing mirror: $mirror (host: $host)" |
|
|
|
# Ping check |
|
if [ "$(ping_host "$host")" = "OK" ]; then |
|
echo " β
Ping successful" |
|
|
|
latency=$(measure_latency "$mirror") |
|
read -r pull_time pull_status <<< "$(pull_image "$mirror")" |
|
else |
|
echo " β Ping failed. Skipping..." |
|
latency="N/A" |
|
pull_time="N/A" |
|
pull_status="UNREACHABLE" |
|
fi |
|
|
|
echo -e "$mirror\t$latency\t$pull_time\t$pull_status" >> "$LOG_FILE" |
|
} |
|
|
|
# Script entry point |
|
main() { |
|
if [ "$#" -lt 1 ]; then |
|
usage |
|
fi |
|
|
|
check_dependencies |
|
|
|
echo -e "Mirror\tLatency(s)\tPullTime(s)\tStatus" > "$LOG_FILE" |
|
|
|
for mirror in "$@"; do |
|
benchmark_mirror "$mirror" |
|
done |
|
|
|
echo "" |
|
echo "π Benchmark results:" |
|
column -t -s $'\t' "$LOG_FILE" |
|
echo -e "\nβ
Finished. Full log: $LOG_FILE" |
|
} |
|
|
|
main "$@" |