nginx优化压力测试

简介:

 Nginx优化

Nginx以事件驱动(epoll的方式编写,所以有非常好的性能,同时也是一个非常高效的反向代理、负载平衡但是Nginx并不支持cgi方式运行,原因是可以减少因此带来的一些程序上的漏洞。所以必须使用FastCGI方式来执行PHP程序。

由于Nginx本身的一些优点,轻量,开源,易用,越来越多的公司使用nginx作为自己公司的web应用服务器,本文详细介绍nginx源码安装的同时并对nginx进行优化配置。

一、Nginx的优化

1、编译安装前优化

编译前的优化主要是用来修改程序名等等目的更改源码隐藏软件名称和版本号

安装zlib-develpcre-devel等依赖包

1
[root@CXW /]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel

下载nginx的源码包:http://nginx.org/download

解压源码包:

1
2
[root@CXW /]# tar zxf nginx- 1.10 . 2 .tar.gz 
[root@CXW /]# cd nginx- 1.10 . 2 /

隐藏软件名称和版本号

[root@CXW nginx-1.10.2]# vim src/core/nginx.h

//此行修改的是你想要的版本

#define NGINX_VERSION      "1.22.22"     //13

//此行修改的是你想修改的软件名称

#define NGINX_VER          "nginx/" NGINX_VERSION  //14

修改上面的信息,即可更改nginx显示版本。例如:

#define NGINX_VERSION      "7.0"

#define NGINX_VER          "IIS/" NGINX_VERSION

截图00.png

修改HTTP头信息中的connection字段,防止回显具体版本号

拓展:通用http通用头包含请求和响应消息都支持的头,通用头包含Cache-Control、 ConnectionDatePragmaTransfer-EncodingUpgradeVia。对通用头的扩展要求通讯双方都支持此扩展,如果存在不支持的通用头,一般将会作为实体头处理。那么也就是说有部分设备,或者是软件,能获取到connection,部分不能,要隐藏就要彻底!

[root@CXW nginx-1.10.2]# vim src/http/ngx_http_header_filter_module.c

修改前:

static char ngx_http_server_string[] = "Server: nginx" CRLF;  //49

修改后:

static char ngx_http_server_string[] = "Server: CXW" CRLF;

截图01.png

修改HTTP头信息中的connection字段,防止回显具体版本号

拓展:通用http通用头包含请求和响应消息都支持的头,通用头包含Cache-Control、 ConnectionDatePragmaTransfer-EncodingUpgradeVia。对通用头的扩展要求通讯双方都支持此扩展,如果存在不支持的通用头,一般将会作为实体头处理。那么也就是说有部分设备,或者是软件,能获取到connection,部分不能,要隐藏就要彻底!

[root@CXW nginx-1.10.2]# vim src/http/ngx_http_header_filter_module.c

修改前:

static char ngx_http_server_string[] = "Server: nginx" CRLF;  //49

修改后:

static char ngx_http_server_string[] = "Server: CXW" CRLF;

1
[root@CXW nginx- 1.10 . 2 ]# vim src/http/ngx_http_special_response.c

修改前

static u_char ngx_http_error_tail[] =     //29

"<hr><center>nginx</center>" CRLF

"</body>" CRLF

"</html>" CRLF

;

1
2
3
4
5
6
修改后
static  u_char ngx_http_error_tail[] =
"<hr><center>CXW</center>"  CRLF
"</body>"  CRLF
"</html>"  CRLF
;

截图02.png

2、安装ngnix

1
2
[root@CXW nginx- 1.10 . 2 ]# groupadd www   #添加www组
[root@CXW nginx- 1.10 . 2 ]# useradd -g www www -s /sbin/nologin  #创建nginx运行账户www并加入到www组,不允许www用户直接登录系统
1
2
root@CXW nginx- 1.10 . 2 ]#./configure --prefix=/nginx1. 10  -- with -http_dav_module -- with -http_stub_status_module -- with -http_addition_module -- with -http_sub_module -- with -http_flv_module -- with -http_mp4_module -- with -pcre -- with -http_ssl_module -- with -http_gzip_static_module --user=www --group=www
[root@CXW nginx- 1.10 . 2 ]# make && make install

