-
Notifications
You must be signed in to change notification settings - Fork 5
/
phpbenchkit.sh
executable file
·185 lines (160 loc) · 5.04 KB
/
phpbenchkit.sh
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env bash
docker ps | grep phpbenchmarks_benchmark-kit > /dev/null
if [ "$?" == 0 ]; then
containerStarted=true
else
containerStarted=false
fi
function exitScript() {
if [ "$?" != "0" ]; then
echo -e "\e[41m Error, script canceled. \e[0m"
fi
}
set -e
trap exitScript EXIT
readonly ROOT_DIR=$(realpath $(dirname $(realpath $0)))
readonly CONTAINER_NAME="phpbenchmarks_benchmark-kit"
readonly SELFUPDATE_CONTAINER_NAME="${CONTAINER_NAME}_selfupdate"
readonly DEFAULT_CONFIG_PATH="/tmp/phpbenchmarkkit.default.sh"
readonly DOCKER_IMAGE_NAME="phpbenchmarks/benchmark-kit:4"
readonly BENCHMARK_KIT_PATH="/var/benchmark-kit"
function addHost() {
local HOST="$1"
if [ "$(cat /etc/hosts | grep -c $HOST)" -eq 0 ]; then
echo -e "Add host \e[32m$HOST\e[0m."
sudo /bin/sh -c "echo \"127.0.0.1 $HOST\" >> /etc/hosts"
fi
}
function startContainer() {
local defaultHostSourceCodePath=$(pwd)
local defaultNginxPort="8080"
if [ -f "$DEFAULT_CONFIG_PATH" ]; then
source $DEFAULT_CONFIG_PATH
fi
if [ "$hostSourceCodePath" == "" ]; then
echo -en "\e[44m Benchmark source code path [$defaultHostSourceCodePath]? \e[0m "
read hostSourceCodePath
if [ "$hostSourceCodePath" == "" ]; then
hostSourceCodePath=$defaultHostSourceCodePath
fi
else
echo -e "Source code: \e[32m$hostSourceCodePath\e[0m."
fi
if [ ! -d "$hostSourceCodePath" ]; then
echo -e "\e[41m Benchmark source code path $hostSourceCodePath is not a redirectory. \e[0m"
exit 1
fi
if [ "$nginxPort" == "" ]; then
echo -en "\e[44m Nginx port [$defaultNginxPort]? \e[0m "
read nginxPort
if [ "$nginxPort" == "" ]; then
nginxPort=$defaultNginxPort
fi
else
echo -e "Nginx port: \e[32m$nginxPort\e[0m."
fi
echo "#!/usr/bin/env bash" > $DEFAULT_CONFIG_PATH
echo "defaultHostSourceCodePath=$hostSourceCodePath" >> $DEFAULT_CONFIG_PATH
echo "defaultNginxPort=$nginxPort" >> $DEFAULT_CONFIG_PATH
if [ $kitAsVolume == true ]; then
kitAsVolumeDockerRunParameter="-v $ROOT_DIR:$BENCHMARK_KIT_PATH"
else
kitAsVolumeDockerRunParameter=""
fi
echo -e "Start \e[32m$CONTAINER_NAME\e[0m container."
docker run \
$ttyParameter \
-d \
--name=$CONTAINER_NAME \
--rm \
-p 127.0.0.1:$nginxPort:$nginxPort \
-v $hostSourceCodePath:/var/www/benchmark \
$kitAsVolumeDockerRunParameter \
$DOCKER_IMAGE_NAME \
> /dev/null
docker exec -it $CONTAINER_NAME /bin/bash -c "echo NGINX_PORT=$nginxPort > $BENCHMARK_KIT_PATH/.env.local"
docker exec -it $CONTAINER_NAME /bin/bash -c "echo HOST_SOURCE_CODE_PATH=$hostSourceCodePath >> $BENCHMARK_KIT_PATH/.env.local"
containerStarted=true
}
function updatePhpBenchKitScript()
{
set +e
docker stop phpbenchmarks_benchmark-kit_selfupdate > /dev/null 2>&1
set -e
docker run \
-it \
-d \
--name=$SELFUPDATE_CONTAINER_NAME \
--rm \
$DOCKER_IMAGE_NAME
local binPath="$ROOT_DIR/$(basename $0)"
echo -en "\e[43m sudo password could be asked to update $binPath \e[0m\n"
sudo docker cp $SELFUPDATE_CONTAINER_NAME:$BENCHMARK_KIT_PATH/phpbenchkit.sh "$binPath"
docker stop $SELFUPDATE_CONTAINER_NAME
}
function stopContainer() {
if [ $containerStarted == true ]; then
echo -e "Stop \e[32m$CONTAINER_NAME\e[0m container."
set +e
docker kill $CONTAINER_NAME
set -e
fi
}
stopContainer=false
restartContainer=false
kitAsVolume=false
consoleParams=""
selfUpdate=false
hostSourceCodePath=""
nginxPort=""
tty=true
for param in "$@"; do
if [ "$param" == "--stop" ]; then
stopContainer=true
elif [ "$param" == "--restart" ]; then
restartContainer=true
elif [ "$param" == "--dev" ]; then
kitAsVolume=true
elif [ "$param" == "--selfupdate" ]; then
selfUpdate=true
elif [ "${param:0:9}" == "--source=" ]; then
hostSourceCodePath=${param:9}
restartContainer=true
elif [ "${param:0:13}" == "--nginx-port=" ]; then
nginxPort=${param:13}
restartContainer=true
elif [ "$param" == "--no-tty" ]; then
tty=false
else
consoleParams="$consoleParams $param"
fi
done
if [ $tty == true ]; then
ttyParameter="-it"
else
ttyParameter="-t"
fi
if [ $selfUpdate == true ]; then
stopContainer
set +e
docker rmi --force $(docker images --format '{{.Repository}}:{{.Tag}}' | grep phpbenchmarks/benchmark-kit)
set -e
updatePhpBenchKitScript
exit 0
fi
if [ $stopContainer == true ]; then
stopContainer
exit 0
fi
if [ $restartContainer == true ]; then
stopContainer
startContainer
fi
if [ "$containerStarted" == false ]; then
startContainer
fi
addHost "benchmark-kit.loc"
addHost "phpinfo.benchmark-kit.loc"
addHost "statistics.benchmark-kit.loc"
addHost "preload-generator.benchmark-kit.loc"
docker exec $ttyParameter --user=phpbenchmarks $CONTAINER_NAME phpbenchkit $consoleParams