From dd057ddefe64f4d998ab9b8fddd64d3aeb04936d Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 31 Aug 2016 18:02:17 +0200 Subject: [PATCH 1/6] Updated the article about performance tuning tips --- performance.rst | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/performance.rst b/performance.rst index f6cffd5e219..aabc25bb7eb 100644 --- a/performance.rst +++ b/performance.rst @@ -12,15 +12,15 @@ application even faster. .. index:: single: Performance; Byte code cache -Use a Byte Code Cache (e.g. APC) --------------------------------- +Use a Byte Code Cache (e.g. OPcache) +------------------------------------ One of the best (and easiest) things that you should do to improve your performance is to use a "byte code cache". The idea of a byte code cache is to remove the need to constantly recompile the PHP source code. There are a number of `byte code caches`_ available, some of which are open source. As of PHP 5.5, PHP comes with `OPcache`_ built-in. For older versions, the most widely used -byte code cache is probably `APC`_ +byte code cache is `APC`_. Using a byte code cache really has no downside, and Symfony has been architected to perform really well in this type of environment. @@ -43,6 +43,25 @@ your ``php.ini`` configuration. .. index:: single: Performance; Autoloader +Configure the PHP realpath cache +-------------------------------- + +PHP uses an internal cache to store the result of mapping file paths to their +real and absolute file system paths. This increases the performance for +applications like Symfony that open many PHP files, specially on Windows +systems. + +By default PHP sets a default ``realpath_cache_size`` of ``16K`` which is too +low for Symfony. Consider updating this value at least to ``4096K``. In +addition, cached paths are only stored for ``120`` seconds. Consider updating +this value too using the ``realpath_cache_ttl`` option: + +.. code-block:: ini + + ; php.ini + realpath_cache_size=4096K + realpath_cache_ttl=600 + Use Composer's Class Map Functionality -------------------------------------- @@ -52,18 +71,25 @@ automatically find any new classes that you've placed in the registered directories. Unfortunately, this comes at a cost, as the loader iterates over all configured -namespaces to find a particular file, making ``file_exists`` calls until it +namespaces to find a particular file, making ``file_exists()`` calls until it finally finds the file it's looking for. -The simplest solution is to tell Composer to build a "class map" (i.e. a -big array of the locations of all the classes). This can be done from the -command line, and might become part of your deploy process: +The simplest solution is to tell Composer to build an optimized "class map", +which is a big array of the locations of all the classes and it's stored +in ``vendor/composer/autoload_classmap.php``. + +The class map can be generated from the command line, and might become part of +your deploy process: .. code-block:: bash - $ composer dump-autoload --optimize + $ composer dump-autoload --optimize --no-dev --classmap-authoritative -Internally, this builds the big class map array in ``vendor/composer/autoload_classmap.php``. +The ``--optimize`` option dumps every PSR-0 and PSR-4 compatible class used in +your application. The ``--no-dev`` option excludes the classes that are only +needed in the development environment (e.g. tests). The ``--classmap-authoritative`` +option prevents Composer from scanning the file system for classes that are not +found in the class map. Caching the Autoloader with APC ------------------------------- From 6409fc55fd325e3f05860f407fb9559e3c700ca1 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 1 Sep 2016 13:37:29 +0200 Subject: [PATCH 2/6] Fixes and improvements --- performance.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/performance.rst b/performance.rst index aabc25bb7eb..58883741f2b 100644 --- a/performance.rst +++ b/performance.rst @@ -54,13 +54,13 @@ systems. By default PHP sets a default ``realpath_cache_size`` of ``16K`` which is too low for Symfony. Consider updating this value at least to ``4096K``. In addition, cached paths are only stored for ``120`` seconds. Consider updating -this value too using the ``realpath_cache_ttl`` option: +this value too using the ``realpath_cache_ttl`` option in your ``php.ini`` file: .. code-block:: ini - ; php.ini - realpath_cache_size=4096K - realpath_cache_ttl=600 + ; php.ini + realpath_cache_size=4096K + realpath_cache_ttl=600 Use Composer's Class Map Functionality -------------------------------------- From fab7a0a2b1dacb6c2f46cca72c0c4be64042f0a0 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 1 Sep 2016 13:38:34 +0200 Subject: [PATCH 3/6] Minor reword --- performance.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/performance.rst b/performance.rst index 58883741f2b..aea8265cc55 100644 --- a/performance.rst +++ b/performance.rst @@ -51,10 +51,10 @@ real and absolute file system paths. This increases the performance for applications like Symfony that open many PHP files, specially on Windows systems. -By default PHP sets a default ``realpath_cache_size`` of ``16K`` which is too -low for Symfony. Consider updating this value at least to ``4096K``. In -addition, cached paths are only stored for ``120`` seconds. Consider updating -this value too using the ``realpath_cache_ttl`` option in your ``php.ini`` file: +By default PHP sets a ``realpath_cache_size`` of ``16K`` which is too low for +Symfony. Consider updating this value at least to ``4096K``. In addition, cached +paths are only stored for ``120`` seconds by default. Consider updating this +value too using the ``realpath_cache_ttl`` option in your ``php.ini`` file: .. code-block:: ini From c1b64cc9278b17532fe6693a1964a54e41357c6c Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 29 Sep 2016 16:58:45 +0200 Subject: [PATCH 4/6] Further rewordings and improvements --- performance.rst | 53 ++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/performance.rst b/performance.rst index aea8265cc55..b8f0fcf522d 100644 --- a/performance.rst +++ b/performance.rst @@ -6,8 +6,7 @@ Performance Symfony is fast, right out of the box. Of course, if you really need speed, there are many ways that you can make Symfony even faster. In this chapter, -you'll explore many of the most common and powerful ways to make your Symfony -application even faster. +you'll explore some of the ways to make your Symfony application even faster. .. index:: single: Performance; Byte code cache @@ -15,12 +14,13 @@ application even faster. Use a Byte Code Cache (e.g. OPcache) ------------------------------------ -One of the best (and easiest) things that you should do to improve your performance -is to use a "byte code cache". The idea of a byte code cache is to remove -the need to constantly recompile the PHP source code. There are a number of -`byte code caches`_ available, some of which are open source. As of PHP 5.5, -PHP comes with `OPcache`_ built-in. For older versions, the most widely used -byte code cache is `APC`_. +The first thing that you should do to improve your performance is to use a +"byte code cache". These caches store the compiled PHP files to avoid having +to recompile them for every request. + +There are a number of `byte code caches`_ available, some of which are open +source. As of PHP 5.5, PHP comes with `OPcache`_ built-in. For older versions, +the most widely used byte code cache is `APC`_. Using a byte code cache really has no downside, and Symfony has been architected to perform really well in this type of environment. @@ -28,22 +28,26 @@ to perform really well in this type of environment. Further Optimizations ~~~~~~~~~~~~~~~~~~~~~ -Byte code caches usually monitor the source files for changes. This ensures -that if the source of a file changes, the byte code is recompiled automatically. -This is really convenient, but obviously adds overhead. +Most byte code caches monitor the source files for changes. This ensures that if +the source of a file changes, the byte code is recompiled automatically. +This is really convenient, but it adds overhead. For this reason, some byte code caches offer an option to disable these checks. -Obviously, when disabling these checks, it will be up to the server admin -to ensure that the cache is cleared whenever any source files change. Otherwise, -the updates you've made won't be seen. +For example, to disable these checks in APC, simply add ``apc.stat=0`` to your +``php.ini`` configuration. + +When disabling these checks, it will be up to the server administrators to +ensure that the cache is cleared whenever any source files change. Otherwise, +the updates you've made in the application won't be seen. -For example, to disable these checks in APC, simply add ``apc.stat=0`` to -your ``php.ini`` configuration. +For the same reasons, the byte code cache must also be cleared when deploying +the application (for example by calling ``apc_clear_cache()`` PHP function when +using APC and ``opcache_reset()`` when using OPCache). .. index:: single: Performance; Autoloader -Configure the PHP realpath cache +Configure the PHP realpath Cache -------------------------------- PHP uses an internal cache to store the result of mapping file paths to their @@ -54,7 +58,7 @@ systems. By default PHP sets a ``realpath_cache_size`` of ``16K`` which is too low for Symfony. Consider updating this value at least to ``4096K``. In addition, cached paths are only stored for ``120`` seconds by default. Consider updating this -value too using the ``realpath_cache_ttl`` option in your ``php.ini`` file: +value too using the ``realpath_cache_ttl`` option: .. code-block:: ini @@ -85,11 +89,14 @@ your deploy process: $ composer dump-autoload --optimize --no-dev --classmap-authoritative -The ``--optimize`` option dumps every PSR-0 and PSR-4 compatible class used in -your application. The ``--no-dev`` option excludes the classes that are only -needed in the development environment (e.g. tests). The ``--classmap-authoritative`` -option prevents Composer from scanning the file system for classes that are not -found in the class map. +``--optimize`` + Dumps every PSR-0 and PSR-4 compatible class used in your application. +``--no-dev`` + Excludes the classes that are only needed in the development environment + (e.g. tests). +``--classmap-authoritative`` + Prevents Composer from scanning the file system for classes that are not + found in the class map. Caching the Autoloader with APC ------------------------------- From b9d94b8c811f0d34fc0d6ffe2845f66669fe91fc Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 9 Dec 2016 10:13:24 +0100 Subject: [PATCH 5/6] Added a mention to opcache.max_accelerated_files --- performance.rst | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/performance.rst b/performance.rst index b8f0fcf522d..ef256b4e6c3 100644 --- a/performance.rst +++ b/performance.rst @@ -25,8 +25,8 @@ the most widely used byte code cache is `APC`_. Using a byte code cache really has no downside, and Symfony has been architected to perform really well in this type of environment. -Further Optimizations -~~~~~~~~~~~~~~~~~~~~~ +Monitoring Source File Changes +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Most byte code caches monitor the source files for changes. This ensures that if the source of a file changes, the byte code is recompiled automatically. @@ -44,6 +44,18 @@ For the same reasons, the byte code cache must also be cleared when deploying the application (for example by calling ``apc_clear_cache()`` PHP function when using APC and ``opcache_reset()`` when using OPCache). +Optimizing all the Files Used by Symfony +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +By default, PHP's OPcache saves up to 2,000 files in the byte code cache. This +number is too low for the typical Symfony applications, so you should set a +higher limit with the `opcache.max_accelerated_files`_ configuration option: + +.. code-block:: ini + + ; php.ini + opcache.max_accelerated_files = 20000 + .. index:: single: Performance; Autoloader @@ -180,6 +192,7 @@ Learn more .. _`byte code caches`: https://en.wikipedia.org/wiki/List_of_PHP_accelerators .. _`OPcache`: http://php.net/manual/en/book.opcache.php +.. _`opcache.max_accelerated_files`: http://php.net/manual/en/opcache.configuration.php#ini.opcache.max-accelerated-files .. _`APC`: http://php.net/manual/en/book.apc.php .. _`autoload.php`: https://github.com/symfony/symfony-standard/blob/master/app/autoload.php .. _`bootstrap file`: https://github.com/sensiolabs/SensioDistributionBundle/blob/master/Composer/ScriptHandler.php From e2abd22966cf59cbfbf67d2810a9712d2cbddcd1 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 9 Dec 2016 10:16:50 +0100 Subject: [PATCH 6/6] Added a note about the different caches for CLI and the server --- performance.rst | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/performance.rst b/performance.rst index ef256b4e6c3..db7e9f45e1d 100644 --- a/performance.rst +++ b/performance.rst @@ -42,7 +42,14 @@ the updates you've made in the application won't be seen. For the same reasons, the byte code cache must also be cleared when deploying the application (for example by calling ``apc_clear_cache()`` PHP function when -using APC and ``opcache_reset()`` when using OPCache). +using APC and ``opcache_reset()`` when using OPcache). + +.. note:: + + In PHP, the CLI and the web processes don't share the same OPcache. This + means that you cannot clear the web server OPcache by executing some command + in your terminal. You either need to restart the web server or call to the + ``apc_clear_cache()`` and ``opcache_reset()`` functions via the web server. Optimizing all the Files Used by Symfony ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~