Skip to content

Commit

Permalink
Merge pull request brefphp#14 from mnapoli/opcache
Browse files Browse the repository at this point in the history
Enable opcache and use it with the file cache
  • Loading branch information
mnapoli committed Jun 8, 2018
2 parents ab8def4 + 9fe6651 commit 7be4e3d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
8 changes: 6 additions & 2 deletions bin/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ RUN ./configure \
# --enable-mbstring is included here because otherwise there's no way to get pecl to use it properly (see https://github.com/docker-library/php/issues/195)
--enable-mbstring \
--enable-mysqlnd \
--enable-opcache \
# Allows to use the opcache.file_cache option
--enable-opcache-file \
--enable-soap \
--enable-zip \
--with-curl \
Expand All @@ -46,7 +49,8 @@ RUN ./configure \
--with-gd \
--with-pdo-mysql \
# https://github.com/docker-library/php/issues/439
--with-mhash \
--without-pear
--with-mhash

RUN make -j 5

RUN make install
8 changes: 7 additions & 1 deletion bin/php/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ docker build --build-arg PHP_VERSION=$PHP_VERSION_GIT_BRANCH -t php-build .
container=$(docker create php-build)

docker -D cp $container:/php-src-$PHP_VERSION_GIT_BRANCH/sapi/cli/php .
mkdir -p ext
docker -D cp $container:/usr/local/lib/php/extensions/no-debug-non-zts-20170718/opcache.a ext/opcache.a
docker -D cp $container:/usr/local/lib/php/extensions/no-debug-non-zts-20170718/opcache.so ext/opcache.so
docker rm $container

tar czf $PHP_VERSION_GIT_BRANCH.tar.gz php
tar czf $PHP_VERSION_GIT_BRANCH.tar.gz php ext
rm php
rm ext/opcache.a
rm ext/opcache.so
rmdir ext
aws s3 cp $PHP_VERSION_GIT_BRANCH.tar.gz s3://bref-php/bin/ --acl public-read
rm $PHP_VERSION_GIT_BRANCH.tar.gz
7 changes: 7 additions & 0 deletions bref.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
$silly->command('hello [name]', function (string $name = 'World!', OutputInterface $output) {
$output->writeln('Hello ' . $name);
});
$silly->command('phpinfo', function (OutputInterface $output) {
ob_start();
phpinfo();
$phpinfo = ob_get_contents();
ob_clean();
$output->write($phpinfo);
});

$app = new \Bref\Application;
$app->simpleHandler(function (array $event) {
Expand Down
2 changes: 2 additions & 0 deletions src/Console/Deployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ private function generateArchive(SymfonyStyle $io, ProgressBar $progress) : void
->mustRun();
// Set correct permissions on the file
$this->fs->chmod('.bref/output/.bref/bin', 0755);
// Install our custom php.ini
$this->fs->copy(__DIR__ . '/../../template/php.ini', '.bref/output/.bref/php.ini');
$progress->advance();

$progress->setMessage('Installing `handler.js`');
Expand Down
7 changes: 6 additions & 1 deletion template/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ exports.handle = function(event, context, callback) {
fs.mkdirSync(TMP_DIRECTORY);
}

// Ensure the directory for storing the opcache file exists else PHP crashes
if (!fs.existsSync(TMP_DIRECTORY + '/opcache')) {
fs.mkdirSync(TMP_DIRECTORY + '/opcache');
}

// Execute bref.php and pass it the event as argument
let script = spawn('php', [PHP_FILE, JSON.stringify(event)]);
let script = spawn('php', ['--php-ini=.bref/php.ini', PHP_FILE, JSON.stringify(event)]);

// PHP's output is passed to the lambda's logs
script.stdout.on('data', function(data) {
Expand Down
22 changes: 22 additions & 0 deletions template/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
opcache.enable=1
opcache.enable_cli=1

# Store the opcodes into a file cache instead of memory
# Since PHP runs on lambdas with a new process each time the memory cache is lost
opcache.file_cache="/tmp/.bref/opcache"
# Disable the memory cache since it's useless
# In my tests it allows to gain 30% of response time in a classic API controller
opcache.file_cache_only=1
# Skip this check to save a it
opcache.validate_permission=0

# The code is readonly on lambdas so it never changes
opcache.validate_timestamps=0

# Set sane values, modern PHP applications have higher needs than opcache's defaults
# See https://tideways.com/profiler/blog/fine-tune-your-opcache-configuration-to-avoid-caching-suprises
opcache.memory_consumption=128
opcache.max_accelerated_files=10000

extension_dir=/var/task/.bref/bin/ext
zend_extension=opcache.so

0 comments on commit 7be4e3d

Please sign in to comment.