Home >Backend Development >PHP Tutorial >An overview of PHP application speed-up_PHP tutorial
One of the biggest advantages of PHP is obviously its speed. In general, PHP is always fast enough to support dynamic generation of Web content, and many times you can't even find a faster method than it. However, when you have to deal with huge traffic, high-load applications, limited bandwidth, and various other factors that create performance bottlenecks, you may ask yourself if there is anything you can do to make the website run better . Perhaps as long as you add a very inconspicuous free module, your PHP application performance and Web server response speed will be significantly improved. This article discusses how to further improve the performance of PHP applications and give users a better browsing experience. This article explains various technologies to improve PHP application performance in three aspects (code optimization, caching, and content compression), and introduces well-known products in various fields.
Code Optimization
First let’s take a look at code optimization. Note that the code optimization here does not mean making the code more beautiful, because this is probably already known and there is no need to discuss it further; in addition, if you have already considered the speed issue, it is likely that you have already made changes to the PHP source code. Made some optimizations. However, some tools can automatically help us complete these complicated tasks, such as Zend Optimizer. Zend Optimizer is available for free from Zend Technologies, but you must agree to its licensing agreement and note that it is not distributed under the GPL. Zend Optimizer obtains the intermediate code generated by Zend Engine runtime compilation and optimizes it, so that the intermediate code has faster execution efficiency.
The installation method of Zend Optimizer is very simple. You only need to download the precompiled version provided for the platform you are using, add the following two lines of code to php.ini, and then restart the web server:
zend_optimizer.optimization_level=15
zend_extension="/path/to/ZendOptimizer.so"
zend_loader.enable=Off
The additional third line of code here is optional. Disabling zend_loader seems to make the Zend Optimizer a bit faster, so it's worth adding this line to php.ini. Note: You can disable zend_loader only if you are not using the Zend Encoder Runtime.
Caching
If you want to make your huge PHP application have better performance, using caching is also a good way. There are many caching solutions available, including: Zend Cache, APC, and Afterburner Cache.
All these products are "caching modules". When a request for a .php file first occurs, they save the PHP intermediate code in the web server's memory, and then respond to subsequent requests with the "compiled" version. This approach can really improve application performance because it reduces disk access to a minimum (the code has been read and parsed), and the code runs directly in memory, making the server respond to requests much faster. Of course, the caching module will also monitor changes in PHP source files and re-cache the page if necessary, thus preventing the user from getting pages that are still generated by outdated PHP code. Because caching modules can significantly reduce the load on the server and improve the response efficiency of PHP applications, they are very suitable for websites with heavy loads.
How to choose these caching products
Zend Cache is a commercial software from Zend Technologies, and Zend Technologies is the company mentioned earlier that provides us with PHP engine and free Zend Optimizer . Zend Cache is indeed well-deserved! For large PHP pages, you can feel that the speed will increase after the first run, and the server will have more available resources. Unfortunately this product is not free, but in some cases it can still be a good value.
Afterburner Cache is a free caching module from Bware Technologies. This product is currently in Beta version. Afterburner Cache looks similar to Zend Cache, but it doesn't improve performance as much as Zend Cache (yet), and it doesn't work with Zend Optimizer.
APC is the abbreviation of Alternative PHP Cache, which is another free caching module from Community Connect. The product is already stable enough for formal use, and it seems to improve the speed of responding to requests to a great extent.
Content Compression
We have discussed several ways to improve the performance of PHP applications. Let’s take a look at another important factor that makes viewers feel that the website is too slow: download speed. If the PHP application is running on an internal intranet and each client connects to the server at 100 MB/s, then download speed should not be an issue. However, if the server also needs to serve slow modem users, it is worth considering content compression. Most browsers support content compression with gzip according to IETF standards. This means that you can gzip the content and send it to the browser, which will decompress the data before displaying the page, and the entire process is completely transparent to the user. As for server-side content compression, many different methods are available.
For example, the free Apache module mod_gzip from Remote Communications has the ability to compress static web content for browsers that support this type of content encoding. For the vast majority of static web content, mod_gzip works very well. mod_gzip can be easily compiled into Apache and can also be used as a DSO. According to Remote Communications, mod_gzip can also compress dynamic content from mod_php, mod_perl, etc. I tried again and again, but it didn't seem to work. I read many forums and articles about mod_gzip, and it seems that in the next version of mod_gzip (probably 1.3.14.6f) this problem will be solved. Until then, we can use mod_gzip in the static parts of the website.
However, sometimes we really need to compress dynamic content, so we must find other ways. One way is to use class.gzip_encode.php, which is a PHP class that can be used to compress the content of the page by calling certain functions of the class at the beginning and end of the PHP script. If you want to implement this solution at the website level, you can call these functions from the auto_prepend and auto_append directives in the php.ini file. Although this method is effective, it undoubtedly brings more overhead to high-load websites. For detailed instructions on how to use this class, see its source code. Its source code description is quite complete, and the author tells you everything you need to know.
PHP 4.0.4 has a new output cache handler ob_gzhandler, which is similar to the previous class but has different usage. The following is added to php.ini when using ob_gzhandler:
output_handler = ob_gzhandler;
This line of code causes PHP to activate output caching and compress everything it sends out. . If for some reason you don't want to add this line of code to php.ini, you can also change the default server behavior (no compression) through the .htaccess file in the directory where the PHP source file is located, with the following syntax:
php_value output_handler ob_gzhandler
Or called from PHP code, as shown below:
ob_start("ob_gzhandler");
using the output cache handle The method is indeed very effective and does not place any special load on the server. However, it must be noted that Netscape Communicator has poor support for compressed graphics, so unless you can ensure that all users use IE browser, you should disable compressed JPEG and GIF graphics. In general, this compression works for all other files, but it is recommended that you test it separately for each browser, especially if you use special plug-ins or data viewers. This is especially important.
Using the various techniques introduced earlier, you can significantly improve the performance of your website, but it should be noted that:
PHP may or may not be the performance bottleneck. Be sure to carefully observe every factor related to application performance, such as databases, etc.
Simply using the technology in this article can only improve the performance of the web server within a certain limit. So before blaming PHP and its cache, consider whether the server should be upgraded and whether load balancing technology can be introduced (the latter requires a larger investment).
Don’t underestimate the power of content compression. Although you will see that the Web application responds very quickly under a 100 MB/s LAN connection, users using a Modem connection will not. They will only complain that your 100 Kb HTML page is too large.