11..index ::
2- single:Tests
2+ single:Performance; Byte code cache; OPcache; APC
33
44Performance
55===========
66
7- Symfony is fast, right out of the box.Of course, if youreally need speed,
8- there are many ways that you can make Symfony even faster. In this article,
9- you'll explore some of the ways to make your Symfony application even faster .
7+ Symfony is fast, right out of the box.However, youcan make it faster if you
8+ optimize your servers and your applications as explained in the following
9+ performance checklists .
1010
11- ..index ::
12- single: Performance; Byte code cache
11+ Symfony Application Checklist
12+ -----------------------------
13+
14+ #.:ref: `Install APCu Polyfill if your server uses APC <performance-install-apcu-polyfill >`
15+ #.:ref: `Enable APC Caching for the Autoloader <performance-autoloader-apc-cache >`
16+ #.:ref: `Use Bootstrap Files <performance-use-bootstrap-files >`
17+
18+ Production Server Checklist
19+ ---------------------------
1320
14- Use a Byte Code Cache (e.g. OPcache)
15- ------------------------------------
21+ #.:ref: `Use the OPcache byte code cache <performance-use-opcache >`
22+ #.:ref: `Configure OPcache for maximum performance <performance-configure-opcache >`
23+ #.:ref: `Don't check PHP files timestamps <performance-dont-check-timestamps >`
24+ #.:ref: `Configure the PHP realpath Cache <performance-configure-realpath-cache >`
25+ #.:ref: `Optimize Composer Autoloader <performance-optimize-composer-autoloader >`
1626
17- The first thing that you should do to improve your performance is to use a
18- "byte code cache". These caches store the compiled PHP files to avoid having
19- to recompile them for every request.
27+ .. _performance-install-apcu-polyfill :
2028
21- There are a number of `byte code caches `_ available, some of which are open
22- source. As of PHP 5.5, PHP comes with `OPcache `_ built-in. For older versions,
23- the most widely used byte code cache is `APC `_.
29+ Install APCu Polyfill if your Server Uses APC
30+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2431
25- ..tip ::
32+ If your production server still uses the legacy APC PHP extension instead of
33+ OPcache, install the `APCu Polyfill component `_ in your application to enable
34+ compatibility with `APCu PHP functions `_ and unlock support for advanced Symfony
35+ features, such as the APCu Cache adapter.
2636
27- If your server still uses the legacy APC PHP extension, install the
28- `APCu Polyfill component `_ in your application to enable compatibility with
29- `APCu PHP functions `_ and unlock support for advanced Symfony features, such
30- as the APCu Cache adapter.
37+ .. _performance-autoloader-apc-cache :
3138
32- Using a byte code cache really has no downside, and Symfony has been designed
33- to perform really well in this type of environment.
39+ Enable APC Caching for the Autoloader
40+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3441
35- Monitoring Source File Changes
36- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42+ The class autoloading mechanism is one of the slowest parts in PHP applications
43+ that make use of lots of classes, such as Symfony. A simple way to improve its
44+ performance is to use the:class: `Symfony\\ Component\\ ClassLoader\\ ApcClassLoader `,
45+ which caches the location of each class after it's located the first time.
3746
38- Most byte code caches monitor the source files for changes. This ensures that if
39- the source of a file changes, the byte code is recompiled automatically.
40- This is really convenient, but it adds overhead.
47+ To use it, adapt your front controller file like this::
4148
42- For this reason, some byte code caches offer an option to disable these checks.
43- For example, to disable these checks in APC, simply add ``apc.stat=0 `` to your
44- ``php.ini `` configuration.
49+ // app.php
50+ // ...
4551
46- When disabling these checks, it will be up to the server administrators to
47- ensure that the cache is cleared whenever any source files change. Otherwise,
48- the updates you've made in the application won't be seen.
52+ $loader = require_once __DIR__.'/../app/bootstrap.php.cache';
4953
50- For the same reasons, the byte code cache must also be cleared when deploying
51- the application (for example by calling ``apc_clear_cache() `` PHP function when
52- using APC and ``opcache_reset() `` when using OPcache).
54+ // Change 'sf' by something unique to this app to prevent
55+ // conflicts with other applications running in the same server
56+ $loader = new ApcClassLoader('sf', $loader);
57+ $loader->register(true);
58+
59+ // ...
60+
61+ For more details, see:doc: `/components/class_loader/cache_class_loader `.
5362
5463..note ::
5564
56- In PHP, the CLI and the web processes don't share the same OPcache. This
57- means that you cannot clear the web server OPcache by executing some command
58- in your terminal. These are some of the possible solutions:
65+ When using the APC autoloader, if you add new classes, they will be found
66+ automatically and everything will work the same as before (i.e. no
67+ reason to "clear" the cache). However, if you change the location of a
68+ particular namespace or prefix, you'll need to flush your APC cache. Otherwise,
69+ the autoloader will still be looking at the old location for all classes
70+ inside that namespace.
5971
60- #. Restart the web server;
61- #. Call the:phpfunction: `apc_clear_cache ` or:phpfunction: `opcache_reset `
62- functions via the web server (i.e. by having these in a script that
63- you execute over the web);
64- #. Use the `cachetool `_ utility to control APC and OPcache from the CLI.
72+ .. _performance-use-bootstrap-files :
6573
66- Optimizing all the Files Used by Symfony
67- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74+ Use Bootstrap Files
75+ ~~~~~~~~~~~~~~~~~~~
6876
69- By default, PHP's OPcache saves up to 2,000 files in the byte code cache. This
70- number is too low for the typical Symfony application, so you should set a
71- higher limit with the `opcache.max_accelerated_files `_ configuration option:
77+ ..caution ::
7278
73- ..code-block ::ini
79+ Thanks to the optimizations introduced in PHP 7, bootstrap files are no
80+ longer necessary when running your Symfony applications with PHP 7 or a
81+ newer PHP version.
7482
75- ; php.ini
76- opcache.max_accelerated_files = 20000
83+ The Symfony Standard Edition includes a script to generate a so-called
84+ `bootstrap file `_, which is a large file containing the code of the most
85+ commonly used classes. This saves a lot of IO operations because Symfony no
86+ longer needs to look for and read those files.
7787
78- Configure the PHP realpath Cache
79- --------------------------------
88+ If you're using the Symfony Standard Edition, then you're probably already
89+ using the bootstrap file. To be sure, open your front controller (usually
90+ ``app.php ``) and check to make sure that the following line exists::
8091
81- PHP uses an internal cache to store the result of mapping file paths to their
82- real and absolute file system paths. This increases the performance for
83- applications like Symfony that open many PHP files, especially on Windows
84- systems.
92+ require_once __DIR__.'/../app/bootstrap.php.cache';
8593
86- Consider increasing the `` realpath_cache_size `` and `` realpath_cache_ttl `` :
94+ Note that there are two disadvantages when using a bootstrap file :
8795
88- ..code-block ::ini
96+ * the file needs to be regenerated whenever any of the original sources change
97+ (i.e. when you update the Symfony source or vendor libraries);
8998
90- ; php.ini
91- ; 4096k is the default value in PHP 7.2
92- realpath_cache_size =4096K
93- realpath_cache_ttl =600
99+ * when debugging, one will need to place break points inside the bootstrap file.
94100
95- ..index ::
96- single: Performance; Autoloader
101+ If you're using the Symfony Standard Edition, the bootstrap file is automatically
102+ rebuilt after updating the vendor libraries via the ``composer install `` command.
103+
104+ ..note ::
97105
98- Use Composer's Class Map Functionality
99- --------------------------------------
106+ Even when using a byte code cache, performance will improve when using a
107+ bootstrap file since there will be fewer files to monitor for changes. Of
108+ course, if this feature is disabled in the byte code cache (e.g.
109+ ``apc.stat=0 `` in APC), there is no longer a reason to use a bootstrap file.
100110
101- By default, the Symfony Standard Edition uses Composer's autoloader
102- in the `autoload.php `_ file. This autoloader is easy to use, as it will
103- automatically find any new classes that you've placed in the registered
104- directories.
111+ .. _performance-use-opcache :
105112
106- Unfortunately, this comes at a cost, as the loader iterates over all configured
107- namespaces to find a particular file, making ``file_exists() `` calls until it
108- finally finds the file it's looking for.
113+ Use the OPcache Byte Code Cache
114+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109115
110- The simplest solution is to tell Composer to build an optimized "class map",
111- which is a big array of the locations of all the classes and it's stored
112- in ``vendor/composer/autoload_classmap.php ``.
116+ OPcache stores the compiled PHP files to avoid having to recompile them for
117+ every request. There are some `byte code caches `_ available, but as of PHP
118+ 5.5, PHP comes with `OPcache `_ built-in. For older versions, the most widely
119+ used byte code cache is `APC `_.
113120
114- The class map can be generated from the command line, and might become part of
115- your deploy process:
121+ .. _performance-configure-opcache :
116122
117- ..code-block ::bash
123+ Configure OPcache for Maximum Performance
124+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
118125
119- $ composer dump-autoload --optimize --no-dev --classmap-authoritative
126+ The default OPcache configuration is not suited for Symfony applications, so
127+ it's recommended to change these settings as follows:
120128
121- ``--optimize ``
122- Dumps every PSR-0 and PSR-4 compatible class used in your application.
123- ``--no-dev ``
124- Excludes the classes that are only needed in the development environment
125- (e.g. tests).
126- ``--classmap-authoritative ``
127- Prevents Composer from scanning the file system for classes that are not
128- found in the class map.
129+ ..code-block ::ini
129130
130- Caching the Autoloader with APC
131- -------------------------------
131+ ; php.ini
132+ ; maximum memory that OPcache can use to store compiled PHP files
133+ opcache.memory_consumption =256M
132134
133- Another solution is to cache the location of each class after it's located
134- the first time. Symfony comes with a class -:class: `Symfony\\ Component\\ ClassLoader\\ ApcClassLoader ` -
135- that does exactly this. To use it, just adapt your front controller file.
136- If you're using the Standard Distribution, this code should already be available
137- as comments in this file::
135+ ; maximum number of files that can be stored in the cache
136+ opcache.max_accelerated_files =20000
138137
139- // app.php
140- // ...
138+ .. _performance-dont-check-timestamps :
141139
142- $loader = require_once __DIR__.'/../app/bootstrap.php.cache';
140+ Don't Check PHP Files Timestamps
141+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
143142
144- // Use APC for autoloading to improve performance
145- // Change 'sf2' by the prefix you want in order
146- // to prevent key conflict with another application
147- /*
148- $loader = new ApcClassLoader('sf2', $loader);
149- $loader->register(true);
150- */
143+ In production servers, PHP files should never change, unless a new application
144+ version is deployed. However, by default OPcache checks if cached files have
145+ changed their contents since they were cached. This check introduces some
146+ overhead that can be avoided as follows:
151147
152- // ...
148+ .. code-block :: ini
153149
154- For more details, see:doc: `/components/class_loader/cache_class_loader `.
150+ ; php.ini
151+ opcache.validate_timestamps =0
155152
156- ..note ::
153+ After each deploy, you must empty and regenerate the cache of OPcache. Otherwise
154+ you won't see the updates made in the application. Given than in PHP, the CLI
155+ and the web processes don't share the same OPcache, you cannot clear the web
156+ server OPcache by executing some command in your terminal. These are some of the
157+ possible solutions:
157158
158- When using the APC autoloader, if you add new classes, they will be found
159- automatically and everything will work the same as before (i.e. no
160- reason to "clear" the cache). However, if you change the location of a
161- particular namespace or prefix, you'll need to flush your APC cache. Otherwise,
162- the autoloader will still be looking at the old location for all classes
163- inside that namespace.
159+ 1. Restart the web server;
160+ 2. Call the ``apc_clear_cache() `` or ``opcache_reset() `` functions via the
161+ web server (i.e. by having these in a script that you execute over the web);
162+ 3. Use the `cachetool `_ utility to control APC and OPcache from the CLI.
164163
165- ..index ::
166- single: Performance; Bootstrap files
164+ .. _performance-configure-realpath-cache :
167165
168- Use Bootstrap Files
169- -------------------
166+ Configure the PHP realpath Cache
167+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
170168
171- To ensure optimal flexibility and code reuse, Symfony applications leverage
172- a variety of classes and 3rd party components. But loading all of these classes
173- from separate files on each request can result in some overhead. To reduce
174- this overhead, the Symfony Standard Edition provides a script to generate
175- a so-called `bootstrap file `_, consisting of multiple classes definitions
176- in a single file. By including this file (which contains a copy of many of
177- the core classes), Symfony no longer needs to include any of the source files
178- containing those classes. This will reduce disc IO quite a bit.
169+ When a relative path is transformed into its real and absolute path, PHP
170+ caches the result to improve performance. The default config of this cache
171+ is not suited for applications that open many PHP files, such as Symfony.
172+ It's recommended to change these settings as follows:
179173
180- If you're using the Symfony Standard Edition, then you're probably already
181- using the bootstrap file. To be sure, open your front controller (usually
182- ``app.php ``) and check to make sure that the following line exists::
174+ ..code-block ::ini
183175
184- require_once __DIR__.'/../app/bootstrap.php.cache';
176+ ; php.ini
177+ ; maximum memory allocated to store the results
178+ realpath_cache_size =4096K
185179
186- Note that there are two disadvantages when using a bootstrap file:
180+ ; save the results for 10 minutes (600 seconds)
181+ realpath_cache_ttl =600
187182
188- * the file needs to be regenerated whenever any of the original sources change
189- (i.e. when you update the Symfony source or vendor libraries);
183+ .. _performance-optimize-composer-autoloader :
190184
191- * when debugging, one will need to place break points inside the bootstrap file.
185+ Optimize Composer Autoloader
186+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
192187
193- If you're using the Symfony Standard Edition, the bootstrap file is automatically
194- rebuilt after updating the vendor libraries via the ``composer install `` command.
188+ The class loader used while developing the application is optimized to find
189+ new and changed classes. In production servers, PHP files should never change,
190+ unless a new application version is deployed. That's why you can optimize
191+ Composer's autoloader to scan the entire application once and build a "class map",
192+ which is a big array of the locations of all the classes and it's stored
193+ in ``vendor/composer/autoload_classmap.php ``.
194+
195+ Execute this command to generate the class map (and make it part of your
196+ deployment process too):
195197
196- Bootstrap Files and Byte Code Caches
197- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
198+ ..code-block ::bash
199+
200+ $ composer dump-autoload --optimize --no-dev --classmap-authoritative
198201
199- Even when using a byte code cache, performance will improve when using a bootstrap
200- file since there will be fewer files to monitor for changes. Of course, if this
201- feature is disabled in the byte code cache (e.g. ``apc.stat=0 `` in APC), there
202- is no longer a reason to use a bootstrap file.
202+ * ``--optimize `` dumps every PSR-0 and PSR-4 compatible class used in your
203+ application;
204+ * ``--no-dev `` excludes the classes that are only needed in the development
205+ environment (e.g. tests);
206+ * ``--classmap-authoritative `` prevents Composer from scanning the file
207+ system for classes that are not found in the class map.
203208
204209Learn more
205210----------
@@ -209,10 +214,9 @@ Learn more
209214
210215.. _`byte code caches` :https://en.wikipedia.org/wiki/List_of_PHP_accelerators
211216.. _`OPcache` :http://php.net/manual/en/book.opcache.php
212- .. _`opcache.max_accelerated_files` :http://php.net/manual/en/opcache.configuration.php#ini.opcache.max-accelerated-files
217+ .. _`bootstrap file` :https://github.com/sensiolabs/SensioDistributionBundle/blob/master/Composer/ScriptHandler.php
218+ .. _`Composer's autoloader optimization` :https://getcomposer.org/doc/articles/autoloader-optimization.md
213219.. _`APC` :http://php.net/manual/en/book.apc.php
214220.. _`APCu Polyfill component` :https://github.com/symfony/polyfill-apcu
215221.. _`APCu PHP functions` :http://php.net/manual/en/ref.apcu.php
216- .. _`autoload.php` :https://github.com/symfony/symfony-standard/blob/master/app/autoload.php
217- .. _`bootstrap file` :https://github.com/sensiolabs/SensioDistributionBundle/blob/master/Composer/ScriptHandler.php
218222.. _`cachetool` :https://github.com/gordalina/cachetool