18-Performance.txt 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. Chapter 18 - Performance
  2. ========================
  3. If you expect your website will attract a crowd, performance and optimization issues should be a major factor during the development phase. Rest assured, performance has always been a chief concern among the core symfony developers.
  4. While the advantages gained by accelerating the development process result in some overhead, the core symfony developers have always been cognizant of performance requirements. Accordingly, every class and every method have been closely inspected and optimized to be as fast as possible. The basic overhead, which you can measure by comparing the time to display a "hello, world" message with and without symfony, is minimal. As a result, the framework is scalable and reacts well to stress tests. And as the ultimate proof, some websites with extremely high traffic (that is, websites with millions of active subscribers and a lot of server-pressuring Ajax interactions) use symfony and are very satisfied with its performance. Check the list of websites developed with symfony in the wiki ([http://trac.symfony-project.org/wiki/ApplicationsDevelopedWithSymfony](http://trac.symfony-project.org/wiki/ApplicationsDevelopedWithSymfony)) for names.
  5. But, of course, high-traffic websites often have the means to expand the server farm and upgrade hardware as they see fit. If you don't have the resources to do this, or if you want to be sure the full power of the framework is always at your disposal, there are a few tweaks that you can use to further speed up your symfony application. This chapter lists some of the recommended performance optimizations at all levels of the framework and they are mostly for advanced users. Some of them were already mentioned throughout the previous chapters, but you will find it useful to have them all in one place.
  6. Tweaking the Server
  7. -------------------
  8. A well-optimized application should rely on a well-optimized server. You should know the basics of server performance to make sure there is no bottleneck outside symfony. Here are a few things to check to make sure that your server isn't unnecessarily slow.
  9. Having `magic_quotes_gpc` turned `on` in the `php.ini` slows down an application, because it tells PHP to escape all quotes in request parameters, but symfony will systematically unescape them afterwards, and the only consequence will be a loss of time--and quotes-escaping problems on some platforms. Therefore, turn this setting off if you have access to the PHP configuration.
  10. The more recent PHP release you use, the better. PHP 5.2 is faster than PHP 5.1, and PHP 5.1 is a lot faster than PHP 5.0. So make sure you upgrade your PHP version to benefit from the latest performance improvements.
  11. The use of a PHP accelerator (such as APC, XCache, or eAccelerator) is almost compulsory for a production server, because it can make PHP run an average 50% faster, with no tradeoff. Make sure you install one of the accelerator extensions to feel the real speed of PHP.
  12. On the other hand, make sure you deactivate any debug utility, such as the Xdebug or APD extension, in your production server.
  13. >**NOTE**
  14. >You might be wondering about the overhead caused by the `mod_rewrite` extension: it is negligible. Of course, loading an image with rewriting rules is slower than loading an image without, but the slowdown is orders of magnitude below the execution of any PHP statement.
  15. >**TIP**
  16. >When one server is not enough, you can still add another and use load balancing. As long as the `uploads/` directory is shared and you use database storage for sessions, a symfony project will react seamlessly in a load-balanced architecture.
  17. Tweaking the Model
  18. ------------------
  19. In symfony, the model layer has the reputation of being the slowest part. If benchmarks show that you have to optimize this layer, here are a few possible improvements.
  20. ### Optimizing Propel Integration
  21. Initializing the model layer (the core Propel classes) takes some time, because of the need to load a few classes and construct various objects. However, because of the way symfony integrates Propel, these initialization tasks occur only when an action actually needs the model--and as late as possible. The Propel classes will be initialized only when an object of your generated model is autoloaded. This means pages that don't use the model are not penalized by the model layer.
  22. If your entire application doesn't require the use of the model layer, you can also save the initialization of the `sfDatabaseManager` by switching the whole layer off in your `settings.yml`:
  23. all:
  24. .settings:
  25. use_database: off
  26. The generated model classes (in `lib/model/om/`) are already optimized--they don't contain comments, and they benefit from the autoloading system. Relying on autoloading instead of manually including files means that classes are loaded only if it is really necessary. So in case one model class is not needed, having classes autoloaded will save execution time, while the alternative method of using `include` statements won't. As for the comments, they document the use of the generated methods but lengthen the model files--resulting in a minor overhead on slow disks. As the generated method names are pretty explicit, the comments are turned off by default.
  27. These two enhancements are symfony-specific, but you can revert to the Propel defaults by changing two settings in your `propel.ini` file, as follows:
  28. propel.builder.addIncludes = true # Add include statements in generated classes
  29. # Instead of relying on the autoloading system
  30. propel.builder.addComments = true # Add comments to generated classes
  31. ### Limiting the Number of Objects to Hydrate
  32. When you use a method of a peer class to retrieve objects, your query goes through the hydrating process (creating and populating objects based on the rows of the result of the query). For instance, to retrieve all the rows of the `article` table with Propel, you usually do the following:
  33. [php]
  34. $articles = ArticlePeer::doSelect(new Criteria());
  35. The resulting `$articles` variable is an array of objects of class `Article`. Each object has to be created and initialized, which takes time. This has one major consequence: Contrary to direct database queries, the speed of a Propel query is directly proportional to the number of results it returns. This means your model methods should be optimized to return only a given number of results. When you don't need all the results returned by a `Criteria`, you should limit it with the `setLimit()` and `setOffset()` methods. For instance, if you need only the rows 10 to 20 of a particular query, refine the `Criteria` as in Listing 18-1.
  36. Listing 18-1 - Limiting the Number of Results Returned by a Criteria
  37. [php]
  38. $c = new Criteria();
  39. $c->setOffset(10); // Offset of the first record returned
  40. $c->setLimit(10); // Number of records returned
  41. $articles = ArticlePeer::doSelect($c);
  42. This can be automated by the use of a pager. The `sfPropelPager` object automatically handles the offset and the limit of a Propel query to hydrate only the objects required for a given page. Refer to the [pager documentation](http://www.symfony-project.org/cookbook/1_1/pager) for more information on this class.
  43. ### Minimizing the Number of Queries with Joins
  44. During application development, you should keep an eye on the number of database queries issued by each request. The web debug toolbar shows the number of queries for each page, and clicking the little database icon reveals the SQL code of these queries. If you see the number of queries rising abnormally, it is time to consider using a Join.
  45. Before explaining the Join methods, let's review what happens when you loop over an array of objects and use a Propel getter to retrieve details about a related class, as in Listing 18-2. This example supposes that your schema describes an `article` table with a foreign key to an `author` table.
  46. Listing 18-2 - Retrieving Details About a Related Class in a Loop
  47. [php]
  48. // In the action
  49. $this->articles = ArticlePeer::doSelect(new Criteria());
  50. // Database query issued by doSelect()
  51. SELECT article.id, article.title, article.author_id, ...
  52. FROM article
  53. // In the template
  54. <ul>
  55. <?php foreach ($articles as $article): ?>
  56. <li><?php echo $article->getTitle() ?>,
  57. written by <?php echo $article->getAuthor()->getName() ?></li>
  58. <?php endforeach; ?>
  59. </ul>
  60. If the `$articles` array contains ten objects, the `getAuthor()` method will be called ten times, which in turn executes one database query each time it is called to hydrate one object of class `Author`, as in Listing 18-3.
  61. Listing 18-3 - Foreign Key Getters Issue One Database Query
  62. [php]
  63. // In the template
  64. $article->getAuthor()
  65. // Database query issued by getAuthor()
  66. SELECT author.id, author.name, ...
  67. FROM author
  68. WHERE author.id = ? // ? is article.author_id
  69. So the page of Listing 18-2 will require a total of 11 queries: the one necessary to build the array of `Article` objects, plus the 10 queries to build one `Author` object at a time. This is a lot of queries to display only a list of articles and their author.
  70. If you were using plain SQL, you would know how to reduce the number of queries to only one by retrieving the columns of the `article` table and those of the `author` table in the same query. That's exactly what the `doSelectJoinAuthor()` method of the `ArticlePeer` class does. It issues a slightly more complex query than a simple `doSelect()` call, but the additional columns in the result set allow Propel to hydrate both `Article` objects and the related `Author` objects. The code of Listing 18-4 displays exactly the same result as Listing 18-2, but it requires only one database query to do so rather than 11 and therefore is faster.
  71. Listing 18-4 - Retrieving Details About Articles and Their Author in the Same Query
  72. [php]
  73. // In the action
  74. $this->articles = ArticlePeer::doSelectJoinAuthor(new Criteria());
  75. // Database query issued by doSelectJoinAuthor()
  76. SELECT article.id, article.title, article.author_id, ...
  77. author.id, author.name, ...
  78. FROM article, author
  79. WHERE article.author_id = author.id
  80. // In the template (unchanged)
  81. <ul>
  82. <?php foreach ($articles as $article): ?>
  83. <li><?php echo $article->getTitle() ?>,
  84. written by <?php echo $article->getAuthor()->getName() ?></li>
  85. <?php endforeach; ?>
  86. </ul>
  87. There is no difference in the result returned by a `doSelect()` call and a `doSelectJoinXXX()` method; they both return the same array of objects (of class Article in the example). The difference appears when a foreign key getter is used on these objects afterwards. In the case of `doSelect()`, it issues a query, and one object is hydrated with the result; in the case of `doSelectJoinXXX()`, the foreign object already exists and no query is required, and the process is much faster. So if you know that you will need related objects, call a `doSelectJoinXXX()` method to reduce the number of database queries--and improve the page performance.
  88. The `doSelectJoinAuthor()` method is automatically generated when you call a `propel-build-model` because of the relationship between the `article` and `author` tables. If there were other foreign keys in the article table structure--for instance, to a category table--the generated `BaseArticlePeer` class would have other Join methods, as shown in Listing 18-5.
  89. Listing 18-5 - Example of Available `doSelect` Methods for an `ArticlePeer` Class
  90. [php]
  91. // Retrieve Article objects
  92. doSelect()
  93. // Retrieve Article objects and hydrate related Author objects
  94. doSelectJoinAuthor()
  95. // Retrieve Article objects and hydrate related Category objects
  96. doSelectJoinCategory()
  97. // Retrieve Article objects and hydrate related objects except Author
  98. doSelectJoinAllExceptAuthor()
  99. // Synonym of
  100. doSelectJoinAll()
  101. The peer classes also contain Join methods for `doCount()`. The classes with an i18n counterpart (see Chapter 13) provide a `doSelectWithI18n()` method, which behaves the same as Join methods but for i18n objects. To discover the available Join methods in your model classes, you should inspect the generated peer classes in `lib/model/om/`. If you don't find the Join method needed for your query (for instance, there is no automatically generated Join method for many-to-many relationships), you can build it yourself and extend your model.
  102. >**TIP**
  103. >Of course, a `doSelectJoinXXX()` call is a bit slower than a call to `doSelect()`, so it only improves the overall performance if you use the hydrated objects afterwards.
  104. ### Avoid Using Temporary Arrays
  105. When using Propel, objects are already hydrated, so there is no need to prepare a temporary array for the template. Developers not used to ORMs usually fall into this trap. They want to prepare an array of strings or integers, whereas the template can rely directly on an existing array of objects. For instance, imagine that a template displays the list of all the titles of the articles present in the database. A developer who doesn't use OOP would probably write code similar to what is shown in Listing 18-6.
  106. Listing 18-6 - Preparing an Array in the Action Is Useless If You Already Have One
  107. [php]
  108. // In the action
  109. $articles = ArticlePeer::doSelect(new Criteria());
  110. $titles = array();
  111. foreach ($articles as $article)
  112. {
  113. $titles[] = $article->getTitle();
  114. }
  115. $this->titles = $titles;
  116. // In the template
  117. <ul>
  118. <?php foreach ($titles as $title): ?>
  119. <li><?php echo $title ?></li>
  120. <?php endforeach; ?>
  121. </ul>
  122. The problem with this code is that the hydrating is already done by the `doSelect()` call (which takes time), making the `$titles` array superfluous, since you can write the same code as in Listing 18-7. So the time spent to build the `$titles` array could be gained to improve the application performance.
  123. Listing 18-7 - Using an Array of Objects Exempts You from Creating a Temporary Array
  124. [php]
  125. // In the action
  126. $this->articles = ArticlePeer::doSelect(new Criteria());
  127. // In the template
  128. <ul>
  129. <?php foreach ($articles as $article): ?>
  130. <li><?php echo $article->getTitle() ?></li>
  131. <?php endforeach; ?>
  132. </ul>
  133. If you feel that you really need to prepare a temporary array because some processing is necessary on objects, the right way to do so is to create a new method in your model class that directly returns this array. For instance, if you need an array of article titles and the number of comments for each article, the action and the template should look like Listing 18-8.
  134. Listing 18-8 - Using a Custom Method to Prepare a Temporary Array
  135. [php]
  136. // In the action
  137. $this->articles = ArticlePeer::getArticleTitlesWithNbComments();
  138. // In the template
  139. <ul>
  140. <?php foreach ($articles as $article): ?>
  141. <li><?php echo $article[0] ?> (<?php echo $article[1] ?> comments)</li>
  142. <?php endforeach; ?>
  143. </ul>
  144. It's up to you to build a fast-processing `getArticleTitlesWithNbComments()` method in the model--for instance, by bypassing the whole object-relational mapping and database abstraction layers.
  145. ### Bypassing the ORM
  146. When you don't really need objects but only a few columns from various tables, as in the previous example, you can create specific methods in your model that bypass completely the ORM layer. You can directly call the database with Creole, for instance, and return a custom-built array. Listing 18-9 illustrates this idea.
  147. Listing 18-9 - Using Direct Creole Access for Optimized Model Methods, in `lib/model/ArticlePeer.php`
  148. [php]
  149. class ArticlePeer extends BaseArticlePeer
  150. {
  151. public static function getArticleTitlesWithNbComments()
  152. {
  153. $connection = Propel::getConnection();
  154. $query = 'SELECT %s as title, COUNT(%s) AS nb FROM %s LEFT JOIN %s ON %s = %sGROUP BY %s';
  155. $query = sprintf($query,
  156. ArticlePeer::TITLE, CommentPeer::ID,
  157. ArticlePeer::TABLE_NAME, CommentPeer::TABLE_NAME,
  158. ArticlePeer::ID, CommentPeer::ARTICLE_ID,
  159. ArticlePeer::ID
  160. );
  161. $statement = $connection->prepareStatement($query);
  162. $resultset = $statement->executeQuery();
  163. $results = array();
  164. while ($resultset->next())
  165. {
  166. $results[] = array($resultset->getString('title'), $resultset->getInt('nb'));
  167. }
  168. return $results;
  169. }
  170. }
  171. When you start building these sorts of methods, you may end up writing one custom method for each action, and lose the benefit of the layer separation--not to mention the fact that you lose database-independence.
  172. >**TIP**
  173. >If Propel doesn't suit you as a model layer, consider using other ORMs before writing your queries by hand. For instance, check the `sfDoctrine` plug-in for an interface with the PhpDoctrine ORM. In addition, you can use another database abstraction layer than Creole to access your database directly. As of PHP 5.1, PDO is bundled with PHP and provides a faster alternative to Creole.
  174. ### Speeding Up the Database
  175. There are many database-specific optimization techniques that can be applied regardless of whether you're using symfony. This section briefly outlines the most common database optimization strategies, but a good knowledge of database engines and administration is required to get the most out of your model layer.
  176. >**TIP**
  177. >Remember that the web debug toolbar displays the time taken by each query in a page, and that every tweak should be monitored to determine whether it really improves performance.
  178. Table queries are often based on non-primary key columns. To improve the speed of such queries, you should define indexes in your database schema. To add a single column index, add the `index: true` property to the column definition, as in Listing 18-10.
  179. Listing 18-10 - Adding a Single Column Index, in `config/schema.yml`
  180. propel:
  181. article:
  182. id:
  183. author_id:
  184. title: { type: varchar(100), index: true }
  185. You can use the alternative `index: unique` syntax to define a unique index instead of a classic one. You can also define multiple column indices in `schema.yml` (refer to Chapter 8 for more details about the indexing syntax). You should strongly consider doing this, because it is often a good way to speed up a complex query.
  186. After adding an index to a schema, you should do the same in the database itself, either by issuing an `ADD INDEX` query directly in the database or by calling the `propel-build-all` command (which will not only rebuild the table structure, but also erase all the existing data).
  187. >**TIP**
  188. >Indexing tends to make `SELECT` queries faster, but `INSERT`, `UPDATE`, and `DELETE` queries are slower. Also, database engines use only one index per query, and they infer the index to be used for each query based on internal heuristics. Adding an index can sometimes be disappointing in terms of performance boost, so make sure you measure the improvements.
  189. Unless specified otherwise, each request uses a single database connection in symfony, and the connection is closed at the end of the request. You can enable persistent database connections to use a pool of database connections that remain open between queries, by setting `persistent: true` in the `databases.yml` file, as shown in Listing 18-11.
  190. Listing 18-11 - Enabling Persistent Database Connection Support, in `config/databases.yml`
  191. prod:
  192. propel:
  193. class: sfPropelDatabase
  194. param:
  195. persistent: true
  196. dsn: mysql://login:passwd@localhost/blog
  197. This may or may not improve the overall database performance, depending on numerous factors. The documentation on the subject is abundant on the Internet. Make sure you benchmark your application performance before and after changing this setting to validate its interest.
  198. >**SIDEBAR**
  199. >MySQL-specific tips
  200. >
  201. >Many settings of the MySQL configuration, found in the my.cnf file, may alter database performance. Make sure you read the online documentation ([http://dev.mysql.com/doc/refman/5.0/en/option-files.html](http://dev.mysql.com/doc/refman/5.0/en/option-files.html)) on this subject.
  202. >
  203. >One of the tools provided by MySQL is the slow queries log. All SQL statements that take more than `long_query_time` seconds to execute (this is a setting that can be changed in the `my.cnf`) are logged in a file that is quite difficult to construe by hand, but that the `mysqldumpslow` command summarizes usefully. This is a great tool to detect the queries that require optimizations.
  204. Tweaking the View
  205. -----------------
  206. According to how you design and implement the view layer, you may notice small slowdowns or speedups. This section describes the alternatives and their tradeoffs.
  207. ### Using the Fastest Code Fragment
  208. If you don't use the caching system, you have to be aware that an `include_component()` is slightly slower than an `include_partial()`, which itself is slightly slower than a simple PHP `include`. This is because symfony instantiates a view to include a partial and an object of class `sfComponent` to include a component, which collectively add some minor overhead beyond what's required to include the file.
  209. However, this overhead is insignificant, unless you include a lot of partials or components in a template. This may happen in lists or tables, and every time you call an `include_partial()` helper inside a `foreach` statement. When you notice that a large number of partial or component inclusions have a significant impact on your performance, you may consider caching (see Chapter 12), and if caching is not an option, then switch to simple `include` statements.
  210. As for slots and component slots, the difference in performance is perceptible. The process time necessary to set and include a slot is negligible--it is equivalent to a variable instantiation. But component slots rely on a view configuration, and they require a few objects to be initiated to work. However, component slots can be cached independently from the calling templates, while slots are always cached within the template that includes them.
  211. ### Speeding Up the Routing Process
  212. As explained in Chapter 9, every call to a link helper in a template asks the routing system to process an internal URI into an external URL. This is done by finding a match between the URI and the patterns of the `routing.yml` file. Symfony does it quite simply: It tries to match the first rule with the given URI, and if it doesn't work, it tries with the following, and so on. As every test involves regular expressions, this is quite time consuming.
  213. There is a simple workaround: Use the rule name instead of the module/action couple. This will tell symfony which rule to use, and the routing system won't lose time trying to match all previous rules.
  214. In concrete terms, consider the following routing rule, defined in your `routing.yml` file:
  215. article_by_id:
  216. url: /article/:id
  217. param: { module: article, action: read }
  218. Then instead of outputting a hyperlink this way:
  219. [php]
  220. <?php echo link_to('my article', 'article/read?id='.$article->getId()) ?>
  221. you should use the fastest version:
  222. [php]
  223. <?php echo link_to('my article', '@article_by_id?id='.$article->getId()) ?>
  224. The difference starts being noticeable when a page includes a few dozen routed hyperlinks.
  225. ### Skipping the Template
  226. Usually, a response is composed of a set of headers and content. But some responses don't need content. For instance, some Ajax interactions need only a few pieces of data from the server in order to feed a JavaScript program that will update different parts of the page. For this kind of short response, a set of headers alone is faster to transmit. As discussed in Chapter 11, an action can return only a JSON header. Listing 18-12 reproduces an example from Chapter 11.
  227. Listing 18-12 - Example Action Returning a JSON Header
  228. [php]
  229. public function executeRefresh()
  230. {
  231. $output = '{"title":"My basic letter","name":"Mr Brown"}';
  232. $this->getResponse()->setHttpHeader("X-JSON", '('.$output.')');
  233. return sfView::HEADER_ONLY;
  234. }
  235. This skips the template and the layout, and the response can be sent at once. As it contains only headers, it is more lightweight and will take less time to transmit to the user.
  236. Chapter 6 explained another way to skip the template by returning content text directly from the action. This breaks the MVC separation, but it can increase the responsiveness of an action greatly. Check Listing 18-13 for an example.
  237. Listing 18-13 - Example Action Returning Content Text Directly
  238. [php]
  239. public function executeFastAction()
  240. {
  241. return $this->renderText("<html><body>Hello, World!</body></html>");
  242. }
  243. ### Restricting the Default Helpers
  244. The standard helper groups (`Partial`, `Cache`, and `Form`) are loaded for every request. If you are sure that you won't use some of them, removing a helper group from the list of standard ones will save you the parsing of the helper file. In particular, the Form helper group, although included by default, is quite heavy and slows down pages with no forms just because of its size. So it might be a good idea to edit the `standard_helpers` setting in the `settings.yml` file to remove it:
  245. all:
  246. .settings:
  247. standard_helpers: [Partial, Cache] # Form is removed
  248. The tradeoff is that you must declare the `Form` helper group on each template using it with `use_helper('Form')`.
  249. ### Compressing the Response
  250. Symfony compresses the response before sending it to the user. This feature is based on the PHP zlib module. You can save a little CPU time for each request by deactivating it in the `settings.yml` file:
  251. all:
  252. .settings:
  253. compressed: off
  254. Be aware that the CPU gain will be balanced by the bandwidth loss, so the performance won't increase in all configurations with this change.
  255. >**TIP**
  256. >If you deactivate zip compression in PHP, you can enable it at the server level. Apache has a compression extension of its own.
  257. Tweaking the Cache
  258. ------------------
  259. Chapter 12 already described how to cache parts of a response or all of it. The response cache results in a major performance improvement, and it should be one of your first optimization considerations. If you want to make the most out of the cache system, read further, for this section unveils a few tricks you might not have thought of.
  260. ### Clearing Selective Parts of the Cache
  261. During application development, you have to clear the cache in various situations:
  262. * When you create a new class: Adding a class to an autoloading directory (one of the project's `lib/` folders) is not enough to have symfony find it automatically in non-development environments. You must clear the autoloading configuration cache so that symfony browses again all the directories of the `autoload.yml` file and references the location of autoloadable classes--including the new ones.
  263. * When you change the configuration in production: The configuration is parsed only during the first request in production. Further requests use the cached version instead. So a change in the configuration in the production environment (or any environment where debug is turned off) doesn't take effect until you clear the cached version of the file.
  264. * When you modify a template in an environment where the template cache is enabled: The valid cached templates are always used instead of existing templates in production, so a template change is ignored until the template cache is cleared or outdated.
  265. * When you update an application with the `project:deploy` command: This case usually covers the three previous modifications.
  266. The problem with clearing the whole cache is that the next request will take quite long to process, because the configuration cache needs to be regenerated. Besides, the templates that were not modified will be cleared from the cache as well, losing the benefit of previous requests.
  267. That means it's a good idea to clear only the cache files that really need to be regenerated. Use the options of the `cache:clear` task to define a subset of cache files to clear, as demonstrated in Listing 18-14.
  268. Listing 18-14 - Clearing Only Selective Parts of the Cache
  269. // Clear only the cache of the frontend application
  270. > php symfony cache:clear frontend
  271. // Clear only the HTML cache of the frontend application
  272. > php symfony cache:clear frontend template
  273. // Clear only the configuration cache of the frontend application
  274. > php symfony cache:clear frontend config
  275. You can also remove files by hand in the `cache/` directory, or clear template cache files selectively from the action with the `$cacheManager->remove()` method, as described in Chapter 12.
  276. All these techniques will minimize the negative performance impact of any of the changes listed previously.
  277. >**TIP**
  278. >When you upgrade symfony, the cache is automatically cleared, without manual intervention (if you set the `check_symfony_version` parameter to `true` in `settings.yml`).
  279. ### Generating Cached Pages
  280. When you deploy a new application to production, the template cache is empty. You must wait for users to visit a page once for this page to be put in the cache. In critical deployments, the overhead of page processing is not acceptable, and the benefits of caching must be available as soon as the first request is issued.
  281. The solution is to automatically browse the pages of your application in the staging environment (where the configuration is similar to the one in production) to have the template cache generated, then to transfer the application with the cache to production.
  282. To browse the pages automatically, one option is to create a shell script that looks through a list of external URLs with a browser (curl for instance). But there is a better and faster solution: a symfony batch using the `sfBrowser` object, already discussed in Chapter 15. That's an internal browser written in PHP (and used by `sfTestBrowser` for functional tests). It takes an external URL and returns a response, but the interesting thing is that it triggers the template cache just like a regular browser. As it only initializes symfony once and doesn't pass by the HTTP transport layer, this method is a lot faster.
  283. Listing 18-15 shows an example batch script used to generate template cache files in a staging environment. Launch it by calling `php batch/generate_cache.php`.
  284. Listing 18-15 - Generating the Template Cache, in `batch/generate_cache.php`
  285. [php]
  286. require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');
  287. $configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'staging', false);
  288. sfContext::createInstance($configuration);
  289. // Array of URLs to browse
  290. $uris = array(
  291. '/foo/index',
  292. '/foo/bar/id/1',
  293. '/foo/bar/id/2',
  294. ...
  295. );
  296. $b = new sfBrowser();
  297. foreach ($uris as $uri)
  298. {
  299. $b->get($uri);
  300. }
  301. ### Using a Database Storage System for Caching
  302. The default storage system for the template cache in symfony is the file system: Fragments of HTML or serialized response objects are stored under the `cache/` directory of a project. Symfony proposes an alternative way to store cache: a SQLite database. Such a database is a simple file that PHP natively knows how to query very efficiently.
  303. To tell symfony to use SQLite storage instead of file system storage for the template cache, open the `factories.yml` file and edit the `view_cache` entry as follows:
  304. view_cache:
  305. class: sfSQLiteCache
  306. param:
  307. database: %SF_TEMPLATE_CACHE_DIR%/cache.db
  308. The benefits of using SQLite storage for the template cache are faster read and write operations when the number of cache elements is important. If your application makes heavy use of caching, the template cache files end up scattered in a deep file structure; in this case, switching to SQLite storage will increase performance. In addition, clearing the cache on file system storage may require a lot of files to be removed from the disk; this operation may last a few seconds, during which your application is unavailable. With a SQLite storage system, the cache clearing process results in a single file operation: the deletion of the SQLite database file. Whatever the number of cache elements currently stored, the operation is instantaneous.
  309. ### Bypassing Symfony
  310. Perhaps the best way to speed symfony up is to bypass it completely . . . this is said only partly in jest. Some pages don't change and don't need to be reprocessed by the framework at each request. The template cache is already here to speed up the delivery of such pages, but it still relies on symfony.
  311. A couple of tricks described in Chapter 12 allow you to bypass symfony completely for some pages. The first one involves the use of HTTP 1.1 headers for asking the proxies and client browsers to cache the page themselves, so that they don't request it again the next time the page is needed. The second one is the super fast cache (automated by the `sfSuperCachePlugin` plug-in), which consists of storing a copy of the response in the `web/` directory and modifying the rewriting rules so that Apache first looks for a cached version before handing a request to symfony.
  312. Both these methods are very effective, and even if they only apply to static pages, they will take the burden of handling these pages off from symfony, and the server will then be fully available to deal with complex requests.
  313. ### Caching the Result of a Function Call
  314. If a function doesn't rely on context-sensitive values nor on randomness, calling it twice with the same parameters should return the same result. That means the second call could very well be avoided if the result had been stored the first time. That's exactly what the `sfFunctionCache` class does. This class has a `call()` method, which expects a callable and an array of parameters as its arguments. When called, this method creates an md5 hash with all its arguments and looks in the cache for a key named by this hash. If such a key is found, the function returns the result stored in the cache. If not, the `sfFunctionCache` executes the function, stores the result in the cache, and returns it. So the second execution of Listing 18-16 will be faster than the first one.
  315. Listing 18-16 - Caching the Result of a Function
  316. [php]
  317. $cache = new sfFileCache(array('cache_dir' => sfConfig::get('sf_cache_dir').'/function'));
  318. $fc = new sfFunctionCache($cache);
  319. $result1 = $fc->call('cos', array(M_PI));
  320. $result2 = $fc->call('preg_replace', array('/\s\s+/', ' ', $input));
  321. The `sfFunctionCache` constructor expects a cache object. The first argument of the `call()` method must be a callable, so it can be a function name, an array of a class name and static method name, or an array of an object name and public method name. As for the other argument of the `call()` method, it's an array of arguments that will be passed to the callable.
  322. >**CAUTION**
  323. >If you use a file based cache object as in the example, it's better to give a cache directory under the `cache/` directory, as it will be cleanup automatically by the `cache:clear` task. If you store the function cache somewhere else, it will not be cleared automatically when you clear the cache through the command line.
  324. ### Caching Data in the Server
  325. PHP accelerators provide special functions to store data in memory so that you can reuse it across requests. The problem is that they all have a different syntax, and each has its own specific way of performing this task. The symfony cache classes abstract all these differences and works with whatever accelerator you are using. See its syntax in Listing 18-17.
  326. Listing 18-17 - Using a PHP accelerator to cache data
  327. [php]
  328. $cache = new sfAPCCache();
  329. // Storing data in the cache
  330. $cache->set($name, $value, $lifetime);
  331. // Retrieving data
  332. $value = $cache->get($name);
  333. // Checking if a piece of data exists in the cache
  334. $value_exists = $cache->has($name);
  335. // Clear the cache
  336. $cache->clear();
  337. The `set()` method returns `false` if the caching didn't work. The cached value can be anything (a string, an array, an object); the `sfProcessCache` class will deal with the serialization. The `get()` method returns `null` if the required variable doesn't exist in the cache.
  338. >**TIP**
  339. >If you want to go further into memory caching, make sure you take a look at the `sfMemcacheCache` class. It provides the same interface as the other cache classes and it can help decrease the database load on load-balanced applications.
  340. Deactivating the Unused Features
  341. --------------------------------
  342. The default symfony configuration activates the most common features of a web application. However, if you happen to not need all of them, you should deactivate them to save the time their initialization takes on each request.
  343. For instance, if your application doesn't use the session mechanism, or if you want to start the session handling by hand, you should turn the `auto_start` setting to `false` in the `storage` key of the `factories.yml` file, as in Listing 18-19.
  344. Listing 18-19 - Turning Sessions Off, in `frontend/config/factories.yml`
  345. all:
  346. storage:
  347. class: sfSessionStorage
  348. param:
  349. auto_start: false
  350. The same applies for the database feature (as explained in the "Tweaking the Model" section earlier in this chapter). If your application makes no use of a database, deactivate it for a small performance gain, this time in the `settings.yml` file (see Listing 18-20).
  351. Listing 18-20 - Turning Database Features Off, in `frontend/config/settings.yml`
  352. all:
  353. .settings:
  354. use_database: off # Database and model features
  355. As for the security features (see Chapter 6), you can deactivate them in the `filters.yml` file, as shown in Listing 18-21.
  356. Listing 18-21 - Turning Features Off, in `frontend/config/filters.yml`
  357. rendering: ~
  358. security:
  359. enabled: off
  360. # generally, you will want to insert your own filters here
  361. cache: ~
  362. common: ~
  363. execution: ~
  364. Some features are useful only in development, so you should not activate them in production. This is already the case by default, since the production environment in symfony is really optimized for performance. Among the performance-impacting development features, the debug mode is the most severe. As for the symfony logs, the feature is also turned off in production by default.
  365. You may wonder how to get information about failed requests in production if logging is disabled, and argue that problems arise not only in development. Fortunately, symfony can use the `sfErrorLoggerPlugin` plug-in, which runs in the background in production and logs the details of 404 and 500 errors in a database. It is much faster than the file logging feature, because the plug-in methods are called only when a request fails, while the logging mechanism, once turned on, adds a nonnegligible overhead whatever the level. Check the installation instructions and manual at [http://www.symfony-project.com/wiki/sfErrorLoggerPlugin](http://trac.symfony-project.org/wiki/sfErrorLoggerPlugin).
  366. >**TIP**
  367. >Make sure you regularly check the server error logs--they also contain very valuable information about 404 and 500 errors.
  368. Optimizing Your Code
  369. --------------------
  370. It's also possible to speed up your application by optimizing the code itself. This section offers some insight regarding how to do that.
  371. ### Core Compilation
  372. Loading ten files requires more I/O operations than loading one long file, especially on slow disks. Loading a very long file requires more resources than loading a smaller file--especially if a large share of the file content is of no use for the PHP parser, which is the case for comments.
  373. So merging a large number of files and stripping out the comments they contain is an operation that improves performance. Symfony already does that optimization; it's called the core compilation. At the beginning of the first request (or after the cache is cleared), a symfony application concatenates all the core framework classes (`sfActions`, `sfRequest`, `sfView`, and so on) into one file, optimizes the file size by removing comments and double blanks, and saves it in the cache, in a file called `config_core_compile.yml.php`. Each subsequent request only loads this single optimized file instead of the 30 files that compose it.
  374. If your application has classes that must always be loaded, and especially if they are big classes with lots of comments, it may be beneficial to add them to the core compile file. To do so, just add a `core_compile.yml` file in your application `config/` directory, and list in it the classes that you want to add, as in Listing 18-22.
  375. Listing 18-22 - Adding Your Classes to the Core Compile File, in `frontend/config/core_compile.yml`
  376. - %SF_ROOT_DIR%/lib/myClass.class.php
  377. - %SF_ROOT_DIR%/apps/frontend/lib/myToolkit.class.php
  378. - %SF_ROOT_DIR%/plugins/myPlugin/lib/myPluginCore.class.php
  379. ...
  380. ### The sfOptimizer Plug-In
  381. Symfony also offers another optimization tool, called `sfOptimizer`. It applies various optimization strategies to the symfony and application code, which may further speed up the execution.
  382. The symfony code counts many tests that rely on configuration parameters--and your application may also do so. For instance, if you take a look at the symfony classes, you will often see a test on the value of the `sf_logging_enabled` parameter before a call to the `sfLogger` object:
  383. [php]
  384. if (sfConfig::get('sf_logging_enabled'))
  385. {
  386. $this->getContext()->getLogger()->info('Been there');
  387. }
  388. Even if the `sfConfig` registry is very well optimized, the number of calls to its `get()` method during the processing of each request is important--and it counts in the final performance. One of the `sfOptimizer` optimization strategies is to replace configuration constants by their value--as long as these constants are not subject to change at runtime. That's the case, for instance, with the `sf_logging_enabled` parameter; when it is defined as `false`, the `sfOptimizer` transforms the previous code into the following:
  389. [php]
  390. if (0)
  391. {
  392. $this->getContext()->getLogger()->info('Been there');
  393. }
  394. And that's not all, because an evident test like the preceding one also gets optimized to an empty string.
  395. To apply the optimizations, you must first install the plug-in from [http://trac.symfony-project.org/wiki/sfOptimizerPlugin](http://trac.symfony-project.org/wiki/sfOptimizerPlugin) and then call the `optimize` task, specifying an application and an environment:
  396. > php symfony optimize frontend prod
  397. If you want to apply other optimization strategies to your code, the `sfOptimizer` plug-in might be a good starting place.
  398. Summary
  399. -------
  400. Symfony is already a very optimized framework and is able to handle high-traffic websites without a problem. But if you really need to optimize your application's performance, tweaking the configuration (whether the server configuration, the PHP configuration, or the application settings) will gain you a small boost. You should also follow good practices to write efficient model methods; and since the database is often a bottleneck in web applications, this point should require all your attention. Templates can also benefit from a few tricks, but the best boost will always come from caching. Finally, don't hesitate to look at existing plug-ins, since some of them provide innovative techniques to further speed up the delivery of web pages (`sfSuperCache`, `sfOptimizer`).