-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_on_devcontainer.sh
More file actions
executable file
·433 lines (398 loc) · 11.8 KB
/
benchmark_on_devcontainer.sh
File metadata and controls
executable file
·433 lines (398 loc) · 11.8 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/bin/sh
set -eu
USAGE() {
cat <<'EOF'
Usage: ./benchmark_on_devcontainer.sh [--php-repo DIR|URL] [--php-ref REF] [--runtime all|fpm|frankenphp] [--runner read|write] [--scenario NAME] [--iterations N] [--warmup N] [--operations N] [--concurrency N] [--key-mode shared|distinct] [--key-space N] [--jit MODE] [--threads N] [--frankenphp-repo DIR|URL] [--frankenphp-ref REF] [--make-jobs N] [--skip-rebuild] [--build-dir DIR] [--output-dir DIR]
EOF
}
REQUIRE_VALUE() {
REQUIRE_OPTION=${1}
REQUIRE_COUNT=${2}
if test "${REQUIRE_COUNT}" -lt 2; then
echo "${REQUIRE_OPTION} requires a value" >&2
exit 1
fi
}
REQUIRE_COMMAND() {
COMMAND_NAME=${1}
if ! command -v "${COMMAND_NAME}" >/dev/null 2>&1; then
echo "${COMMAND_NAME} is required" >&2
exit 1
fi
}
REQUIRE_EXECUTABLE_PATH() {
PATH_NAME=${1}
DESCRIPTION=${2}
if test ! -x "${PATH_NAME}"; then
echo "${DESCRIPTION} is missing or not executable for --skip-rebuild: ${PATH_NAME}" >&2
exit 1
fi
}
REQUIRE_FILE_PATH() {
PATH_NAME=${1}
DESCRIPTION=${2}
if test ! -f "${PATH_NAME}"; then
echo "${DESCRIPTION} is missing for --skip-rebuild: ${PATH_NAME}" >&2
exit 1
fi
}
SCRIPT_DIR=$(CDPATH= cd "$(dirname "${0}")" && pwd)
DEFAULT_SOURCE_ROOT=$(CDPATH= cd "${SCRIPT_DIR}/.." && pwd)
PHP_REPO=${PHP_REPO:-${DEFAULT_SOURCE_ROOT}}
PHP_REF=${PHP_REF:-}
SOURCE_ROOT=
BENCHMARK_RUNTIME=${BENCHMARK_RUNTIME:-all}
RUNNER=${RUNNER:-}
SCENARIO=${SCENARIO:-}
ITERATIONS=${ITERATIONS:-}
WARMUP=${WARMUP:-}
OPERATIONS=${OPERATIONS:-}
CONCURRENCY=${CONCURRENCY:-}
KEY_MODE=${KEY_MODE:-}
KEY_SPACE=${KEY_SPACE:-}
JIT=${JIT:-off}
FRANKENPHP_THREADS=${FRANKENPHP_THREADS:-5}
FRANKENPHP_REPO=${FRANKENPHP_REPO:-https://github.com/php/frankenphp.git}
FRANKENPHP_REF=${FRANKENPHP_REF:-}
MAKE_JOBS=${MAKE_JOBS:-0}
NGINX_BIN=${NGINX_BIN:-/usr/sbin/nginx}
BUILD_PARENT=${OPCACHE_STATIC_CACHE_BENCHMARK_BUILD_DIR:-${TMPDIR:-/tmp}/opcache-static-cache-benchmark-devcontainer}
OUTPUT_DIR=${OUTPUT_DIR:-${SCRIPT_DIR}/results}
SKIP_REBUILD=${SKIP_REBUILD:-0}
NTS_BUILD_DIR=
ZTS_BUILD_DIR=
APCU_NTS_DIR=
APCU_ZTS_DIR=
FRANKENPHP_SRC=
FRANKENPHP_BIN=
PHP_SRC_CLONE=
while test "${#}" -gt 0; do
case "${1}" in
--php-repo)
REQUIRE_VALUE "${1}" "${#}"
PHP_REPO=${2}
shift 2
;;
--php-ref)
REQUIRE_VALUE "${1}" "${#}"
PHP_REF=${2}
shift 2
;;
--runtime)
REQUIRE_VALUE "${1}" "${#}"
BENCHMARK_RUNTIME=${2}
shift 2
;;
--iterations)
REQUIRE_VALUE "${1}" "${#}"
ITERATIONS=${2}
shift 2
;;
--runner)
REQUIRE_VALUE "${1}" "${#}"
RUNNER=${2}
shift 2
;;
--scenario)
REQUIRE_VALUE "${1}" "${#}"
SCENARIO=${2}
shift 2
;;
--warmup)
REQUIRE_VALUE "${1}" "${#}"
WARMUP=${2}
shift 2
;;
--operations)
REQUIRE_VALUE "${1}" "${#}"
OPERATIONS=${2}
shift 2
;;
--concurrency)
REQUIRE_VALUE "${1}" "${#}"
CONCURRENCY=${2}
shift 2
;;
--key-mode)
REQUIRE_VALUE "${1}" "${#}"
KEY_MODE=${2}
shift 2
;;
--key-space)
REQUIRE_VALUE "${1}" "${#}"
KEY_SPACE=${2}
shift 2
;;
--jit)
REQUIRE_VALUE "${1}" "${#}"
JIT=${2}
shift 2
;;
--threads)
REQUIRE_VALUE "${1}" "${#}"
FRANKENPHP_THREADS=${2}
shift 2
;;
--frankenphp-repo)
REQUIRE_VALUE "${1}" "${#}"
FRANKENPHP_REPO=${2}
shift 2
;;
--frankenphp-ref)
REQUIRE_VALUE "${1}" "${#}"
FRANKENPHP_REF=${2}
shift 2
;;
--make-jobs)
REQUIRE_VALUE "${1}" "${#}"
MAKE_JOBS=${2}
shift 2
;;
--skip-rebuild)
SKIP_REBUILD=1
shift
;;
--build-dir)
REQUIRE_VALUE "${1}" "${#}"
BUILD_PARENT=${2}
shift 2
;;
--output-dir)
REQUIRE_VALUE "${1}" "${#}"
OUTPUT_DIR=${2}
shift 2
;;
-h|--help)
USAGE
exit 0
;;
*)
echo "Unknown argument: ${1}" >&2
USAGE >&2
exit 1
;;
esac
done
case "${BENCHMARK_RUNTIME}" in
all|fpm|frankenphp)
;;
*)
echo "unsupported runtime: ${BENCHMARK_RUNTIME}" >&2
exit 1
;;
esac
if test "${MAKE_JOBS}" = 0; then
MAKE_JOBS=$(getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)
fi
CLEANUP() {
EXIT_CODE=${?}
trap - 0 2 15
exit "${EXIT_CODE}"
}
trap CLEANUP 0 2 15
PREPARE_BUILD_ROOT() {
if test "${SKIP_REBUILD}" -eq 0; then
rm -rf "${BUILD_PARENT}"
fi
NTS_BUILD_DIR=${BUILD_PARENT}/php-nts
ZTS_BUILD_DIR=${BUILD_PARENT}/php-zts
APCU_NTS_DIR=${BUILD_PARENT}/apcu-nts
APCU_ZTS_DIR=${BUILD_PARENT}/apcu-zts
FRANKENPHP_SRC=${BUILD_PARENT}/frankenphp-src
FRANKENPHP_BIN=${BUILD_PARENT}/frankenphp
PHP_SRC_CLONE=${BUILD_PARENT}/php-src-source
mkdir -p "${NTS_BUILD_DIR}" "${ZTS_BUILD_DIR}"
}
PREPARE_SOURCE_ROOT() {
if test -d "${PHP_REPO}"; then
SOURCE_ROOT=$(CDPATH= cd "${PHP_REPO}" && pwd)
return 0
fi
REQUIRE_COMMAND git
if test -n "${PHP_REF}"; then
if git clone --depth 1 --branch "${PHP_REF}" "${PHP_REPO}" "${PHP_SRC_CLONE}"; then
SOURCE_ROOT=${PHP_SRC_CLONE}
return 0
fi
git clone --depth 1 "${PHP_REPO}" "${PHP_SRC_CLONE}"
(cd "${PHP_SRC_CLONE}" && git fetch --depth 1 origin "${PHP_REF}" && git checkout --detach FETCH_HEAD)
else
git clone --depth 1 "${PHP_REPO}" "${PHP_SRC_CLONE}"
fi
SOURCE_ROOT=${PHP_SRC_CLONE}
}
ENSURE_NTS_RUNTIME() {
if test "${SKIP_REBUILD}" -ne 0; then
REQUIRE_EXECUTABLE_PATH "${NTS_BUILD_DIR}/sapi/fpm/php-fpm" 'NTS php-fpm binary'
REQUIRE_FILE_PATH "${APCU_NTS_DIR}/apcu.so" 'NTS APCu module'
return 0
fi
BUILD_NTS_RUNTIME
}
ENSURE_ZTS_RUNTIME() {
if test "${SKIP_REBUILD}" -ne 0; then
REQUIRE_EXECUTABLE_PATH "${ZTS_BUILD_DIR}/sapi/cli/php" 'ZTS PHP CLI binary'
REQUIRE_FILE_PATH "${APCU_ZTS_DIR}/apcu.so" 'ZTS APCu module'
return 0
fi
BUILD_ZTS_RUNTIME
}
ENSURE_FRANKENPHP() {
if test "${SKIP_REBUILD}" -ne 0; then
REQUIRE_EXECUTABLE_PATH "${FRANKENPHP_BIN}" 'FrankenPHP binary'
return 0
fi
BUILD_FRANKENPHP
}
BUILD_NTS_RUNTIME() {
REQUIRE_COMMAND make
REQUIRE_COMMAND curl
REQUIRE_COMMAND git
cd "${NTS_BUILD_DIR}"
"${SOURCE_ROOT}/configure" \
--disable-all \
--enable-cli \
--enable-fpm \
--enable-pcntl \
--enable-session \
--enable-deepclone
make -j"${MAKE_JOBS}" sapi/cli/php sapi/fpm/php-fpm scripts/phpize scripts/php-config
"${SCRIPT_DIR}/scripts/build_apcu.sh" \
"${NTS_BUILD_DIR}/scripts/phpize" \
"${NTS_BUILD_DIR}/scripts/php-config" \
"${APCU_NTS_DIR}" >/dev/null
}
BUILD_ZTS_RUNTIME() {
REQUIRE_COMMAND make
REQUIRE_COMMAND curl
REQUIRE_COMMAND git
REQUIRE_COMMAND go
cd "${ZTS_BUILD_DIR}"
"${SOURCE_ROOT}/configure" \
--disable-all \
--enable-cli \
--enable-pcntl \
--enable-session \
--enable-embed=static \
--enable-zend-max-execution-timers \
--enable-zts \
--disable-zend-signals \
--enable-deepclone
make -j"${MAKE_JOBS}" libphp.la sapi/cli/php scripts/phpize scripts/php-config
"${SCRIPT_DIR}/scripts/build_apcu.sh" \
"${ZTS_BUILD_DIR}/scripts/phpize" \
"${ZTS_BUILD_DIR}/scripts/php-config" \
"${APCU_ZTS_DIR}" >/dev/null
}
BUILD_FRANKENPHP() {
PHP_CONFIG=${ZTS_BUILD_DIR}/scripts/php-config
PHP_EMBED_LIB=${ZTS_BUILD_DIR}/.libs/$(${PHP_CONFIG} --lib-embed)
PHP_EXTRA_LIBS=$(printf '%s\n' "$(${PHP_CONFIG} --libs)" | sed -E 's/(^|[[:space:]])-lphp[^[:space:]]*//g')
PHP_ZTS_CFLAGS='-DZTS -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DZEND_MAX_EXECUTION_TIMERS -DHAVE_CONFIG_H'
if test -d "${FRANKENPHP_REPO}"; then
cp -R "${FRANKENPHP_REPO}" "${FRANKENPHP_SRC}"
if test -n "${FRANKENPHP_REF}"; then
git -C "${FRANKENPHP_SRC}" checkout "${FRANKENPHP_REF}"
fi
else
if test -n "${FRANKENPHP_REF}"; then
if ! git clone --depth 1 --branch "${FRANKENPHP_REF}" "${FRANKENPHP_REPO}" "${FRANKENPHP_SRC}"; then
git clone --depth 1 "${FRANKENPHP_REPO}" "${FRANKENPHP_SRC}"
(cd "${FRANKENPHP_SRC}" && git fetch --depth 1 origin "${FRANKENPHP_REF}" && git checkout --detach FETCH_HEAD)
fi
else
git clone --depth 1 "${FRANKENPHP_REPO}" "${FRANKENPHP_SRC}"
fi
fi
grep -RIl --include='*.go' '#cgo .*LDFLAGS:.*-lphp' "${FRANKENPHP_SRC}" \
| xargs -r sed -i 's/ -lphp//g'
# OPcache Static Cache is opt-in per SAPI. FrankenPHP registers its own SAPI
# module (frankenphp_sapi_module), not the embed SAPI, so it must opt in
# itself for the cache to be available. Inject the opt-in into its startup.
FRANKENPHP_C=$(grep -RIl --include='*.c' 'frankenphp_startup(sapi_module_struct' "${FRANKENPHP_SRC}" | head -1)
if test -n "${FRANKENPHP_C}" \
&& ! grep -q 'zend_opcache_static_cache_opt_in' "${FRANKENPHP_C}"; then
if ! grep -q 'zend_static_cache.h' "${FRANKENPHP_C}"; then
sed -i '0,/^#include /s//#include "ext\\/opcache\\/zend_static_cache.h"\\n&/' "${FRANKENPHP_C}"
fi
sed -i 's/^\(\([[:space:]]*\)return php_module_startup(sapi_module, &frankenphp_module);\)/\2zend_opcache_static_cache_opt_in();\n\1/' "${FRANKENPHP_C}"
fi
if grep -RIn --include='*.go' '#cgo .*LDFLAGS:.*-lphp' "${FRANKENPHP_SRC}"; then
echo 'FrankenPHP still references -lphp in cgo LDFLAGS after compatibility patch' >&2
exit 1
fi
if test ! -f "${PHP_EMBED_LIB}"; then
echo "PHP embed library not found: ${PHP_EMBED_LIB}" >&2
exit 1
fi
if printf '%s\n' "${PHP_EXTRA_LIBS}" | grep -Eq '(^|[[:space:]])-lphp[^[:space:]]*([[:space:]]|$)'; then
echo "php-config --libs still contains libphp after sanitization: ${PHP_EXTRA_LIBS}" >&2
exit 1
fi
cd "${FRANKENPHP_SRC}/caddy/frankenphp"
CGO_ENABLED=1 \
GOFLAGS="${GOFLAGS:-} -tags=nowatcher,nobadger,nobrotli,nomysql,nopgx" \
CGO_CFLAGS="$("${PHP_CONFIG}" --includes) ${PHP_ZTS_CFLAGS} -I${SOURCE_ROOT} -I${SOURCE_ROOT}/main -I${SOURCE_ROOT}/TSRM -I${SOURCE_ROOT}/Zend -I${SOURCE_ROOT}/ext -I${SOURCE_ROOT}/ext/date/lib -I${ZTS_BUILD_DIR} -I${ZTS_BUILD_DIR}/main -I${ZTS_BUILD_DIR}/Zend" \
CGO_LDFLAGS="${PHP_EMBED_LIB} ${PHP_EXTRA_LIBS} -Wl,--export-dynamic" \
go build -o "${FRANKENPHP_BIN}"
}
RUN_FPM_BENCHMARK() {
if test ! -x "${NGINX_BIN}"; then
echo "nginx binary is not executable: ${NGINX_BIN}; set NGINX_BIN=/path/to/nginx" >&2
exit 1
fi
ENSURE_NTS_RUNTIME
cd "${SCRIPT_DIR}"
set -- \
--php-fpm "${NTS_BUILD_DIR}/sapi/fpm/php-fpm" \
--php-cli "${NTS_BUILD_DIR}/sapi/cli/php" \
--apcu-so "${APCU_NTS_DIR}/apcu.so" \
--nginx-bin "${NGINX_BIN}" \
--output-dir "${OUTPUT_DIR}" \
--jit "${JIT}"
if test -n "${ITERATIONS}"; then set -- "${@}" --iterations "${ITERATIONS}"; fi
if test -n "${WARMUP}"; then set -- "${@}" --warmup "${WARMUP}"; fi
if test -n "${OPERATIONS}"; then set -- "${@}" --operations "${OPERATIONS}"; fi
if test -n "${RUNNER}"; then set -- "${@}" --runner "${RUNNER}"; fi
if test -n "${SCENARIO}"; then set -- "${@}" --scenario "${SCENARIO}"; fi
if test -n "${CONCURRENCY}"; then set -- "${@}" --concurrency "${CONCURRENCY}"; fi
if test -n "${KEY_MODE}"; then set -- "${@}" --key-mode "${KEY_MODE}"; fi
if test -n "${KEY_SPACE}"; then set -- "${@}" --key-space "${KEY_SPACE}"; fi
"${SCRIPT_DIR}/scripts/benchmark_fpm.sh" "${@}"
}
RUN_FRANKENPHP_BENCHMARK() {
ENSURE_ZTS_RUNTIME
ENSURE_FRANKENPHP
cd "${SCRIPT_DIR}"
set -- \
--frankenphp "${FRANKENPHP_BIN}" \
--php-cli "${ZTS_BUILD_DIR}/sapi/cli/php" \
--apcu-so "${APCU_ZTS_DIR}/apcu.so" \
--threads "${FRANKENPHP_THREADS}" \
--output-dir "${OUTPUT_DIR}" \
--jit "${JIT}"
if test -n "${ITERATIONS}"; then set -- "${@}" --iterations "${ITERATIONS}"; fi
if test -n "${WARMUP}"; then set -- "${@}" --warmup "${WARMUP}"; fi
if test -n "${OPERATIONS}"; then set -- "${@}" --operations "${OPERATIONS}"; fi
if test -n "${RUNNER}"; then set -- "${@}" --runner "${RUNNER}"; fi
if test -n "${SCENARIO}"; then set -- "${@}" --scenario "${SCENARIO}"; fi
if test -n "${CONCURRENCY}"; then set -- "${@}" --concurrency "${CONCURRENCY}"; fi
if test -n "${KEY_MODE}"; then set -- "${@}" --key-mode "${KEY_MODE}"; fi
if test -n "${KEY_SPACE}"; then set -- "${@}" --key-space "${KEY_SPACE}"; fi
"${SCRIPT_DIR}/scripts/benchmark_frankenphp.sh" "${@}"
}
PREPARE_BUILD_ROOT
PREPARE_SOURCE_ROOT
sh "${SCRIPT_DIR}/scripts/install_dependencies.sh"
case "${BENCHMARK_RUNTIME}" in
all)
RUN_FRANKENPHP_BENCHMARK
RUN_FPM_BENCHMARK
;;
frankenphp)
RUN_FRANKENPHP_BENCHMARK
;;
fpm)
RUN_FPM_BENCHMARK
;;
esac