Home  >  Article  >  类库下载  >  php + nginx website concurrency stress testing and optimization

php + nginx website concurrency stress testing and optimization

高洛峰
高洛峰Original
2016-10-20 13:41:543269browse

1. Testing tools:

Apache stress testing tool ab

ab is a performance testing tool for apache. You can only install the ab tool.

Ubuntu installation ab

apt-get install apache2-utils

centos installation ab

yum install httpd-tools

Detailed explanation of the parameters of ab

Format: ./ab [options] [http://]hostname[:port]/path

Parameters:

-n The number of requests executed during the test session. By default, only one request is performed.

-c The number of requests generated at one time. The default is one at a time.

-tThe maximum number of seconds for the test to take place. Its internal implicit value is -n 50000, which limits the test of the server to a fixed total time. By default, there is no time limit.

-p contains the file containing the data that needs to be POSTed.

-P provides BASIC certified trust to a transit agent. The username and password are separated by a : and sent in base64 encoding. This string is sent regardless of whether the server requires it (i.e., whether a 401 authentication requirement code was sent).

-T Content-type header information used by POST data.

-v sets the verbosity of displayed information - a value of 4 or greater will display header information, a value of 3 or greater can display response codes (404, 200, etc.), and a value of 2 or greater can display warnings and other information.

-V displays the version number and exits.

-w outputs the results in HTML table format. By default, it is a two-column width table with a white background.

-i perform HEAD request instead of GET.

-x sets the string of the

attribute.

-X uses a proxy server for requests.

-y string to set the

attribute.

-z sets the string of the

attribute.

-C Attach a Cookie to the request: OK. Its typical form is a parameter pair of name=value, and this parameter can be repeated.

-H appends additional header information to the request. The typical form of this parameter is a valid header line containing colon-separated field and value pairs (e.g., "Accept-Encoding:zip/zop;8bit").

-A provides BASIC certified trust to the server. The username and password are separated by a : and sent in base64 encoding. This string is sent regardless of whether the server requires it (i.e., whether a 401 authentication requirement code was sent).

-h shows usage.

-d Don't show "percentage served within XX [ms] table" message (provides support for previous versions).

-e produces a comma-delimited (CSV) file containing the corresponding percentage of time (in microseconds) required to process each corresponding percentage of requests (from 1% to 100%). Since this format has been "binarized", it is more useful than the 'gnuplot' format.

-g writes all test results to a 'gnuplot' or TSV (tab-delimited) file. This file can be easily imported into Gnuplot, IDL, Mathematica, Igor and even Excel. The first line is the title.

-i perform HEAD request instead of GET.

-k enables the HTTP KeepAlive function, which performs multiple requests in one HTTP session. By default, the KeepAlive function is not enabled.

-q If the number of requests processed is greater than 150, ab will output a progress count on stderr every time it processes approximately 10% or 100 requests. The -q flag suppresses these messages.

eg:

### POST /user/login

ab -n 100 -kc 10 -p user_login -T 'application/json' -H 'Accept-Encoding:gzip, deflate' -H 'accept-language:zh-Hans-CN;q=1, en-CN;q=0.9'  http://XXX/user/login


##### request data
文件 user_login 内容:
{"email":"ws65536@qq.com","password":"ws65536"}
### GET /default/index

ab -n 1000 -c 100 http://XXX/default/index

While testing, you can use the htop command on the server being tested to view the real-time usage of CPU and memory:

php + nginx website concurrency stress testing and optimization

For details on ab, please refer to: Apache performance testing tool ab usage detailed explanation

2. Configuration optimization

nginx has three steps to process PHP requests.

Step one: Accept the request, find it is a PHP request, and move to step two.

Step 2: Connect PHP-FPM’s fast-cgi through socket and let PHP-FPM process the request.

Step 3: Obtain the PHP-FPM processing result, add the http header, and return it to the client.

So, if we want to improve the PHP concurrency performance of nginx, we need to do these three steps.

1. Increase the number of concurrent connections of nginx (adjust worker_connections and worker_processes in nginx.conf).

worker_connections: The maximum number of connections that each worker process can handle (initiate) concurrently (including the number of all connections)

worker_processes: Indicates the number of processes to be opened by nginx. According to the official statement, one is usually enough. Open more. Several can reduce the impact of machine io. Generally, it is 1 to 2 times the total number of CPU cores on the current machine.

Reference:

nginx Concurrency issue thinking: worker_connections, worker_processes and max clients

Nginx working principle, optimization, loopholes

2.调大php-fpm的并发连接数(调php-fpm.conf 的pm.max_children等)。

本人用的是Ubuntu 14.04.4,PHP 5.5.9,以下目录结构对其他服务器环境可能不适用。

由于在 /etc/php5/fpm/php-fpm.conf 中并没有找到 max_children ,于是使用以下命令进行查找:

# 在/etc/php5/fpm 目录下查找所有文件,并依次在每个文件中查找"max_children"
sudo find /etc/php5/fpm -name * | xargs grep "max_children"

终于在 /etc/php5/fpm/pool.d/www.conf 中找到了 max_children 等相关配置。

关于具体配置,请参考以下内容:

(PHP手册)FastCGI 进程管理器(FPM)

php-fpm的配置和优化

PHP-FPM性能优化参考

PHP FPM php-fpm.conf设置详解

3.增加系统的最大文件数量限制(ulimit -n 65535)。

由于NGINX处理PHP请求的第二步需要通过socket的方式和PHP-FPM通信,它能新建的最大socket数受到系统最大打开文件数的限制。新装的Linux默认只有1024,所以必须增加系统最大打开文件数目。

ulimit -n 命令可以查看当前系统最大打开文件数。

ulimit -n 65535 可以将系统最大打开文件数临时修改为65535,然而退出登录后就会失效。

想要修改系统最大打开文件数,并永久生效:

vi /etc/security/limits.conf
        
# 添加如下的行
* soft noproc 11000
* hard noproc 11000
* soft nofile 65535
* hard nofile 65535

说明:

* 代表针对所有用户

noproc 是代表最大进程数

nofile 是代表最大文件打开数

具体配置参考一下内容:

ulimit -n 修改

linux有效修改max open files/ulimit -n


Statement:
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
Previous article:substr() function in phpNext article:substr() function in php

Related articles

See more