一次Nginx+PHP+Mysql的并发测试经历
一、硬件环境
CPU:4核Intel(R) Xeon(R) CPU E5504 @ 2.00GHz
6G内存,120G硬盘
二、软件环境
Linux centos12 2.6.18-164.6.1.el5 #1 SMP x86_64 GNU/Linux
nginx-0.7.61.tar.gz
mysql-5.1.35.tar.gz
php-5.2.10.tar.gz
memcache-2.2.5.tgz
eaccelerator-0.9.5.3.tar.bz2
ZendOptimizer-3.3.9-linux-glibc23-x86_64.tar.gz
三、相关软件配置
1、Nginx配置文件
user www www; #worker_processes 1; #因为我是4核cpu所以设置为4 worker_processes 4; error_log /web/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; } http { include mime.types; default_type application/octet-stream; #charse gb2312; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 64k; client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; #增加超时,以避免502错误 fastcgi_connect_timeout 600; fastcgi_send_timeout 600; fastcgi_read_timeout 600; fastcgi_buffer_size 128k; fastcgi_buffers 8 128k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; #limit_zone crawler $binary_remote_addr 10m; server { listen 80; server_name test.abc.com; index phpinfo.php; root /web/www; #limit_conn crawler 20; #location /status { #stub_status on; #access_log off; #} location ~ .*/.(php|php5)?$ { fastcgi_pass unix:/tmp/php-cgi.sock; #fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; } location ~ .*/.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*/.(js|css)?$ { expires 12h; } log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; access_log /web/logs/access.log access; } server{ listen 80; server_name status.abc.com; location / { stub_status on; access_log off; } } server { listen 80; server_name admin.abc.com; index index.html index.htm index.php; root /web/www/phpmyadmin; location ~ .*/.(php|php5)?$ { fastcgi_pass unix:/tmp/php-cgi.sock; #fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; } location ~ .*/.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*/.(js|css)?$ { expires 12h; } access_log off; } server { listen 80; server_name uc.abc.com; index index.html index.htm index.php; root /web/www/qm/uc; location ~ .*/.(php|php5)?$ { fastcgi_pass unix:/tmp/php-cgi.sock; #fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; } location ~ .*/.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*/.(js|css)?$ { expires 12h; } access_log off; } server { listen 80; server_name www.abc.com; index index.php; root /web/www/qm/ss; location ~ .*/.(php|php5)?$ { fastcgi_pass unix:/tmp/php-cgi.sock; #fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; } location ~ .*/.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*/.(js|css)?$ { expires 12h; } access_log off; } server { listen 80; server_name sns.abc.com; index index.php; root /web/www/qm/sns; location ~ .*/.(php|php5)?$ { fastcgi_pass unix:/tmp/php-cgi.sock; #fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; } location ~ .*/.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*/.(js|css)?$ { expires 12h; } access_log off; } server { listen 80; server_name status.lnmp.org; location / { stub_status on; access_log off; } } }
2、php-fpm配置文件
<configuration> All relative paths in this config are relative to php's install prefix <section name="global_options"> Pid file <value name="pid_file">/usr/local/php/logs/php-fpm.pid</value> Error log file <value name="error_log">/usr/local/php/logs/php-fpm.log</value> Log level <value name="log_level">notice</value> When this amount of php processes exited with SIGSEGV or SIGBUS ... <value name="emergency_restart_threshold">10</value> ... in a less than this interval of time, a graceful restart will be initiated. Useful to work around accidental curruptions in accelerator's shared memory. <value name="emergency_restart_interval">1m</value> Time limit on waiting child's reaction on signals from master <value name="process_control_timeout">5s</value> Set to 'no' to debug fpm <value name="daemonize">yes</value> </section> <workers> <section name="pool"> Name of pool. Used in logs and stats. <value name="name">default</value> Address to accept fastcgi requests on. Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket' <value name="listen_address">/tmp/php-cgi.sock</value> <value name="listen_options"> Set listen(2) backlog <value name="backlog">-1</value> Set permissions for unix socket, if one used. In Linux read/write permissions must be set in order to allow connections from web server. Many BSD-derrived systems allow connections regardless of permissions. <value name="owner"></value> <value name="group"></value> <value name="mode">0666</value> </value> Additional php.ini defines, specific to this pool of workers. <value name="php_defines"> <value name="sendmail_path">/usr/sbin/sendmail -t -i</value> <value name="display_errors">1</value> </value> Unix user of processes <value name="user">www</value> Unix group of processes <value name="group">www</value> Process manager settings <value name="pm"> Sets style of controling worker process count. Valid values are 'static' and 'apache-like' <value name="style">static</value> Sets the limit on the number of simultaneous requests that will be served. Equivalent to Apache MaxClients directive. Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi Used with any pm_style. <value name="max_children">64</value> Settings group for 'apache-like' pm style <value name="apache_like"> Sets the number of server processes created on startup. Used only when 'apache-like' pm_style is selected <value name="StartServers">20</value> Sets the desired minimum number of idle server processes. Used only when 'apache-like' pm_style is selected <value name="MinSpareServers">10</value> Sets the desired maximum number of idle server processes. Used only when 'apache-like' pm_style is selected <value name="MaxSpareServers">35</value> </value> </value> The timeout (in seconds) for serving a single request after which the worker process will be terminated Should be used when 'max_execution_time' ini option does not stop script execution for some reason '0s' means 'off' <value name="request_terminate_timeout">0s</value> The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file '0s' means 'off' <value name="request_slowlog_timeout">0s</value> The log file for slow requests <value name="slowlog">logs/slow.log</value> Set open file desc rlimit <value name="rlimit_files">51200</value> Set max core size rlimit <value name="rlimit_core">0</value> Chroot to this directory at the start, absolute path <value name="chroot"></value> Chdir to this directory at the start, absolute path <value name="chdir"></value> Redirect workers' stdout and stderr into main error log. If not set, they will be redirected to /dev/null, according to FastCGI specs <value name="catch_workers_output">yes</value> How much requests each process should execute before respawn. Useful to work around memory leaks in 3rd party libraries. For endless request processing please specify 0 Equivalent to PHP_FCGI_MAX_REQUESTS <value name="max_requests">500</value> Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect. Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+) Makes sense only with AF_INET listening socket. <value name="allowed_clients">127.0.0.1</value> Pass environment variables like LD_LIBRARY_PATH All $VARIABLEs are taken from current environment <value name="environment"> <value name="HOSTNAME">$HOSTNAME</value> <value name="PATH">/usr/local/bin:/usr/bin:/bin</value> <value name="TMP">/tmp</value> <value name="TMPDIR">/tmp</value> <value name="TEMP">/tmp</value> <value name="OSTYPE">$OSTYPE</value> <value name="MACHTYPE">$MACHTYPE</value> <value name="MALLOC_CHECK_">2</value> </value> </section> </workers> </configuration>
3、php配置文件
在该配置文件中主要变化了一下部分
[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="128"
eaccelerator.cache_dir="/usr/local/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="300"
eaccelerator.shm_prune_period="120"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.keys ="disk_only"
eaccelerator.sessions = "disk_only"
eaccelerator.content ="disk_only"
[Zend]
zend_optimizer.optimization_level=15;
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/ZendOptimizer.so"
四、应用
安装了康盛的UCenter、UCHome、SupeSite套装,组合安装成功后,对整个系统进行测试。
五、系统测试
首先、使用了webbench对phpinfo.php进行了简单测试,测试结果如下:
测试工具 | WebBench | ||||||||
并发用户数 | 运行时间(s) | 每分钟加载页面数 | 每秒获得的字节数 | 成功的事务数 | 失败的事务数 | 请求页面 | 失败率 | 测试日期 | 测试主机 |
5,000 | 30 | 183,692 | 14,427,482 | 91,846 | - | phpinfo.php | 0.00% | 2009-11-19 | client |
5,000 | 30 | 179,010 | 10,362,354 | 89,505 | - | phpinfo.php | 0.00% | 2009-11-19 | client |
5,000 | 30 | 180,304 | 11,254,512 | 90,152 | - | phpinfo.php | 0.00% | 2009-11-19 | client |
5,000 | 30 | 177,216 | 8,831,614 | 88,608 | - | phpinfo.php | 0.00% | 2009-11-19 | client |
5,000 | 30 | 192,190 | 21,630,233 | 96,095 | - | phpinfo.php | 0.00% | 2009-11-19 | client |
top - 10:56:00 up 21:11, 1 user, load average: 3.00, 1.01, 0.98 |
Tasks: 243 total, 2 running, 241 sleeping, 0 stopped, 0 zombie |
Cpu(s): 12.3%us, 1.6%sy, 0.0%ni, 83.7%id, 0.0%wa, 0.1%hi, 2.3%si, 0.0%st |
Mem: 6103672k total, 1581720k used, 4521952k free, 83440k buffers |
Swap: 8159224k total, 0k used, 8159224k free, 456724k cached |
其次、使用了LoadRunner对supesite的登录Action进行了测试,测试结果如下: