Skip to content

Instantly share code, notes, and snippets.

@philpoore
Created September 5, 2018 19:13
Show Gist options
  • Save philpoore/05eca572f3aadf70f529c470ac679147 to your computer and use it in GitHub Desktop.
Save philpoore/05eca572f3aadf70f529c470ac679147 to your computer and use it in GitHub Desktop.
Generate JSON output of docker build step timing, useful for profiling
#!/bin/bash
# script: time-docker-build.sh
#
# All command line arguments are passed to docker build command.
#
# usage: ./time-docker-build.sh
#
DATE_FORMAT="+%s"
(
# Output START line
echo "$(date $DATE_FORMAT) | - 0 - START"
docker build $* . | \
grep "^Step" | \
while read line ;
do
# Output build output prefixed with date
echo "$(date $DATE_FORMAT) | $line";
done;
# Output END line
echo "$(date $DATE_FORMAT) | - -1 - END"
) | (
# Generate JSON array output.
# - START is step: 0
# - END is step: -1
echo "["
FIRST_RUN=true
while read line ;
do
[[ -z "$FIRST_RUN" ]] && echo "," # if not first line, print ','
lineArray=($line)
time="${lineArray[0]}" # step is 0th
step="${lineArray[3]}" # step is 2nd
cmd="${lineArray[@]:5}" # cmd is everything after 5th
stepNum=${step/\/*/}
escapedCmd="${cmd//\"/\\\"}" # escape all double quotes '"'
echo " {"
echo " \"time\": $time,"
echo " \"step\": $stepNum,"
echo " \"cmd\": \"$escapedCmd\""
echo -n " }"
unset FIRST_RUN
done
echo
echo "]"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment