Performance Optimization for Android Web Serve

Performance Optimization for Android Web Serve

Through three different tests, we have a certain understanding of the server’s concurrency. This section will optimize the NGINX and PHP modules in Android system. In the process of adjusting the configuration, we can adjust the configuration of the NGINX module, and then adjust the configuration of the PHP module. These three types of requests are one include one relationship, the PHP request include HTTP requests, and the SQL request include PHP requests. So when adjusting the configuration parameters of NGINX, you can know whether performance is improved by testing the HTTP request. As for the adjustment of the PHP module, which is based on the HTTP request, through the PHP request and SQL test to understand whether the server performance is improved

.Nginx Module Optimization There are two particularly important parameters in the Nginx[10] profile –“worker” (the number of child processes) and “worker connection” (the number of concurrent connections per process). These two parameters have great influence on the performance of NGINX. Whether to write a log file “access_logs” to the Android system will also serve as an optimization factor. In the preliminary test work, because of the long time test, the log file is too big. It causes the system memory to be swallowed, and the following work cannot carry out. Android system is the operating system running on the mobile device, whose memory space is not large, and the log file will occupy a lot of memory, resulting in the system crash. So, the optimization suggestion is closing writing the log file of Ngnix (this paper only close writing the “access_logs”, “error_logs” is not closed). In general, Nginx’s child processes are set to a multiple of CPU core. The connection is also set to a multiple of 1024. When the experiment sets Nginx’s child process to 4 (twice the CPU core) and the connection is set to 2048, the total CPU utilization of the Nginx child process does not increase exponentially, only doubling the memory consumed from the child process. Since Nginx child process memory consumption is not large, this method can be used to optimize the Nginx module.

As we can see from the above figure, the optimized curveis twice as not optimized. In the initial conditions, the bottleneck of server for HTTP requests appears near 300.From the optimized results, the server’s HTTP request bottleneck point appears near 800, and the highest average response number doubled.

Through the comparison of response time, the average response time is significantly reduced after optimization. For the number of concurrent clients in the HTTP request within 1200, the server’s average response time is less than 2 seconds, which greatly improve the performance of concurrency.

B.PHP Module Optimization For the PHP module, there is a parameter is “max_execution_time” in the php.ini initial file, whichrepresents the maximum execution time of the PHP process.If the execution time exceeds this time, the process will be terminated directly and If the request is not processed, the server will return a 502 error. In the test, this parameter is increased to 180 to extend the PHP process execution time.Of course, modify this parameter can not significantly improve the performance of PHP. PHP need to increase the process of work, so that more requests are processed. In “php-fpm.conf” file, there are four parameters:“max_children”, “start_servers”,” min_spare_servers” and “max_spare_servers”. ” Max_children” represents the largest number of child processes that PHP can produce(Fig. 13). And “start_servers” represents the number of child processes should be generated when PHP start. “Min_spare_servers”and “max_spare_servers” are used to adjust the size of the process pool. If the number of child process is less than “min_spare_servers”, PHP will produce some new child process to handle PHP requests. If the idle process is more than “max_spare_servers”, PHP must kill those excess idle process. In fact, PHP requests and SQL requests have a lot of 502 errors in the previous section of the test. Now the main purpose of optimization is to minimize the 502 error.

If we combine these four parameters to test together, the test will be a lot of times. Therefore, we give an approximate range, then carry out the specific test work. According to the existing test experience and data, we can probably know that a PHP sub process may take up about 20M of memory. The test hardware system memory is 853M, so the max_children should be set to less than 42. The initialization value of max_children is 5, so the range should be between 5-42. The maximum number of sub processes is the most important parameter, which is the key to improve the performance of PHP concurrency. For the other three parameters, the combination of them is that “start_servers” and “max_spare_servers” should be half of “max_children”, and” min_spare_servers” should be half of “start_servers”. In this way, we can find the parameter combination that makes the server perform best.

When the number of clients within the 350, the response time is very short, which response basically within half a second. The server response speed is very fast. In the case of 350-550, the response time is longer than the initial case, except for the case of 10 child process. And the greater number of child processes, the longer response time will be. This shows that because of the increase in child processes the server encounters a bottleneck at this time, resulting in response time increased rather than reduced. With 5 seconds response time as the standard, when the maximum number of child processes is less than 20, the server can accept the number of concurrent clients is 600. But when the maximum number of child processes is more than 20, the server can accept the number of concurrent clients is 500. Through the analysis of the previous three charts, if you want the server error rate as low as possible, the “max_children” should be set as large as possible which is between the value of 30 and 40. And if you want to respond faster, “max_children” should be slightly reduced which is between 10-20.