Performance (Symfony Docs) (original) (raw)
Symfony is fast, right out of the box. However, you can make it faster if you optimize your servers and your applications as explained in the following performance checklists.
Performance Checklists
Use these checklists to verify that your application and server are configured for maximum performance:
- Symfony Application Checklist:
- Production Server Checklist:
Install APCu Polyfill if your Server Uses APC
If your production server still uses the legacy APC PHP extension instead of OPcache, install the APCu Polyfill component in your application to enable compatibility with APCu PHP functions and unlock support for advanced Symfony features, such as the APCu Cache adapter.
Dump the Service Container into a Single File
Symfony compiles the service container into multiple small files by default. Set this parameter to true to compile the entire container into a single file, which could improve performance when using "class preloading" in PHP 7.4 or newer versions:
Tip
The . prefix denotes a parameter that is only used during compilation of the container. See Configuration Parameters for more details.
Use the OPcache Byte Code Cache
OPcache stores the compiled PHP files to avoid having to recompile them for every request. There are some byte code caches available, but as of PHP 5.5, PHP comes with OPcache built-in. For older versions, the most widely used byte code cache is APC.
Use the OPcache class preloading
Starting from PHP 7.4, OPcache can compile and load classes at start-up and make them available to all requests until the server is restarted, improving performance significantly.
During container compilation (e.g. when running the cache:clear command), Symfony generates a file with the list of classes to preload in thevar/cache/ directory. Rather than use this file directly, use theconfig/preload.php file that is created whenusing Symfony Flex in your project:
If this file is missing, run this command to update the Symfony Flex recipe:composer recipes:update symfony/framework-bundle.
Use the container.preload andcontainer.no_preload service tags to define which classes should or should not be preloaded by PHP.
Configure OPcache for Maximum Performance
The default OPcache configuration is not suited for Symfony applications, so it's recommended to change these settings as follows:
Don't Check PHP Files Timestamps
In production servers, PHP files should never change, unless a new application version is deployed. However, by default OPcache checks if cached files have changed their contents since they were cached. This check introduces some overhead that can be avoided as follows:
After each deployment, you must empty and regenerate the cache of OPcache. Otherwise you won't see the updates made in the application. Given that in PHP, the CLI and the web processes don't share the same OPcache, you cannot clear the web server OPcache by executing some command in your terminal. These are some of the possible solutions:
- Restart the web server;
- Call the
apc_clear_cache()oropcache_reset()functions via the web server (i.e. by having these in a script that you execute over the web); - Use the cachetool utility to control APC and OPcache from the CLI.
Configure the PHP realpath Cache
When a relative path is transformed into its real and absolute path, PHP caches the result to improve performance. Applications that open many PHP files, such as Symfony projects, should use at least these values:
Note
PHP disables the realpath cache when the open_basedir config option is enabled.
Optimize Composer Autoloader
The class loader used while developing the application is optimized to find new and changed classes. In production servers, PHP files should never change, unless a new application version is deployed. That's why you can optimize Composer's autoloader to scan the entire application once and 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.
Execute this command to generate the new class map (and make it part of your deployment process too):
--no-devexcludes the classes that are only needed in the development environment (i.e.require-devdependencies andautoload-devrules);--classmap-authoritativecreates a class map for PSR-0 and PSR-4 compatible classes used in your application and prevents Composer from scanning the file system for classes that are not found in the class map. (see: Composer's autoloader optimization).
Disable Dumping the Container as XML in Debug Mode
In debug mode, Symfony generates an XML file with all theservice container information (services, arguments, etc.) This XML file is used by various debugging commands such as debug:containerand debug:autowiring.
When the container grows larger and larger, so does the size of the file and the time to generate it. If the benefit of this XML file does not outweigh the decrease in performance, you can stop generating the file as follows:
Profiling Symfony Applications
Profiling with Blackfire
Blackfire is the best tool to profile and optimize performance of Symfony applications during development, test and production. It's a commercial service, but provides a full-featured demo.
Profiling with Symfony Stopwatch
Symfony provides a basic performance profiler in the developmentconfig environment. Click on the "time panel" of the web debug toolbar to see how much time Symfony spent on tasks such as making database queries and rendering templates.
You can measure the execution time and memory consumption of your own code and display the result in the Symfony profiler thanks to the Stopwatch component.
When using autowiring, type-hint any controller or service argument with the Stopwatch class and Symfony will inject the debug.stopwatch service:
If the request calls this service during its execution, you'll see a new event called export-data in the Symfony profiler.
The start(), stop() and getEvent() methods return aStopwatchEvent object that provides information about the current event, even while it's still running. This object can be converted to a string for a quick summary:
You can also profile your template code with the stopwatch Twig tag:
Profiling Categories
Use the second optional argument of the start() method to define the category or tag of the event. This helps keep events organized by type:
Profiling Periods
A real-world stopwatch not only includes the start/stop button but also a "lap button" to measure each partial lap. This is exactly what the lap()method does, which stops an event and then restarts it immediately:
Profiling Sections
Sections are a way to split the profile timeline into groups. Example:
All events that don't belong to any named section are added to the special section called __root__. This way you can get all stopwatch events, even if you don't know their names, as follows:
This work, including the code samples, is licensed under aCreative Commons BY-SA 3.0 license.