Web site performance analysis in PHP Web development

Source: Internet
Author: User
Tags php web development
Analysis of the performance of peripheral resources in PHP development

First, the back-end perimeter resources are the network and IO operations, storage services, middleware proxies, caches, and database accesses that are not related to the language itself while PHP is running, and in this article we analyze IO operations and middleware services.

Why the analysis of the performance of peripheral resources, should be based on the above three analysis? We can see the following domestic professional performance monitoring Tool OneAPM PHP WEB application background interception of the overview map, through this graph can see that the time spent in the total PHP response time, the database occupies 60% or more of the proportion, while the Memcached cache service, in this picture accounted for The response time is almost invisible.

One, IO operation

PHP language itself Despite the performance differences, but from the performance of the micro-analysis of PHP can also be seen, if only a single operation, the actual difference is very small, the previous experiment, more than 100,000 operations, only hundred MS difference, because the PHP language itself is operating memory, a memory access, about Around 50ns. The IO operation is disk access, which takes more than 5ms of time to access the disk. Only from this order of magnitude is 100,000 times times the gap, in fact, according to the experiment, there are also hundred levels of difference (sequential access and random access gap is huge, the actual two simultaneously, there will be disk cache, etc.).

So compared to the language itself, IO is more likely to become a bottleneck. First look at the performance differences that the IO operation brings.

A PHP script that runs through PHP commands

After emptying the disk cache with the following command:

Echo 3 | The sudo tee/proc/sys/vm/drop_caches code is exactly the same, but it runs 6 times times longer than the normal run time. Of course, this time is slow, and not only because the program itself is caused by the IO operation, and the larger the slow factor is in the CGI mode, every time the PHP script runs all need to load all modules, this load, but also with a large number of IO operations.

One more experiment, two pages of exactly the same function, one using MVC, to split the head and tail into separate templates (not using the template engine), and the intermediate logic to handle it using a separate Model class. The other only require the macro definition and database operations two files.

Use the command ab-c 40-n http://xxxxx/0929/zuche/carlist.php for stress testing,

In this page, the MVC version takes more than 6-8ms time. Although only a few additional files included, but significantly increased request latency, if the file operation itself more complex, such as file upload, detection, conversion, the delay will increase by an order of magnitude above. In the actual production use, also does not say that has the file operation, will certainly have the big delay, because like this example require, because of the existence of the disk cache and so on, the delay influence has been reduced a lot.

Second, the middleware agent

Before we formally use the middleware, let's compare the difference between using a database and not using a database, which is the same example above, where we take the data result set from the database to the direct result array setting, and in order to be structured, we adopt the MVC version. In order to make a more significant comparison of the previous test results, but also to eliminate some of the language itself slow factors, in this round of experiments, we use PHP7, the results are surprising. As with the database connection and the data read version, the PHP extension uses mysqli.

As a result of this page, only one database operation, the page structure is relatively simple, the language itself has a very large impact factor, PHP7 speed of twice times higher, the original average response time of 37-40ms, now is 14ms.

Even so, when the database is not read, there is a 4ms gap, although the number is not large, but for a total response time only 14ms of the application, this 4ms is very significant, and this is just a database query operation.

Let's take a look at how efficiency changes when you add a layer of database middleware. Because the author uses the middleware, currently does not support PHP7, so we also in the old version of PHP based on the comparison. Under the same server pressure, the version of the middleware used is more than one-times slower. As shown in.

From this example can be seen, the original PHP directly connected to the database, access to data operations, increased middleware, changed the first to the middleware, the middleware to the database, return is also, resulting in a sharp decline in speed (here has been removed from the middleware itself occupies the resources of the factors, in the original direct-attached version is Around 37-40ms).

Here also ask readers not to misunderstand, demonstrate the use of the speed of the middleware example, does not mean that in order to illustrate the middleware is not good, in the distributed environment, the use of middleware is very necessary. Instead, the external resources of the program are often important factors that affect performance, especially when the connection of external resources and the speed of data acquisition itself is not optimal.

For an analysis of IO operations and middleware services, the next chapter will analyze the impact of the database on the overall application performance.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.