Detailed introduction to PHP application speed_PHP tutorial

WBOY
Release: 2016-07-20 10:57:46
Original
961 people have browsed it

We all know that fast speed is the biggest advantage of PHP. 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 face huge traffic, high-load applications, limited bandwidth, and various other factors that cause performance bottlenecks, you may ask yourself if there is anything you can do. Make your 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:

<ol class="dp-xml">
<li class="alt"><span><span class="attribute">zend_optimizer.optimization_level</span><span>=</span><span class="attribute-value">15</span><span> </span></span></li>
<li>
<span class="attribute">zend_extension</span><span>=</span><span class="attribute-value">"/path/to/ZendOptimizer.so"</span><span> </span>
</li>
<li class="alt">
<span class="attribute">zend_loader.enable</span><span>=</span><span class="attribute-value">Off</span><span> </span>
</li>
</ol>
Copy after login

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 method can really improve the performance of the application, 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 one mentioned earlier that provides us with PHP engine and free Zend Optimizer company. 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, there are many different methods available.

例如,来自Remote Communications的免费Apache模块mod_gzip就具有为支持这类内容编码的浏览器 压缩静态Web内容的能力。对于绝大多数静态Web内容,mod_gzip都非常有效。mod_gzip可以方便地编译到 Apache里面,也可以作为DSO使用。据Remote communications公司说,mod_gzip也能够压缩来自mod_php 、mod_perl等的动态内容。

我试了一次又一次,但看来还是不行。我看了许多关于mod_gzip的论坛和文章,看 来到了mod_gzip的下一个版本(可能是1.3.14.6f)这个问题有望得到解决。在此之前,我们可以在网站的静态 部分使用mod_gzip。

然而有时我们确实需要压缩动态内容,所以必须找找其他办法。有一种办法是使用class.gzip_encode.php ,这是一个可以用来压缩页面内容的PHP类,具体方法是在PHP脚本的开头和末尾调用该类的某些函数。如果要 在网站级实现这个方案,可以从php.ini文件的auto_prepend以及auto_append指令调用这些函数。

这种方法虽 然有效,但它无疑为高负载的网站带来了更多的开销。关于如何使用这个类的详细说明,请参见它的源代码。 它的源代码说明相当完善,作者告诉了你所有你必须知道的事情。

PHP 4.0.4有一个新的输出缓存句柄ob_gzhandler,它与前面的类相似,但用法不同。使用 ob_gzhandler时要在php.ini中加入的内容如下:

<ol class="dp-c"><li class="alt"><span><span>output_handler = ob_gzhandler ; </span></span></li></ol>
Copy after login

这行代码使得PHP激活输出缓存,并压缩它发送出去的所有内容。如果由于某种原因你不想在php.ini中加上这行代码,你还可以通过PHP源文件所在目录的.htaccess文件改变默认的服务器行为(不压缩),语法如下 :

<ol class="dp-c"><li class="alt"><span><span>php_value output_handler ob_gzhandler </span></span></li></ol>
Copy after login

或者是从PHP代码调用,如下所示:

<ol class="dp-c"><li class="alt"><span><span>ob_start(</span><span class="string">"ob_gzhandler"</span><span>); </span></span></li></ol>
Copy after login

采用输出缓存句柄的方法确实非常有效,而且不会给服务器带来什么特殊的负荷。但必须注意的是,Netscape Communicator对压缩图形的支持不佳,因此除非你能够保证所有用户都使用IE浏览器,否则你 应该禁止压缩JPEG和GIF图形。一般地,对于所有其他文件,这种压缩都有效,但建议你针对各种浏览器都分别 进行测试,特别是当你使用了特殊的插件或者数据查看器时这一点尤其重要。

使用前面介绍的各种技术,你能够显著地改善网站的性能表现,但应该注意的是:

PHP可能是、也可能不是性能瓶颈所在。务必仔细地观察每一个和应用性能有关的因素,比如数据库等。

单纯使用本文技术只能在一定限度之内提高Web服务器的性能。因此在归咎于PHP以及它的缓存之前,不妨看看是否应该升级服务器以及是否可以引入负载平衡技术(后者需要较大的投资)。

不要低估内容压缩的作用。虽然你在100 MB/s的LAN连接下看到Web应用响应非常迅速,但使用Modem 连接的用户不会,他们只会抱怨你那100 Kb的HTML页面实在过于庞大。

希望通过本文对于PHP的介绍,能够给你带来帮助。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445740.htmlTechArticle我们都知道,速度快是 PHP 最大的优点。一般情况下PHP总是具有足够的速度支持Web内容动态生成,许多时候你甚至无法找出比它更快的方法...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template