Skip to content

Commit

Permalink
Merge pull request #182 from bryanlatten/file-opcache
Browse files Browse the repository at this point in the history
File-based opcache: ENV variable
  • Loading branch information
bryanlatten committed Nov 25, 2020
2 parents d9d1600 + a9408c4 commit bd2a203
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Dockerfile-7.0
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM behance/docker-nginx:8.8
FROM behance/docker-nginx:8.9
LABEL maintainers="Behance Team <dev-behance@adobe.com>"

# Set TERM to suppress warning messages.
Expand All @@ -23,6 +23,7 @@ ENV CONF_PHPFPM=/etc/php/7.0/fpm/php-fpm.conf \
PHP_OPCACHE_MEMORY_CONSUMPTION=128 \
PHP_OPCACHE_INTERNED_STRINGS_BUFFER=16 \
PHP_OPCACHE_MAX_WASTED_PERCENTAGE=5 \
PHP_OPCACHE_ENABLE_CLI=1 \
PHP_ENGINE_VERSION=20151012 \
CFG_APP_DEBUG=1

Expand Down
3 changes: 2 additions & 1 deletion Dockerfile-7.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM behance/docker-nginx:8.8
FROM behance/docker-nginx:8.9
LABEL maintainers="Behance Team <dev-behance@adobe.com>"

# Set TERM to suppress warning messages.
Expand All @@ -23,6 +23,7 @@ ENV CONF_PHPFPM=/etc/php/7.1/fpm/php-fpm.conf \
PHP_OPCACHE_MEMORY_CONSUMPTION=128 \
PHP_OPCACHE_INTERNED_STRINGS_BUFFER=16 \
PHP_OPCACHE_MAX_WASTED_PERCENTAGE=5 \
PHP_OPCACHE_ENABLE_CLI=1 \
PHP_ENGINE_VERSION=20160303 \
CFG_APP_DEBUG=1

Expand Down
3 changes: 2 additions & 1 deletion Dockerfile-7.2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM behance/docker-nginx:8.8
FROM behance/docker-nginx:8.9
LABEL maintainers="Behance Team <dev-behance@adobe.com>"

# Set TERM to suppress warning messages.
Expand All @@ -23,6 +23,7 @@ ENV CONF_PHPFPM=/etc/php/7.2/fpm/php-fpm.conf \
PHP_OPCACHE_MEMORY_CONSUMPTION=128 \
PHP_OPCACHE_INTERNED_STRINGS_BUFFER=16 \
PHP_OPCACHE_MAX_WASTED_PERCENTAGE=5 \
PHP_OPCACHE_ENABLE_CLI=1 \
PHP_ENGINE_VERSION=20170718 \
CFG_APP_DEBUG=1

Expand Down
3 changes: 2 additions & 1 deletion Dockerfile-7.3
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM behance/docker-nginx:8.8
FROM behance/docker-nginx:8.9
LABEL maintainers="Behance Team <dev-behance@adobe.com>"

# Set TERM to suppress warning messages.
Expand All @@ -23,6 +23,7 @@ ENV CONF_PHPFPM=/etc/php/7.3/fpm/php-fpm.conf \
PHP_OPCACHE_MEMORY_CONSUMPTION=128 \
PHP_OPCACHE_INTERNED_STRINGS_BUFFER=16 \
PHP_OPCACHE_MAX_WASTED_PERCENTAGE=5 \
PHP_OPCACHE_ENABLE_CLI=1 \
PHP_ENGINE_VERSION=20180731 \
CFG_APP_DEBUG=1

Expand Down
3 changes: 2 additions & 1 deletion Dockerfile-7.3-alpine
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM behance/docker-nginx:8.8-alpine
FROM behance/docker-nginx:8.9-alpine
LABEL maintainers="Behance Team <dev-behance@adobe.com>"

