Performance Tuning

Performance of Web applications is affected by many factors. Databaseaccess, file system operations, network bandwidth are all potentialaffecting factors. Yii has tried in every aspect to reduce the performanceimpact caused by the framework. But still, there are many places in theuser application that can be improved to boost performance.

1. Enabling APC Extension

Enabling the PHP APCextension is perhaps theeasiest way to improve the overall performance of an application. Theextension caches and optimizes PHP intermediate code and avoids the timespent in parsing PHP scripts for every incoming request.

2. Disabling Debug Mode

Disabling debug mode is another easy way to improve performance. An Yiiapplication runs in debug mode if the constant YII_DEBUG is defined astrue. Debug mode is useful during development stage, but it would impactperformance because some components cause extra burden in debug mode. Forexample, the message logger may record additional debug information forevery message being logged.

3. Using yiilite.php

When the PHP APC extension isenabled, we can replace yii.php with a different Yii bootstrap file namedyiilite.php to further boost the performance of an Yii-powered application.

The file yiilite.php comes with every Yii release. It is the result ofmerging some commonly used Yii class files. Both comments and tracestatements are stripped from the merged file. Therefore, usingyiilite.php would reduce the number of files being included and avoidexecution of trace statements.

Note, using yiilite.php without APC may actually reduce performance,because yiilite.php contains some classes that are not necessarily usedin every request and would take extra parsing time. It is also observed thatusing yiilite.php is slower with some server configurations, even whenAPC is turned on. The best way to judge whether to use yiilite.php or notis to run a benchmark using the included hello world demo.

4. Using Caching Techniques

As described in the Caching section, Yiiprovides several caching solutions that may improve the performance of aWeb application significantly. If the generation of some data takes longtime, we can use the data caching approach toreduce the data generation frequency; If a portion of page remainsrelatively static, we can use the fragmentcaching approach to reduce its renderingfrequency; If a whole page remains relative static, we can use the pagecaching approach to save the rendering cost forthe whole page.

If the application is using Active Record, weshould turn on the schema caching to save the time of parsing databaseschema. This can be done by configuring theCDbConnection::schemaCachingDuration property to be a value greater than 0.

Besides these application-level caching techniques, we can also useserver-level caching solutions to boost the application performance. As amatter of fact, the APC caching wedescribed earlier belongs to this category. There are other servertechniques, such as Zend Optimizer,eAccelerator,Squid, to name a few.

5. Database Optimization

Fetching data from database is often the main performance bottleneck in aWeb application. Although using caching may alleviate the performance hit,it does not fully solve the problem. When the database contains enormousdata and the cached data is invalid, fetching the latest data could beprohibitively expensive without proper database and query design.

Design index wisely in a database. Indexing can make SELECT queries muchfaster, but it may slow down INSERT, UPDATE or DELETE queries.

For complex queries, it is recommended to create a database view for itinstead of issuing the queries inside the PHP code and asking DBMS to parsethem repetitively.

Do not overuse Active Record. Although ActiveRecord is good at modelling data in an OOPfashion, it actually degrades performance due to the fact that it needs tocreate one or several objects to represent each row of query result. Fordata intensive applications, using DAO ordatabase APIs at lower level could be a better choice.

Last but not least, use LIMIT in your SELECT queries. This avoidsfetching overwhelming data from database and exhausting the memoryallocated to PHP.

6. Minimizing Script Files

Complex pages often need to include many external JavaScript and CSS files. Because each file would cause one extra round trip to the server and back, we should minimize the number of script files by merging them into fewer ones. We should also consider reducing the size of each script file to reduce the network transmission time. There are many tools around to help on these two aspects.

For a page generated by Yii, chances are that some script files are rendered by components that we do not want to modify (e.g. Yii core components, third-party components). In order to minimizing these script files, we need two steps.

Note: The scriptMap feature described in the following has been available since version 1.0.3.

First, we declare the scripts to be minimized by configuring the scriptMap property of the clientScript application component. This can be done either in the application configuration or in code. For example,

  1. $cs=Yii::app()->clientScript;
  2. $cs->scriptMap=array(
  3. 'jquery.js'=>'/js/all.js',
  4. 'jquery.ajaxqueue.js'=>'/js/all.js',
  5. 'jquery.metadata.js'=>'/js/all.js',
  6. ......
  7. );

What the above code does is that it maps those JavaScript files to the URL /js/all.js. If any of these JavaScript files need to be included by some components, Yii will include the URL (once) instead of the individual script files.

Second, we need to use some tools to merge (and perhaps compress) the JavaScript files into a single one and save it as js/all.js.

The same trick also applies to CSS files.

We can also improve page loading speed with the help of Google AJAX Libraries API. For example, we can include jquery.js from Google servers instead of our own server. To do so, we first configure the scriptMap as follows,

  1. $cs=Yii::app()->clientScript;
  2. $cs->scriptMap=array(
  3. 'jquery.js'=>false,
  4. 'jquery.ajaxqueue.js'=>false,
  5. 'jquery.metadata.js'=>false,
  6. ......
  7. );

By mapping these script files to false, we prevent Yii from generating the code to include these files. Instead, we write the following code in our pages to explicitly include the script files from Google,

  1. <head>
  2. <?php echo CGoogleApi::init(); ?>
  3.  
  4. <?php echo CHtml::script(
  5. CGoogleApi::load('jquery','1.3.2') . "\n" .
  6. CGoogleApi::load('jquery.ajaxqueue.js') . "\n" .
  7. CGoogleApi::load('jquery.metadata.js')
  8. ); ?>
  9. ......
  10. </head>

原文: https://www.yiichina.com/doc/guide/1.0/topics.performance