相关选项说明

--with-http_dav_module  #增加PUT,DELETE,MKCOL:创建集合,COPYMOVE方法

--with-http_stub_status_module  #获取Nginx的状态统计信息

--with-http_addition_module   #作为一个输出过滤器,支持不完全缓冲,分部分相应请求

--with-http_sub_module     #允许一些其他文本替换Nginx相应中的一些文本

--with-http_flv_module     #提供支持flv视频文件支持

--with-http_mp4_module  #提供支持mp4视频文件支持,提供伪流媒体服务端支持

--with-http_ssl_module         #启用ngx_http_ssl_module

1
2
3
4
5
6
7
[root@CXW nginx- 1.10 . 2 ]# ln -s /usr/src/nginx1. 10 /sbin/nginx /usr/local/sbin/
[root@CXW nginx- 1.10 . 2 ]# nginx -t
nginx: the configuration file /usr/src/nginx1. 10 /conf/nginx.conf syntax  is  ok
nginx: configuration file /usr/src/nginx1. 10 /conf/nginx.conf test  is  successful
[root@CXW nginx- 1.10 . 2 ]# nginx
[root@CXW nginx- 1.10 . 2 ]# netstat -anpt | grep nginx
tcp         0       0  0.0 . 0.0 : 80               0.0 . 0.0 :*               LISTEN       7935 /nginx: master
1
2
[root@CXW nginx- 1.10 . 2 ]# nginx -h
nginx version: cxw/ 7.0

3nginx配置项优化

[root@CXW nginx-1.10.2]# ps -ef | grep nginx

root      7935     1  0 17:24 ?        00:00:00 nginx: master process nginx

www       7936  7935  0 17:24 ?        00:00:00 nginx: worker process

root      8125  4969  0 17:30 pts/1    00:00:00 grep --color=auto nginx

截图04.png

在这里我们还可以看到在查看的时候,work进程是nginx程序用户,但是master进程还是root其中,master是监控进程,也叫主进程,work是工作进程

(1):Nginx运行工作进程个数,一般我们设置CPU的核心或者核心数x2

如果不了解cpu的核数可以top命令之后按1也可以看出来,也可以查看/proc/cpuinfo文件#grep ^processor /proc/cpuinfo | wc -l

1
[root@CXW nginx- 1.10 . 2 ]# vim /usr/src/nginx1. 10 /conf/nginx.conf

截图05.png

1
2
3
4
[root@CXW nginx- 1.10 . 2 ]# /usr/src/nginx1. 10 /sbin/nginx -s reload 
[root@CXW nginx- 1.10 . 2 ]#  ps -aux | grep nginx | grep -v grep
root       7935   0.0   0.1   46008   1924  ?        Ss    17 : 24    0 : 00  nginx: master process nginx
www        8355   0.0   0.1   48520   2072  ?        S     17 : 39    0 : 00  nginx: worker process

Nginx运行CPU亲和力

比如4核配置

worker_processes  4;

worker_cpu_affinity 0001 0010 0100 1000

比如8核配置

worker_processes 8;

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

worker_processes最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,所以8个进程够用了。

Nginx最多可以打开文件数

worker_rlimit_nofile 65535;

这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。

注:

文件资源限制的配置可以在/etc/security/limits.conf设置,针对root/user等各个用户或者*代表所有用户来设置。

*     soft   nofile   65535

*     hard  nofile    65535

(3)开启高效传输模式

1
2
3
4
5
6
7
http {
include  mime.types;
default_type application/octet-stream;
……
sendfile on;
tcp_nopush on;
……

Include mime.types; //媒体类型,include 只是一个在当前文件中包含另一个文件内容的指令

default_type application/octet-stream;   //默认媒体类型足够

sendfile on//开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。

注意:如果图片显示不正常把这个改成off

tcp_nopush on必须在sendfile开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量将响应头和正文的开始部分一起发送,而不一个接一个的发送。

(4)连接超时时间

主要目的是保护服务器资源,CPU,内存,控制连接数,因为建立连接也是需要消耗资源的

1
2
3
4
5
6
7
8
9
10
11
12
keepalive_timeout  60 ;
tcp_nodelay on;
client_header_buffer_size 4k;
open_file_cache max= 102400  inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses  1 ;
client_header_timeout  15 ;
client_body_timeout  15 ;
reset_timedout_connection on;
send_timeout  15 ;
server_tokens off;
client_max_body_size 10m;

keepalived_timeout客户端连接保持会话超时时间,超过这个时间,服务器断开这个链接

tcp_nodelay;也是防止网络阻塞,不过要包涵在keepalived参数才有效

client_header_buffer_size 4k;
客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过 1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。
open_file_cache max=102400 inactive=20s;
这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件
数一致,inactive 是指经过多长时间文件没被请求后删除缓存。
open_file_cache_valid 30s;
这个是指多长时间检查一次缓存的有效信息。
open_file_cache_min_uses 1;
open_file_cache指令中的inactive 参数时间内文件的最少使用次数,如果超过这个数字,文
件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive 时间内一次没被使用,它将被移除。

client_header_timeout设置请求头的超时时间。我们也可以把这个设置低些,如果超过这个时间没有发送任何数据,nginx将返回request time out的错误

client_body_timeout设置请求体的超时时间。我们也可以把这个设置低些,超过这个时间没有发送任何数据,和上面一样的错误提示

reset_timeout_connection 告诉nginx关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间。

send_timeout响应客户端超时时间,这个超时时间仅限于两个活动之间的时间,如果超过这个时间,客户端没有任何活动,nginx关闭连接

server_tokens  并不会让nginx执行的速度更快,但它可以关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。

client_max_body_size上传文件大小限制

(5)fastcgi调优

1
2
3
4
5
6
7
8
9
10
fastcgi_connect_timeout      600 ;
fastcgi_send_timeout  600 ;
fastcgi_read_timeout  600 ;
fastcgi_buffer_size 64k;
fastcgi_buffers  4  64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_temp_path /usr/local/nginx1. 10 /nginx_tmp;
fastcgi_intercept_errors on;
fastcgi_cache_path /usr/local/nginx1. 10 /fastcgi_cache levels= 1 : 2  keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;

fastcgi_connect_timeout 600; #指定接到后端FastCGI的超时时间。

fastcgi_send_timeout 600; #FastCGI传送请求的超时时间。

fastcgi_read_timeout 600; #指定接收FastCGI应答的超时时间。

fastcgi_buffer_size 64k; #指定读取FastCGI应答第一部分需要用多大的缓冲区,默认的缓冲区大小为fastcgi_buffers指令中的每块大小,可以将这个值设置更小。

总结:

nginx的缓存功能有:proxy_cache / fastcgi_cache

proxy_cache的作用是缓存后端服务器的内容,可能是任何内容,包括静态的和动态。
fastcgi_cache的作用是缓存fastcgi生成的内容,很多情况是php生成的动态的内容。
proxy_cache缓存减少了nginx与后端通信的次数,节省了传输时间和后端宽带。
fastcgi_cache缓存减少了nginxphp的通信的次数,更减轻了php和数据库(mysql)的压力。

下面贴一个完整的内核优化设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
fs.file-max =  999999
net.ipv4.ip_forward =  0
net.ipv4.conf. default .rp_filter =  1
net.ipv4.conf. default .accept_source_route =  0
kernel.sysrq =  0
kernel.core_uses_pid =  1
net.ipv4.tcp_syncookies =  1
kernel.msgmnb =  65536
kernel.msgmax =  65536
kernel.shmmax =  68719476736
kernel.shmall =  4294967296
net.ipv4.tcp_max_tw_buckets =  6000
net.ipv4.tcp_sack =  1
net.ipv4.tcp_window_scaling =  1
net.ipv4.tcp_rmem =  10240  87380  12582912
net.ipv4.tcp_wmem =  10240  87380  12582912
net.core.wmem_default =  8388608
net.core.rmem_default =  8388608
net.core.rmem_max =  16777216
net.core.wmem_max =  16777216
net.core.netdev_max_backlog =  262144
net.core.somaxconn =  40960
net.ipv4.tcp_max_orphans =  3276800
net.ipv4.tcp_max_syn_backlog =  262144
net.ipv4.tcp_timestamps =  0
net.ipv4.tcp_synack_retries =  1
net.ipv4.tcp_syn_retries =  1
net.ipv4.tcp_tw_recycle =  1
net.ipv4.tcp_tw_reuse =  1
net.ipv4.tcp_mem =  94500000  915000000  927000000
net.ipv4.tcp_fin_timeout =  1
net.ipv4.tcp_keepalive_time =  30
net.ipv4.ip_local_port_range =  1024  65000

执行sysctl  -p使内核修改生效

(10)关于系统连接数的优化:

linux 默认值 open files1024

#ulimit -n

1024

说明server只允许同时打开1024个文件

使用ulimit -a 可以查看当前系统的所有限制值,使用ulimit -n 可以查看当前的最大打开文件数。

新装的linux 默认只有1024 ,当作负载较大的服务器时,很容易遇到error: too many open files。因此,需要将其改大

/etc/security/limits.conf最后增加:

*                soft    nofile          65535

*                hard    nofile          65535

*                soft    noproc          65535

*                hard    noproc          65535

二、部署LNMP

1、安装php

(1)解决依赖关系

1
[root@CXW /]#  yum -y install libxml2-devel libcurl-devel openssl-devel bzip2-devel

安装libmcrypt

1
2
3
[root@CXW src]# tar zxf libmcrypt- 2.5 . 7 .tar.gz 
[root@CXW src]# cd libmcrypt- 2.5 . 7 /
[root@CXW libmcrypt- 2.5 . 7 ]# ./configure --prefix=/usr/src/libmcrypt && make && make install

(2)编译安装php

1
2
[root@CXW src]# tar zxf php- 5.6 . 27 .tar.gz 
[root@CXW src]# cd php- 5.6 . 27 /

(3)提供php配置文件

1
[root@CXW php- 5.6 . 27 ]# cp php.ini-production /etc/php.ini

(4)php-fpm提供脚本

1
2
3
4
[root@CXW php- 5.6 . 27 ]#  cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@CXW php- 5.6 . 27 ]# chmod +x /etc/init.d/php-fpm 
[root@CXW php- 5.6 . 27 ]# chkconfig --add php-fpm
[root@CXW php- 5.6 . 27 ]# chkconfig php-fpm on

(5)提供php-fpm配置文件并编辑:

1
[root@CXW php- 5.6 . 27 ]# cp /usr/src/php5. 6 /etc/php-fpm.conf. default  /usr/src/php5. 6 /etc/php-fpm.conf

修改内容如下:

1
2
3
4
5
6
pid = run/php-fpm.pid
listen =  0.0 . 0.0 : 9000
pm.max_children = 300
pm.start_servers = 20
pm.min_spare_servers =  20
pm.max_spare_servers =  100

启动php-fpm服务:

1
[root@CXW php- 5.6 . 27 ]# systemctl restart php-fpm

nginx.conf文件的server中添加下面内容支持php

1
2
3
4
5
6
7
8
9
10
11
12
13
location ~ .*\.(php|php5)?$ {
             root html;
             fastcgi_pass  127.0 . 0.1 : 9000 ;
             fastcgi_index index.php;
             include  fastcgi.conf;
             fastcgi_cache cache_fastcgi;
             fastcgi_cache_valid  200  302  1h;
             fastcgi_cache_valid  301  1d;
             fastcgi_cache_valid any 1m;
             fastcgi_cache_min_uses  1 ;
             fastcgi_cache_use_stale error timeout invalid_header http_500;
             fastcgi_cache_key http: //$host$request_uri;
}

下面是nginx.conf的一个完整配置文件

user www www;

worker_processes  1;

worker_cpu_affinity 0001;

error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

pid        logs/nginx.pid;

 

 

events {

use epoll;

    worker_connections  65535;

    multi_accept on;

}

 

 

http {

include       mime.types;

    default_type  application/octet-stream;

 

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

 

    #access_log  logs/access.log  main;

 

    sendfile        on;

    tcp_nopush     on;

    keepalive_timeout  65;

    tcp_nodelay on;

    client_header_buffer_size 4k;

    open_file_cache max=102400 inactive=20s;

    open_file_cache_valid 30s;

    open_file_cache_min_uses 1;

    client_header_timeout 15;

    client_body_timeout 15;

    reset_timedout_connection on;

    send_timeout 15;

    server_tokens off;

    client_max_body_size 10m;

 

    fastcgi_connect_timeout     600;

    fastcgi_send_timeout 600;

    fastcgi_read_timeout 600;

    fastcgi_buffer_size 64k;

    fastcgi_buffers     4 64k;

    fastcgi_busy_buffers_size 128k;

    fastcgi_temp_file_write_size 128k;

    fastcgi_temp_path /usr/local/nginx1.10/nginx_tmp;

    fastcgi_intercept_errors on;

    fastcgi_cache_path /usr/local/nginx1.10/fastcgi_cache levels=1:2 keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;

 

gzip on;

    gzip_min_length  2k;

    gzip_buffers     4 32k;

    gzip_http_version 1.1;

    gzip_comp_level 6;

    gzip_types  text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;

    gzip_vary on;

    gzip_proxied any;

server {

        listen       80;

        server_name  www.benet.com;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location ~* ^.+\.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {

            valid_referers none blocked  www.benet.com benet.com;

            if ($invalid_referer) {

                #return 302  http://www.benet.com/img/nolink.jpg;

                return 404;

                break;

             }

             access_log off;

        }

        location / {

             root   html;

             index  index.php index.html index.htm;

        }

        location ~* \.(ico|jpe?g|gif|png|bmp|swf|flv)$ {

            expires 30d;

            #log_not_found off;

            access_log off;

        }

 

        location ~* \.(js|css)$ {

            expires 7d;

            log_not_found off;

            access_log off;

        }      

 

        location = /(favicon.ico|roboots.txt) {

            access_log off;

            log_not_found off;

        }

        location /status {

            stub_status on;

        }

        location ~ .*\.(php|php5)?$ {

            root html;

            fastcgi_pass 127.0.0.1:9000;

            fastcgi_index index.php;

            include fastcgi.conf;

            fastcgi_cache cache_fastcgi;

            fastcgi_cache_valid 200 302 1h;

            fastcgi_cache_valid 301 1d;

            fastcgi_cache_valid any 1m;

            fastcgi_cache_min_uses 1;

            fastcgi_cache_use_stale error timeout invalid_header http_500;

            fastcgi_cache_key http://$host$request_uri;

        }

        #error_page  404              /404.html;

 

        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

             root   html;

        }

   }

}

1
2
[root@CXW /]# /usr/src/nginx1. 10 /sbin/nginx -s reload
[root@CXW /]# ab -c  500  -n  50000  http: //www.benet.com/index.html

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking www.benet.com (be patient)

Completed 5000 requests

Completed 10000 requests

Completed 15000 requests

Completed 20000 requests

Completed 25000 requests

Completed 30000 requests

Completed 35000 requests

Completed 40000 requests

Completed 45000 requests

Completed 50000 requests

Finished 50000 requests

 

 

Server Software:        IIS

Server Hostname:        www.benet.com

Server Port:            80

 

Document Path:          /index.html

Document Length:        612 bytes

 

Concurrency Level:      500

Time taken for tests:   5.734 seconds

Complete requests:      50000

Failed requests:        0

Write errors:           0

Total transferred:      41800000 bytes

HTML transferred:       30600000 bytes

Requests per second:    8719.82 [#/sec] (mean)

Time per request:       57.341 [ms] (mean)

Time per request:       0.115 [ms] (mean, across all concurrent requests)

Transfer rate:          7118.92 [Kbytes/sec] received

 

Connection Times (ms)

min  mean[+/-sd] median   max

Connect:        1   25   4.2     25      38

Processing:     7   32   5.5     31      47

Waiting:        4   24   6.8     21      39

Total:         40   57   3.9     57      71

float

Percentage of the requests served within a certain time (ms)

  50%     57

  66%     59

  75%     59

  80%     60

  90%     61

  95%     62

  98%     63

  99%     64

 100%     71 (longest request)

第二次压力测试,比较两次的差异

1
[root@www ~]# ab -c  1000  -n  100000  http: //www.benet.com/index.html

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking www.benet.com (be patient)

Completed 10000 requests

Completed 20000 requests

Completed 30000 requests

Completed 40000 requests

Completed 50000 requests

Completed 60000 requests

Completed 70000 requests

Completed 80000 requests

Completed 90000 requests

Completed 100000 requests

Finished 100000 requests

 

 

Server Software:        IIS

Server Hostname:        www.benet.com

Server Port:            80

 

Document Path:          /index.html

Document Length:        612 bytes

 

Concurrency Level:      1000

Time taken for tests:   12.010 seconds

Complete requests:      100000

Failed requests:        0

Write errors:           0

Total transferred:      83600000 bytes

HTML transferred:       61200000 bytes

Requests per second:    8326.49 [#/sec] (mean)

Time per request:       120.099 [ms] (mean)

Time per request:       0.120 [ms] (mean, across all concurrent requests)

Transfer rate:          6797.80 [Kbytes/sec] received

 

Connection Times (ms)

min  mean[+/-sd] median   max

Connect:        1   53   8.9     53      82

Processing:    17   67  11.4     66      98

Waiting:        0   49  14.3     43      84

Total:         70  119   6.5    120     140

 

Percentage of the requests served within a certain time (ms)

  50%    120

  66%    122

  75%    123

  80%    124

  90%    126

  95%    128

  98%    129

  99%    130

 100%    140 (longest request)

(5)xcache加速php

安装xcache

1
2
3
[root@CXW src]# wget http: //xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz
[root@CXW src]# cd xcache- 3.2 . 0 /
[root@CXW xcache- 3.2 . 0 ]# /usr/src/php5. 6 /bin/phpize

图片1.png

1
2
3
[root@CXW xcache- 3.2 . 0 ]# ./configure --enable-xcache --enable-xcache-coverager --enable-xcache-optimizer -- with -php-config=/usr/src/php5. 6 /bin/php-config 
[root@CXW xcache- 3.2 . 0 ]# make && make install
Installing shared extensions:

安装完成之后,出现下面的界面,记住以下路径,后面会用到

1
2
3
/usr/src/php5. 6 /lib/php/extensions/no-debug-non-zts- 20131226 /
[root@CXW xcache- 3.2 . 0 ]# touch /tmp/xcache
[root@CXW xcache- 3.2 . 0 ]# chmod  777  /tmp/xcache

3拷贝xcache后台管理程序到网站根目录

1
[root@CXW xcache- 3.2 . 0 ]# cp -r htdocs/ /usr/src/nginx1. 10 /html/xcache

4配置php支持xcache

vi / etc/php.ini #编辑配置文件,在最后一行添加以下内容

[xcache-common]

extension = /usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/xcache.so

[xcache.admin]

xcache.admin.enable_auth = Off

[xcache]

xcache.shm_scheme ="mmap"

xcache.size=60M

xcache.count =1

xcache.slots =8K

xcache.ttl=0

xcache.gc_interval =0

xcache.var_size=64M

xcache.var_count =1

xcache.var_slots =8K

xcache.var_ttl=0

xcache.var_maxttl=0

xcache.var_gc_interval =300

xcache.test =Off

xcache.readonly_protection = Off

xcache.mmap_path ="/tmp/xcache"

xcache.coredump_directory =""

xcache.cacher =On

xcache.stat=On

xcache.optimizer =Off

[xcache.coverager]

xcache.coverager =On

xcache.coveragedump_directory =""

测试

1
[root@CXW xcache- 3.2 . 0 ]# systemctl restart php-fpm

浏览器打开网站根目录下面的xcache

http://http://www.benet.com/xcache可以看到如下页面:

截图06.png

测试对php动态页面的压力测试

[root@CXW xcache-3.2.0]# ab -c 1000 -n 100000 http://www.benet.com/test.php

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking www.benet.com (be patient)

Completed 10000 requests

Completed 20000 requests

Completed 30000 requests

Completed 40000 requests

Completed 50000 requests

Completed 60000 requests

Completed 70000 requests

Completed 80000 requests

Completed 90000 requests

Completed 100000 requests

Finished 100000 requests

 

 

Server Software:        IIS

Server Hostname:        www.benet.com

Server Port:            80

 

Document Path:          /test.php

Document Length:        85102 bytes

 

Concurrency Level:      1000

Time taken for tests:   13.686 seconds

Complete requests:      100000

Failed requests:        0

Write errors:           0

Total transferred:      8527900000 bytes

HTML transferred:       8510200000 bytes

Requests per second:    7306.71 [#/sec] (mean)

Time per request:       136.861 [ms] (mean)

Time per request:       0.137 [ms] (mean, across all concurrent requests)

Transfer rate:          608504.46 [Kbytes/sec] received

 

Connection Times (ms)

min  mean[+/-sd] median   max

Connect:        0   17   5.5     17      81

Processing:    21  119  10.8    121     140

Waiting:        1   17   6.7     16      68

Total:         50  136   8.1    137     151

 

Percentage of the requests served within a certain time (ms)

  50%    137

  66%    139

  75%    140

  80%    141

  90%    143

  95%    144

  98%    146

  99%    148

 100%    151 (longest request)

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking www.benet.com (be patient)

Completed 10000 requests

Completed 20000 requests

Completed 30000 requests

Completed 40000 requests

Completed 50000 requests

Completed 60000 requests

Completed 70000 requests

Completed 80000 requests

Completed 90000 requests

Completed 100000 requests

Finished 100000 requests

 

 

Server Software:        IIS

Server Hostname:        www.benet.com

Server Port:            80

 

Document Path:          /test.php

Document Length:        85102 bytes

 

Concurrency Level:      1000

Time taken for tests:   13.686 seconds

Complete requests:      100000

Failed requests:        0

Write errors:           0

Total transferred:      8527900000 bytes

HTML transferred:       8510200000 bytes

Requests per second:    7306.71 [#/sec] (mean)

Time per request:       136.861 [ms] (mean)

Time per request:       0.137 [ms] (mean, across all concurrent requests)

Transfer rate:          608504.46 [Kbytes/sec] received

 

Connection Times (ms)

min  mean[+/-sd] median   max

Connect:        0   17   5.5     17      81

Processing:    21  119  10.8    121     140

Waiting:        1   17   6.7     16      68

Total:         50  136   8.1    137     151

 

Percentage of the requests served within a certain time (ms)

  50%    137

  66%    139

  75%    140

  80%    141

  90%    143

  95%    144

  98%    146

  99%    148

 100%    151 (longest request)

 






     本文转自柴鑫旺 51CTO博客,原文链接:http://blog.51cto.com/chaixinwang/2071556,如需转载请自行联系原作者




相关文章
|
17天前
|
敏捷开发 监控 前端开发
自动化测试中Selenium Grid的优化策略
【4月更文挑战第12天】 在持续集成和持续部署(CI/CD)流程日益重要的今天,自动化测试成为确保软件质量的关键步骤。Selenium Grid作为实现并行测试的有效工具,其性能优化直接关系到测试效率的提升。本文将探讨针对Selenium Grid的优化策略,包括环境配置、脚本设计及资源管理等,旨在为软件测试工程师提供实用的参考,以缩短测试周期,提高自动化测试的整体效能。
23 3
|
17天前
|
安全 Linux 测试技术
提升龙蜥内核测试能力!探究持续性模糊测试优化实践
清华大学软件学院对Anolis OS使用靶向模糊测试方法将测试工作引向修改的代码,进而提高对业务代码的测试能力。
|
17天前
|
测试技术 API Python
Appium控件交互策略:优化自动化测试效率的关键方法
该文介绍了如何使用Selenium与APP进行交互,包括点击、输入和状态判断等操作。例如,通过element.click()点击控件,element.send_keys()输入文本,以及element.is_displayed()检查元素是否可见。还展示了如何获取元素属性,如resource-id、text和class,并提供了Python代码示例来定位并操作APP元素,如滑动条的显示、可点击性检测及点击滑动条中心位置。在编写测试脚本时,应注意元素定位和状态验证以确保测试稳定性。
25 1
|
17天前
|
运维 Kubernetes 测试技术
容器技术:优化软件测试流程的利器
本文介绍了容器技术的概念、优势和历史发展,对比了容器与虚拟机的区别,并提及了Docker和Kubernetes等常见容器技术。容器作为轻量级虚拟化工具,提供高效、灵活的应用部署方式,广泛应用于软件开发、云计算和微服务架构。随着技术演进,容器将在边缘计算、人工智能等领域发挥更大作用,推动行业变革。
22 3
|
17天前
|
机器学习/深度学习 数据采集 人工智能
【专栏】AI在软件测试中的应用,如自动执行测试用例、识别缺陷和优化测试设计
【4月更文挑战第27天】本文探讨了AI在软件测试中的应用,如自动执行测试用例、识别缺陷和优化测试设计。AI辅助工具利用机器学习、自然语言处理和图像识别提高效率,但面临数据质量、模型解释性、维护更新及安全性挑战。未来,AI将更注重用户体验,提升透明度,并在保护隐私的同时,通过联邦学习等技术共享知识。AI在软件测试领域的前景广阔,但需解决现有挑战。
|
5天前
|
机器学习/深度学习 敏捷开发 测试技术
深入理解自动化测试:框架选择与实践挑战利用机器学习技术优化数据中心冷却系统
【5月更文挑战第27天】 在现代软件开发周期中,自动化测试已成为确保产品质量和加快市场投放的关键步骤。本文深入探讨了自动化测试的框架选择问题,并剖析了实施过程中面临的挑战及其解决方案。通过比较不同测试框架的特点,我们旨在为读者提供一套明确的指导原则,帮助他们根据项目需求做出恰当的技术决策。同时,文中还分享了实际案例和最佳实践,以期帮助开发团队克服实施自动化测试时可能遇到的障碍。
|
5天前
|
安全 数据管理 测试技术
网络安全与信息安全:防范漏洞、加强加密与提升安全意识深入探索自动化测试框架的设计原则与实践应用化测试解决方案。文章不仅涵盖了框架选择的标准,还详细阐述了如何根据项目需求定制测试流程,以及如何利用持续集成工具实现测试的自动触发和结果反馈。最后,文中还将讨论测试数据管理、测试用例优化及团队协作等关键问题,为读者提供全面的自动化测试框架设计与实施指南。
【5月更文挑战第27天】 在数字化时代,网络安全与信息安全已成为维护国家安全、企业利益和个人隐私的重要环节。本文旨在分享关于网络安全漏洞的识别与防范、加密技术的应用以及提升安全意识的重要性。通过对这些方面的深入探讨,我们希望能为读者提供一些实用的建议和策略,以应对日益严峻的网络安全挑战。 【5月更文挑战第27天】 在软件开发周期中,自动化测试作为保障软件质量的关键步骤,其重要性日益凸显。本文旨在剖析自动化测试框架设计的核心原则,并结合具体案例探讨其在实际应用中的执行策略。通过对比分析不同测试框架的优缺点,我们提出一套高效、可扩展且易于维护的自动
|
6天前
|
机器学习/深度学习 人工智能 自然语言处理
2024年5月大语言模型论文推荐:模型优化、缩放到推理、基准测试和增强性能
本文汇总了2024年5月发布的七篇重要大语言模型论文,涉及模型优化、缩放、推理及性能增强。
22 2
|
17天前
|
缓存 负载均衡 安全
深入探索Nginx高性能Web服务器配置与优化
【5月更文挑战第7天】本文深入探讨了Nginx的配置与优化,重点介绍了基础配置参数如`worker_processes`、`worker_connections`和`keepalive_timeout`,以及优化策略,包括使用epoll事件驱动模型、开启gzip压缩、启用缓存、负载均衡和安全配置。此外,还提到了性能调优工具,如ab、nginx-stats和nmon,以助于提升Nginx的性能和稳定性。
|
17天前
|
敏捷开发 数据管理 测试技术
探索自动化测试在持续集成环境中的优化策略
【5月更文挑战第6天】 本文旨在深入剖析自动化测试在持续集成(CI)环境中所面临的挑战,并提出一系列创新的优化策略。通过对现代软件开发过程中自动化测试角色的分析,我们揭示了在快速迭代和部署的背景下,如何通过改进测试框架、选择合适的测试工具、以及实施数据驱动测试等手段来提高测试效率和准确性。文章不仅聚焦于技术层面的解决方案,还探讨了团队协作和流程管理对提升自动化测试效能的重要性。