# Set TERM to suppress warning messages.
Expand All @@ -23,6 +23,7 @@ ENV CONF_PHPFPM=/etc/php7/php-fpm.conf \
PHP_OPCACHE_MEMORY_CONSUMPTION=128 \
PHP_OPCACHE_INTERNED_STRINGS_BUFFER=16 \
PHP_OPCACHE_MAX_WASTED_PERCENTAGE=5 \
PHP_OPCACHE_ENABLE_CLI=1 \
PHP_ENGINE_VERSION=20180731 \
CFG_APP_DEBUG=1

Expand Down
3 changes: 2 additions & 1 deletion Dockerfile-7.4
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM behance/docker-nginx:8.8
FROM behance/docker-nginx:8.9
LABEL maintainers="Behance Team <dev-behance@adobe.com>"

# Set TERM to suppress warning messages.
Expand All @@ -23,6 +23,7 @@ ENV CONF_PHPFPM=/etc/php/7.4/fpm/php-fpm.conf \
PHP_OPCACHE_MEMORY_CONSUMPTION=128 \
PHP_OPCACHE_INTERNED_STRINGS_BUFFER=16 \
PHP_OPCACHE_MAX_WASTED_PERCENTAGE=5 \
PHP_OPCACHE_ENABLE_CLI=1 \
PHP_ENGINE_VERSION=20190902 \
CFG_APP_DEBUG=1

Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,11 @@ PHP_FPM_MIN_SPARE_SERVERS | PHP_FPM_MIN_SPARE_SERVERS=10 | 5 | [docs](http://php
PHP_OPCACHE_MEMORY_CONSUMPTION | PHP_OPCACHE_MEMORY_CONSUMPTION=512 | 128 | [docs](http://php.net/manual/en/opcache.configuration.php#ini.opcache.memory-consumption)
PHP_OPCACHE_MAX_WASTED_PERCENTAGE | PHP_OPCACHE_MAX_WASTED_PERCENTAGE=10 | 5 | [docs](http://php.net/manual/en/opcache.configuration.php#ini.opcache.max-wasted-percentage)
PHP_OPCACHE_INTERNED_STRINGS_BUFFER | PHP_OPCACHE_INTERNED_STRINGS_BUFFER=64 | 16 | [docs](http://php.net/manual/en/opcache.configuration.php#ini.opcache.interned-strings-buffer)
PHP_FPM_LOG_LIMIT | PHP_FPM_LOG_LIMIT=4096 | 1024 | PHP 7.3-only, allows configurable stdout message max length [docs](https://www.php.net/manual/en/install.fpm.configuration.php)
PHP_FPM_LOG_BUFFERING | PHP_FPM_LOG_BUFFERING=no | yes | Experimental, PHP 7.3-only [docs](https://www.php.net/manual/en/install.fpm.configuration.php)
PHP_OPCACHE_FILE_CACHE | PHP_OPCACHE_FILE_CACHE=/tmp | - | [docs](https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.file-cache)
PHP_OPCACHE_ENABLE_CLI | PHP_OPCACHE_ENABLE_CLI=0 | 1 | [docs](https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.enable-cli)
PHP_OPCACHE_PRELOAD | PHP_OPCACHE_PRELOAD=/etc/php/preload.php | - | [docs](https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.preload)
PHP_FPM_LOG_LIMIT | PHP_FPM_LOG_LIMIT=4096 | 1024 | PHP 7.3+ only, allows configurable stdout message max length [docs](https://www.php.net/manual/en/install.fpm.configuration.php)
PHP_FPM_LOG_BUFFERING | PHP_FPM_LOG_BUFFERING=no | yes | PHP 7.3+ only [docs](https://www.php.net/manual/en/install.fpm.configuration.php)

### Testing
---
Expand Down
21 changes: 21 additions & 0 deletions container/root/etc/php/7.0/mods-available/opcache.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,26 @@ opcache.interned_strings_buffer=${PHP_OPCACHE_INTERNED_STRINGS_BUFFER}
opcache.fast_shutdown=0
opcache.optimization_level=0xfffffff0

; Ignored when validate_timestamps is disabled
opcache.revalidate_freq=0

; Since container deployments are atomic, there is no need to monitor for file updates
; https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.file_update_protection
; https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.validate-timestamps
opcache.file_update_protection=${CFG_APP_DEBUG}
opcache.validate_timestamps=${CFG_APP_DEBUG}

; Backup memory cache to file system
; https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.file-cache
opcache.file_cache=${PHP_OPCACHE_FILE_CACHE}

; https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.enable-cli
opcache.enable_cli=${PHP_OPCACHE_ENABLE_CLI}

; https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.preload-user
opcache.preload_user=${NOT_ROOT_USER}

; https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.preload
opcache.preload=${PHP_OPCACHE_PRELOAD}


70 changes: 70 additions & 0 deletions container/root/etc/php/preload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Sends all eligible files into PHP's opcode cache before server startup
*/
$LOGPREFIX = '[opcache]';
$MEMORY_USAGE_WARNING_PERCENTAGE = 85;
$app_root = getenv('APP_ROOT');

if (!$app_root) {
echo("${LOGPREFIX} cannot preload, APP_ROOT is empty");
return;
}

echo "${LOGPREFIX} preloading from ${app_root}\n";
$start_time = microtime(true);

[ $count, $success, $failure ] = compileFiles($app_root);

$end_time = microtime(true);
$elapsed = ($end_time - $start_time);
$elapsed = round($elapsed, 3);

echo "${LOGPREFIX} preloaded: processed ${count} files - ${success} successful / ${failure} failed, in ${elapsed} seconds\n";

$opcache_status = opcache_get_status(false);
$memory_usage = $opcache_status['memory_usage'] ?? [];

if (empty($memory_usage)) {
echo "${LOGPREFIX} error: no memory reported\n";
return;
}

$free_memory = $memory_usage['free_memory'] ?? 0;
$wasted_memory = $memory_usage['wasted_memory'] ?? 0;
$used_memory = $memory_usage['used_memory'] ?? 0;

$memory_total = $wasted_memory + $free_memory + $used_memory;
$memory_consumed = $used_memory + $wasted_memory;
$percentage_used = ($memory_consumed / $memory_total) * 100;
$percentage_used = round($percentage_used, 2);

if ($percentage_used >= $MEMORY_USAGE_WARNING_PERCENTAGE) {
echo "${LOGPREFIX} warning: ${percentage_used}% memory usage, consider raising PHP_OPCACHE_MEMORY_CONSUMPTION env variable\n";
} else {
echo "${LOGPREFIX} complete: ${percentage_used}% memory utilized\n";
}

function compileFiles($input) {
$directory = new RecursiveDirectoryIterator($input);
$fullTree = new RecursiveIteratorIterator($directory);

$phpunit_filter = '/^(?:(?!test).)*\.php$/i'; // Removes PHPUnit-style filenames (ie. calculatortest.php)

$all_files = new RegexIterator($fullTree, $phpunit_filter, RecursiveRegexIterator::GET_MATCH);
$success = 0;
$failure = 0;
$results = [];

foreach($all_files as $file) {
if (@opcache_compile_file($file[0])) {
++$success;
} else {
++$failure;
}
}

$count = $success + $failure;

return [ $count, $success, $failure ];
}
7 changes: 7 additions & 0 deletions container/root/tests/php-fpm/base.goss.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,10 @@ file:
exists: true
contains:
- '/^error_log = \/dev\/stdout/'
{{ .Env.CONF_PHPMODS }}/opcache.ini:
exists: true
contains:
- '/^opcache.file_cache=\${PHP_OPCACHE_FILE_CACHE}/'
- '/^opcache.enable_cli=\${PHP_OPCACHE_ENABLE_CLI}/'
- '/^opcache.preload_user=\${NOT_ROOT_USER}/'
- '/^opcache.preload=\${PHP_OPCACHE_PRELOAD}/'

0 comments on commit bd2a203

Please sign in to comment.