Skip to content

Instantly share code, notes, and snippets.

@xorilog
Forked from ervinb/docker-registry-benchmark.sh
Created October 14, 2020 11:34
Show Gist options
  • Save xorilog/cbddfa43d998e098bebb2b203a5c703a to your computer and use it in GitHub Desktop.
Save xorilog/cbddfa43d998e098bebb2b203a5c703a to your computer and use it in GitHub Desktop.
#!/bin/bash
regions=(local ca-central-1 eu-central-1 ap-southeast-1 ap-northeast-1 us-east-1)
image_name_base="cache-bench"
image_sizes_in_mb=(64 512 1024)
dockerfile="Dockerfile.sample"
log_file=docker-bench.log
local_registry_dir=${SEMAPHORE_CACHE_DIR:-'registry_data'}
function init-dockerfile() {
if ! [ -e $dockerfile ]; then
echo "| Creating ${dockerfile}"
cat <<DOCKERFILE > $dockerfile
FROM alpine
ARG SIZE
RUN dd if=/dev/urandom of=outputfile.out bs=1024k count=\$SIZE
RUN ls -lah outputfile.out
DOCKERFILE
fi
}
function stopwatch() {
/usr/bin/time -f '|> runtime: %E\n' $@
}
function docker() {
stopwatch docker $@
}
function setup-local-registry() {
echo '{"insecure-registries" : ["localhost:5000"]}' | sudo tee /etc/docker/daemon.json
sudo service docker restart
docker run -d -p 5000:5000 -v $local_registry_dir/registry:/var/lib/registry --restart=always --name registry registry:2
sleep 5
curl http://localhost:5000/v2/_catalog
}
function benchmark() {
# a variable passed as a first argument will be set to
# contain the runtime
local runtime="$1"; shift
# hook up new file descriptors to capture time
exec 3>&1 4>&2
eval $runtime=$(TIMEFORMAT="%E"; { time $@ 1>&3 2>&4; } 2>&1)
# clean up the custom file descriptors
exec 3>&- 4>&-
}
function benchmark-push-pull() {
image=$1
registry_url=$2
report_string=""
echo "| Tagging '$image' for region '${region}'"
docker tag $image $registry_url
report_string+="$region|"
report_string+="$image|"
echo "| Pushing image to ${registry_url}"
benchmark push_runtime docker push $registry_url
report_string+="$push_runtime|"
echo "| Removing local images and tags..."
docker rmi -f $image
docker rmi -f ${registry_url}
echo "| Pulling image from ${registry_url}"
benchmark pull_runtime docker pull $registry_url
report_string+="$pull_runtime|"
echo "| Removing local image..."
docker rmi -f ${registry_url}
echo $report_string >> $log_file
}
function build-images() {
init-dockerfile
echo "| Building local images..."
echo '.semaphore-cache' > .dockerignore
if ! [ -e $dockerfile ]; then
echo "$dockerfile missing!"
exit 1
fi
for size in ${image_sizes_in_mb[@]}; do
benchmark build_runtime docker build --build-arg SIZE=$size -t "${image_name_base}-${size}" -f $dockerfile .
done
}
function run-benchmark() {
for region in ${regions[@]}; do
build-images
if ! [ $region == "local" ]; then
aws ecr get-login --region $region --no-include-email | bash
fi
for size in ${image_sizes_in_mb[@]}; do
image="${image_name_base}-${size}"
ecr_regional_url="501965974906.dkr.ecr.${region}.amazonaws.com/cache-bench:latest"
local_url="localhost:5000/cache-bench:latest"
echo -e "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo -e "Image: ${image}\nRegion: ${region}\nSize: ${size}MB\n" | column -t -s $' '
echo "--------------------------------"
if [ $region == "local" ]; then
benchmark-push-pull $image $local_url
else
benchmark-push-pull $image $ecr_regional_url
echo "| Removing image from ECR"
aws ecr batch-delete-image --region $region --repository-name cache-bench --image-ids imageTag=latest
fi
echo -e "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"
done
done
}
function print-report() {
report=docker-benchmark-report.log
unique_images=$(cat $log_file | cut -d '|' -f2 | tail -n+2 | sort -k2,2 | uniq)
echo "Region|Image|Push time|Pull time" > $report
for image in $unique_images; do
grep "$image" $log_file | sort -t '|' -k3,3n >> $report
done
echo -e "\nPrinting raw table for easier importing to Numbers:\n"
cat $report
echo -e "\nFormatted table:\n"
cat $report | column -t -s $'|'
}
setup-local-registry
run-benchmark
print-report
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment