File: build_speed.sh

package info (click to toggle)
golang-github-containers-buildah 1.39.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,724 kB
  • sloc: sh: 2,398; makefile: 236; perl: 187; asm: 16; awk: 12; ansic: 1
file content (128 lines) | stat: -rwxr-xr-x 3,461 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#! /bin/sh

# The main goal of this script is test and time builds using Buildah or Docker.
# We hope to use it to help optimize Buildah build performance
#
# It takes two options
# First option tells the type of the container image
# build to do.  Valid options are:
#       Docker  - docker build
#       Buildah - buildah bud
#       Both    - Do docker build followed by buildah bud
#
# Second Option specifies a directory or cleanup
# The script will 'find' files beginning with Dockerfile, for each Dockerfile
# it finds it will run a build with the Dockerfile and directory for the
# context. When it does the builds, it will call time on them to show how
# long the builds take.  The created image name will be a combination of the
# lowercased Directory name that the Dockerfile was found in plus the lower
# cased dockerfile name.
#
# if the second field is cleanup, the script will remove all images from the
# specified builder.
#
# The script does not check for conflicts on naming.
#
# Outputs file:
#
#
# cat /tmp/build_speed.json
# {
#   "/usr/share/fedora-dockerfiles/redis/Dockerfile": {
#     "docker": {
#       "command": "docker build -f /usr/share/fedora-dockerfiles/redis/Dockerfile -t redis_dockerfile /usr/share/fedora-dockerfiles/redis",
#       "real": "3:28.70"
#     },
#     "buildah": {
#       "command": "buildah bud --layers -f /usr/share/fedora-dockerfiles/redis/Dockerfile -t redis_dockerfile /usr/share/fedora-dockerfiles/redis",
#       "real": "2:55.48"
#     }
#   }
# }
#
# Examples uses
#     ./build_speed.sh Docker ~/MyImages
#     ./build_speed.sh Both /usr/share/fedora-dockerfiles/django/Dockerfile

#totalsfile=$(mktemp /tmp/buildspeedXXX.json)
totalsfile=/tmp/build_speed.json
commaDockerfile=""

echo -n '{' > $totalsfile
Dockerfiles() {
    find -L $1 -name Dockerfile\*
}

Buildah() {
    Name=$1
    Dockerfile=$2
    Context=$3
    echo buildah bud --layers -f ${Dockerfile} -t ${Name} ${Context}
    Time buildah bud --layers -f ${Dockerfile} -t ${Name} ${Context}
}

Time() {
    outfile=$(mktemp /tmp/buildspeedXXX)
    /usr/bin/time -o $outfile --f "%E" $@
    echo "{\"engine\": \"$1\", \"command\": \"$@\", \"real\": \"$(cat ${outfile})\"}"
    echo -n "${comma}\"$1\": {\"command\": \"$@\", \"real\": \"$(cat ${outfile})\"}" >> $totalsfile
    comma=","
    rm -f $outfile
}

Docker() {
    Name=$1
    Dockerfile=$2
    Context=$3
    echo docker build -f ${Dockerfile} -t ${Name} ${Context}
    Time docker build -f ${Dockerfile} -t ${Name} ${Context}
}

Both() {
    comma=""
    echo -n "${commaDockerfile}\"$2\": {" >> $totalsfile
    commaDockerfile=","
    Docker $1 $2 $3
    Buildah $1 $2 $3
    echo -n "}" >> $totalsfile
}

Docker_cleanup() {
    docker rmi --force $(docker images  -q) 
}

Buildah_cleanup() {
    buildah rmi --force --all
}

Both_cleanup() {
    Docker_cleanup
    Buildah_cleanup
}

Cmd=${1?Missing CMD argument}
Path=${2?Missing PATH argument}

case "$Cmd" in
    Docker)   ;;
    Buildah)  ;;
    Both)     ;;
    *)     echo "Invalid command '$Cmd'; must be Buildah, Docker, or Both"; exit 1;;
esac


if [ "$Path" == "cleanup" ]; then
    ${Cmd}_cleanup
    exit 0
fi

for i in $(Dockerfiles ${Path});do
    name=$(basename $(dirname $i) | sed -e 's/\(.*\)/\L\1/')
    name=${name}_$(basename $i | sed -e 's/\(.*\)/\L\1/')
    echo ${Cmd} ${name} $i $(dirname $i)
    ${Cmd} ${name} $i $(dirname $i)
done

echo '}'>>$totalsfile
echo cat $totalsfile
cat $totalsfile