1
1
.. index ::
2
- single: Tests
2
+ single: Performance; Byte code cache; OPcache; APC
3
3
4
4
Performance
5
5
===========
6
6
7
- Symfony is fast, right out of the box. Of course, if you really 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, you can make it faster if you
8
+ optimize your servers and your applications as explained in the following
9
+ performance checklists .
10
10
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
+ ---------------------------
20
+
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 >`
26
+
27
+ .. _performance-install-apcu-polyfill :
28
+
29
+ Install APCu Polyfill if your Server Uses APC
30
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31
+
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.
36
+
37
+ .. _performance-autoloader-apc-cache :
38
+
39
+ Enable APC Caching for the Autoloader
40
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41
+
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.
46
+
47
+ To use it, adapt your front controller file like this::
48
+
49
+ // app.php
50
+ // ...
51
+
52
+ $loader = require_once __DIR__.'/../app/bootstrap.php.cache';
53
+
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 `.
62
+
63
+ .. note ::
64
+
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.
13
71
14
- Use a Byte Code Cache (e.g. OPcache)
15
- ------------------------------------
72
+ .. _performance-use-bootstrap-files :
16
73
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.
74
+ Use Bootstrap Files
75
+ ~~~~~~~~~~~~~~~~~~~
20
76
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 `_.
77
+ .. caution ::
24
78
25
- .. tip ::
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.
26
82
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 .
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 .
31
87
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.
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::
34
91
35
- Monitoring Source File Changes
36
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
92
+ require_once __DIR__.'/../app/bootstrap.php.cache';
37
93
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.
94
+ Note that there are two disadvantages when using a bootstrap file:
41
95
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.
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);
45
98
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.
99
+ * when debugging, one will need to place break points inside the bootstrap file.
49
100
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).
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.
53
103
54
104
.. note ::
55
105
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:
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.
59
110
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.
111
+ .. _performance-use-opcache :
65
112
66
- Optimizing all the Files Used by Symfony
67
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113
+ Use the OPcache Byte Code Cache
114
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68
115
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:
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 `_.
120
+
121
+ .. _performance-configure-opcache :
122
+
123
+ Configure OPcache for Maximum Performance
124
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125
+
126
+ The default OPcache configuration is not suited for Symfony applications, so
127
+ it's recommended to change these settings as follows:
72
128
73
129
.. code-block :: ini
74
130
75
131
; php.ini
76
- opcache.max_accelerated_files = 20000
132
+ ; maximum memory that OPcache can use to store compiled PHP files
133
+ opcache.memory_consumption =256M
77
134
78
- Configure the PHP realpath Cache
79
- --------------------------------
135
+ ; maximum number of files that can be stored in the cache
136
+ opcache.max_accelerated_files =20000
137
+
138
+ .. _performance-dont-check-timestamps :
80
139
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.
140
+ Don't Check PHP Files Timestamps
141
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
85
142
86
- Consider increasing the ``realpath_cache_size `` and ``realpath_cache_ttl ``:
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:
87
147
88
148
.. code-block :: ini
89
149
90
150
; php.ini
91
- ; 4096k is the default value in PHP 7.2
92
- realpath_cache_size =4096K
93
- realpath_cache_ttl =600
151
+ opcache.validate_timestamps =0
94
152
95
- .. index ::
96
- single: Performance; Autoloader
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:
158
+
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.
163
+
164
+ .. _performance-configure-realpath-cache :
97
165
98
- Use Composer's Class Map Functionality
99
- --------------------------------------
166
+ Configure the PHP realpath Cache
167
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
168
+
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:
173
+
174
+ .. code-block :: ini
175
+
176
+ ; php.ini
177
+ ; maximum memory allocated to store the results
178
+ realpath_cache_size =4096K
179
+
180
+ ; save the results for 10 minutes (600 seconds)
181
+ realpath_cache_ttl =600
100
182
101
- Symfony uses Composer's autoloader. This autoloader is easy to use, as it will
102
- automatically find any new classes that you've placed in the registered
103
- directories.
183
+ .. _performance-optimize-composer-autoloader :
104
184
105
- Unfortunately, this comes at a cost, as the loader iterates over all configured
106
- namespaces to find a particular file, making ``file_exists() `` calls until it
107
- finally finds the file it's looking for.
185
+ Optimize Composer Autoloader
186
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108
187
109
- The simplest solution is to tell Composer to build an optimized "class map",
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",
110
192
which is a big array of the locations of all the classes and it's stored
111
193
in ``vendor/composer/autoload_classmap.php ``.
112
194
113
- The class map can be generated from the command line, and might become part of
114
- your deploy process:
195
+ Execute this command to generate the class map ( and make it part of your
196
+ deployment process too) :
115
197
116
198
.. code-block :: bash
117
199
118
200
$ composer dump-autoload --optimize --no-dev --classmap-authoritative
119
201
120
- ``--optimize ``
121
- Dumps every PSR-0 and PSR-4 compatible class used in your application.
122
- ``--no-dev ``
123
- Excludes the classes that are only needed in the development environment
124
- (e.g. tests).
125
- ``--classmap-authoritative ``
126
- Prevents Composer from scanning the file system for classes that are not
127
- found in the class map.
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.
128
208
129
209
Learn more
130
210
----------
@@ -134,7 +214,8 @@ Learn more
134
214
135
215
.. _`byte code caches` : https://en.wikipedia.org/wiki/List_of_PHP_accelerators
136
216
.. _`OPcache` : http://php.net/manual/en/book.opcache.php
137
- .. _`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
138
219
.. _`APC` : http://php.net/manual/en/book.apc.php
139
220
.. _`APCu Polyfill component` : https://github.com/symfony/polyfill-apcu
140
221
.. _`APCu PHP functions` : http://php.net/manual/en/ref.apcu.php
0 commit comments