Skip to content

Instantly share code, notes, and snippets.

@MM25Zamanian
Created June 23, 2025 13:38
Show Gist options
  • Save MM25Zamanian/4dc6786c7c5d237cdca7e21e4de4037b to your computer and use it in GitHub Desktop.
Save MM25Zamanian/4dc6786c7c5d237cdca7e21e4de4037b to your computer and use it in GitHub Desktop.
chmod +x docker-mirror-benchmark.sh
./docker-mirror-benchmark.sh \
  https://focker.ir \
  https://dockerhub.ir \
  https://mirror.gcr.mirrors.iranserver.com \
  https://docker.nikancloud.ir \
  https://docker-mirror.ir
#!/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 "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment