|
| 1 | +.. index:: |
| 2 | + single: Tests |
| 3 | + |
| 4 | +Performance |
| 5 | +=========== |
| 6 | + |
| 7 | +Symfony2 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 chapter, |
| 9 | +you'll explore many of the most common and powerful ways to make your Symfony |
| 10 | +application even faster. |
| 11 | + |
| 12 | +.. index:: |
| 13 | + single: Performance; Byte code cache |
| 14 | + |
| 15 | +Use a Byte Code Cache (e.g. APC) |
| 16 | +-------------------------------- |
| 17 | + |
| 18 | +One the best (and easiest) things that you should do to improve your performance |
| 19 | +is to use a "byte code cache". The idea of a byte code cache is to remove |
| 20 | +the need to constantly recompile the PHP source code. There are a number of |
| 21 | +`byte code caches`_ available, some of which are open source. The most widely |
| 22 | +used byte code cache is probably `APC`_ |
| 23 | + |
| 24 | +Using a byte code cache really has no downside, and Symfony2 has been architected |
| 25 | +to perform really well in this type of environment. |
| 26 | + |
| 27 | +Further Optimizations |
| 28 | +~~~~~~~~~~~~~~~~~~~~~ |
| 29 | + |
| 30 | +Byte code caches usually monitor the source files for changes. This ensures |
| 31 | +that if the source of a file changes, the byte code is recompiled automatically. |
| 32 | +This is really convenient, but obviously adds overhead. |
| 33 | + |
| 34 | +For this reason, some byte code caches offer an option to disable these checks. |
| 35 | +Obviously, when disabling these checks, it will be up to the server admin |
| 36 | +to ensure that the cache is cleared whenever any source files change. Otherwise, |
| 37 | +the updates you've made won't be seen. |
| 38 | + |
| 39 | +For example, to disable these checks in APC, simply add ``apc.stat=0`` to |
| 40 | +your php.ini configuration. |
| 41 | + |
| 42 | +.. index:: |
| 43 | + single: Performance; Autoloader |
| 44 | + |
| 45 | +Use an Autoloader that caches (e.g. ``ApcUniversalClassLoader``) |
| 46 | +---------------------------------------------------------------- |
| 47 | + |
| 48 | +By default, the Symfony2 standard edition uses the ``UniversalClassLoader`` |
| 49 | +in the `autoloader.php`_ file. This autoloader is easy to use, as it will |
| 50 | +automatically find any new classes that you've placed in the registered |
| 51 | +directories. |
| 52 | + |
| 53 | +Unfortunately, this comes at a cost, as the loader iterates over all configured |
| 54 | +namespaces to find a particular file, making ``file_exists`` calls until it |
| 55 | +finally finds the file it's looking for. |
| 56 | + |
| 57 | +The simplest solution is to cache the location of each class after it's located |
| 58 | +the first time. Symfony comes with a class - ``ApcUniversalClassLoader`` - |
| 59 | +loader that extends the ``UniversalClassLoader`` and stores the class locations |
| 60 | +in APC. |
| 61 | + |
| 62 | +To use this class loader, simply adapt your ``autoloader.php`` as follows: |
| 63 | + |
| 64 | +.. code-block:: php |
| 65 | +
|
| 66 | + // app/autoload.php |
| 67 | + require __DIR__.'/../vendor/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php'; |
| 68 | + require __DIR__.'/../vendor/symfony/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php'; |
| 69 | +
|
| 70 | + use Symfony\Component\ClassLoader\ApcUniversalClassLoader; |
| 71 | +
|
| 72 | + $loader = new ApcUniversalClassLoader('some caching unique prefix'); |
| 73 | + // ... |
| 74 | +
|
| 75 | +.. note:: |
| 76 | + |
| 77 | + When using the APC autoloader, if you add new classes, they will be found |
| 78 | + and automatically and everything will work the same as before (i.e. no |
| 79 | + reason to "clear" the cach). However, if you change the location of a |
| 80 | + particular namespace or prefix, you'll need to flush your APC cache. Otherwise, |
| 81 | + the autoloader will still be looking at the old location for all classes |
| 82 | + inside that namespace. |
| 83 | + |
| 84 | +.. index:: |
| 85 | + single: Performance; Bootstrap files |
| 86 | + |
| 87 | +Use Bootstrap Files |
| 88 | +------------------- |
| 89 | + |
| 90 | +To ensure optimal flexibility and code reuse, Symfony2 applications leverage |
| 91 | +a variety of classes and 3rd party components. But loading all of these classes |
| 92 | +from separate files on each request can result in some overhead. To reduce |
| 93 | +this overhead, the Symfony2 Standard Edition provides a script to generate |
| 94 | +a so-called `bootstrap file`_, consisting of multiple classes definitions |
| 95 | +in a single file. By including this file (which contains a copy of many of |
| 96 | +the core classes), Symfony no longer needs to include any of the source files |
| 97 | +containing those classes. This will reduce disc IO quite a bit. |
| 98 | + |
| 99 | +If you're using the Symfony2 Standard Edition, then you're probably already |
| 100 | +using the bootstrap file. To be sure, open your front controller (usually |
| 101 | +``app.php``) and check to make sure that one of the following lines exists |
| 102 | +and is uncommented (exactly which you need depends on if you're using Symfony's |
| 103 | +:doc:`HTTP Caching layer</book/http_cache>`):: |
| 104 | + |
| 105 | + require_once __DIR__.'/../app/bootstrap.php.cache'; |
| 106 | + require_once __DIR__.'/../app/bootstrap_cache.php.cache'; |
| 107 | + |
| 108 | +Note that there are two disadvantages when using a bootstrap file: |
| 109 | + |
| 110 | +* the file needs to be regenerated whenever any of the original sources change |
| 111 | + (i.e. when you update the Symfony2 source or vendor libraries); |
| 112 | + |
| 113 | +* when debugging, one will need to place break points inside the bootstrap file. |
| 114 | + |
| 115 | +If you're using Symfony2 Standard Edition, the bootstrap files are automatically |
| 116 | +rebuilt after updating the vendor libraries via the ``php bin/vendors install`` |
| 117 | +command. |
| 118 | + |
| 119 | +Bootstrap Files and Byte Code Caches |
| 120 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 121 | + |
| 122 | +Even when using a byte code cache, performance will improve when using a bootstrap |
| 123 | +file since there will be less files to monitor for changes. Of course if this |
| 124 | +feature is disabled in the byte code cache (e.g. ``apc.stat=0`` in APC), there |
| 125 | +is no longer a reason to use a bootstrap file. |
| 126 | + |
| 127 | +.. _`byte code caches`: http://en.wikipedia.org/wiki/List_of_PHP_accelerators |
| 128 | +.. _`APC`: http://php.net/manual/en/book.apc.php |
| 129 | +.. _`autoloader.php`: https://github.com/symfony/symfony-standard/blob/master/app/autoload.php |
| 130 | +.. _`bootstrap file`: https://github.com/sensio/SensioDistributionBundle/blob/master/Resources/bin/build_bootstrap.php |
0 commit comments