PHP stress testing Optimization

Source: Internet
Author: User
The coding of a product does not indicate that the product can be used for user experience. It must also include testing, stress testing, and analysis, however, stress testing and analysis are often ignored before our products are launched. Since stress testing and analysis are very important, how should we proceed? This article mainly learns about the pressure test process through practical experience, and puts forward some suggestions

The coding of a product does not indicate that the product can be used for user experience. It must also include testing, stress testing, and analysis, however, stress testing and analysis are often ignored before our products are launched. Since stress testing and analysis are very important, how should we proceed? This article mainly learns about the pressure test process through practical experience, and puts forward some suggestions

Overview

The code of a product does not mean that the product can be used for user experience. It must also include testing, stress testing, and analysis. However, stress testing and analysis are often ignored before the product is launched. Since stress testing and analysis are very important, how should we proceed?

This article mainly learns about the stress testing process through practical experience, and puts forward some functional points that can be optimized on the PHP end, so as to help apply the optimal encoding method in the subsequent development process.

Tools

There is a saying that it is necessary to sharpen the knife without cutting the firewood by mistake. Good tools are required to make work more efficient, and tools must be used to further optimize system projects.

For PHP tool performance testing tools, you can apply xhprof or CI native time printing tools. For how to use the Xhprof tool, see http://blog.snsgou.com/post-278.html.

The CI native tool prints time to understand the application of the mark Method in the Benchmark class library in CI, it is mainly used to add all mark times of * _ start and * _ end to the debug page information of CI. Therefore, if I want to view the loading time of a class library, I only need to perform the following processing method:


$BM =& load_class('Benchmark', 'core');$BM->mark('base_classes_start');// load base classes$BM->mark('base_classes_end);


If you add them one by one, you will find it very painful. Because there are too many class libraries, you can apply tips here. You only need to add the processing function at the load model and load library, if you use native require, it will be relatively troublesome.

When adjusting the code above, remember to back up the complete code. After applying the above tools to the project code, we will analyze the interfaces to be optimized.

Code pre-stress testing Optimization

You need to solve the obvious problems before stress testing, for example, a request will load the class library multiple times, a request will request the same data in memcached multiple times, a request will request the same data in redis multiple times, and a request will request a new class libraries. These problems can be analyzed and solved before stress testing. The following is a self-detection table. You can perform relevant checks and optimization if necessary.

Optimization Problems

Analysis

Optimization Direction

Remarks

Load the class library multiple times

When you create a class library similar to redis, memcached, and mongodb, a connection is created in the constructor. Otherwise, multiple handles are created multiple times, as a result, there are many server handle connections, increasing the processing time of each connection.

If there is a persistent connection mode, try to use the persistent connection. If not, the static variables will be saved during each connection and the next time you need to re-create the connection, avoid Multiple handles from a single request.

Generally, class libraries that are frequently used do this kind of processing, but it is not ruled out that they have not been processed. Therefore, you can check them before stress testing.

Cache of a single data request multiple times

Under normal circumstances, everyone will think that since the cache is read, the efficiency should be very high, so in general, the cache content is directly read when data is required. This may cause a single data cache to be requested multiple times.

First, you need to know which data has been requested multiple times. Therefore, you can add logs to the cache get method to record the key value of each read cache, finally, analyze and check which data has been read multiple times. In this way, after the cache read is complete, a static array is used to save the read data. If the read data is read, it is directly obtained from the local memory without the need to remotely cache the data.

If the cache is in a static variable, it is equivalent to reading the local memory. If other cache tools are used, there will be slight gaps in connection creation and algorithms. In the case of high concurrency, this comparison will be infinitely magnified.

Disable debug logs

Generally, PHP systems will have debug logs, which may be affected when the stress test is not performed. Therefore, it is best to Disable debug to simulate the current network with only info and error logs.

Disable debug logs and retain info and error, which are consistent with those of the current network.

This part is also difficult to see when low concurrency occurs, but in high concurrency, it can be clearly seen that this will affect the system performance. Therefore, it must be closed before the stress test and the current network.

After the above three directions are optimized, we will start to prepare for the system pressure test and analysis.

Stress Testing and Analysis

Pay attention to the following points before stress testing:

1. Before stress testing, ensure that the login logic is removed and normal data requests can be entered;

2. Stress Testing analyzes the interface so that the same class of interface can avoid modifying the logic for stress testing together;

3. Design a pressure test data table and try to design and analyze the system's extreme processing capabilities, as shown in the following table;

Stress Testing concurrency

Pressure Test Request volume

Stress testing server

Throughput

Xhprof Standard

20

10000

236

510

Http: // xxx

50

10000

236

550

Http: // xxx

4. Apply xhprof hitting analysis. In order that xhprof does not affect the running of the current network, you can use the probability hitting method.

Stress Testing

If the preparations are complete in the early stage, the next pressure test will be relatively simple. You just need to run the process and check the data printed by xhprof, record the xhprof page url and the throughput of the stress test results. Note that the next stress test can be performed only when the load of the server to be tested is reduced to avoid the failure to achieve the best performance.

Stress Testing Data Analysis

1. Stress Testing Data Analysis

If the Pre-stress test data has been completed, then the pressure test table data is made into a line chart (execl can be used to draw a line chart ). Analyze the optimal concurrency of the system server and the maximum throughput under the optimal concurrency through a line chart, for example, the line chart below.

After the analysis is complete, we can see that the server is the maximum performance when about 100 is not, so we can analyze the system code running exceptions after 100 concurrency.

2,Xhprof Performance Analysis

After the preceding data analysis, you do not need to check the performance before the 100 concurrency, but analyze the xhprof result after the 100 is not the last, and carefully view the processing time and number of requests of each function. After record the data, check whether some interfaces can be optimized through source code comparison, or further reduce the number of requests to achieve optimization.

Stress Testing optimization points

After the stress test, we found that the connection and reading of mongodb will have a very big impact on the system. Therefore, I recorded the optimization scheme (increasing the cache time and correcting the code, when cache data is available, the mongodb class library is not loaded. If no cache is available, the class library is loaded, that is, the base class is modified. The mongodb class library is not directly loaded in the constructor, instead, add a separate method to load the mongodb class library ).

Perform stress testing after optimization

After the stress test is completed, the entire code is optimized (back up before rectification). After optimization, the Code logic is run again to avoid service exceptions after rectification, therefore, the stress test logic cannot be achieved.

After the code is complete, we will conduct the next round of pressure test comparison. The comparison at this time will show the significant changes after optimization, which will further improve our information, it makes us feel more satisfied with the pressure test process.

Summary

I have learned a lot of knowledge in the PHP stress testing and optimization process and will be more experienced in the subsequent development process, I also hope that this short summary will help you learn more.


For more information, see my personal information.

Http://blog.lovedan.cn/

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.