nginx的性能测试及常用优化手段

nginx的性能测试及常用优化手段

一、nginx的性能测试及对比
1.环境准备
[root@test8_hadoop_kaf ~]# yum install -y httpd-tools
[root@test8_hadoop_kaf conf.d]# cat ab_xingneng.conf 
server {

    listen 80;

    server_name testserver01 es.chinasoft.com;

    root /opt/app;


    location / {
        root /opt/app/code/cache;
        try_files $uri @java_page;
    }

    location @java_page {
        proxy_pass http://127.0.0.1:9090;
    }

}

2.测试由nginx提供服务的静态资源
ab -n -c 2 http://es.chinasoft.com/jack.html
-n 总的请求数
-c 并发数
-k 是否开启长连接

[root@test8_hadoop_kaf ~]# ab -n 2000 -c 2 http://es.chinasoft.com/jack.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 es.chinasoft.com (be patient)
Completed 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requests


Server Software:        nginx/1.12.2    # 服务器程序
Server Hostname:        es.chinasoft.com    # 域名
Server Port:            80        # 端口

Document Path:          /jack.html    # 访问的页面
Document Length:        114 bytes    # 内容的长度

Concurrency Level:      2
Time taken for tests:   1.777 seconds    # 测试的时间
Complete requests:      2000        # 完成的请求数量
Failed requests:        0        # 失败的请求数量
Write errors:           0        # 写失败的格式
Total transferred:      708000 bytes    # 总共传输的字节数
HTML transferred:       228000 bytes    # html传输的字节数
Requests per second:    1125.37 [#/sec] (mean)    # 每秒的请求数
Time per request:       1.777 [ms] (mean)    # 每个请求耗费的时间
Time per request:       0.889 [ms] (mean, across all concurrent requests)    # 服务器响应请求花费的时间
Transfer rate:          389.05 [Kbytes/sec] received                # 传输速度

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.3      1       7
Processing:     1    1   0.6      1      16
Waiting:        1    1   0.6      1      16
Total:          1    2   0.7      2      17

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      2
  80%      2
  90%      2
  95%      2
  98%      3
  99%      4
 100%     17 (longest request)


3.测试java的动态接口程序
[root@test8_hadoop_kaf ROOT]# cat /data/yunva/test_tomcat8.0.37_9090/webapps/ROOT/test_sleep.jsp 
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
    <HEAD>
        <TITLE>JSP TEST PAGE</TITLE>
    </HEAD>
    <BODY>
        <%
            Thread.sleep(5000);
            Random rand = new Random();
            out.println("<h1>Random number:</h1>");
            out.println(rand.nextInt(99) + 100);
        %>
    </BODY>
</HTML>


[root@test8_hadoop_kaf bin]# curl -I http://es.chinasoft.com/test_sleep.jsp
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 17 Jan 2018 11:32:14 GMT
Content-Type: text/html;charset=utf-8
Connection: keep-alive
Set-Cookie: JSESSIONID=0B5052BEF444639173C600EB08A87980; Path=/; HttpOnly

# 对动态接口进行压测,可以看到每秒完成的请求数是0.36个,性能比较差
[root@test8_hadoop_kaf ROOT]# ab -n 20 -c 2 http://es.chinasoft.com/test_sleep.jsp
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 es.chinasoft.com (be patient).....done


Server Software:        nginx/1.12.2
Server Hostname:        es.chinasoft.com
Server Port:            80

Document Path:          /test_sleep.jsp
Document Length:        112 bytes

Concurrency Level:      2
Time taken for tests:   55.033 seconds
Complete requests:      20
Failed requests:        0
Write errors:           0
Total transferred:      6880 bytes
HTML transferred:       2240 bytes
Requests per second:    0.36 [#/sec] (mean)
Time per request:       5503.345 [ms] (mean)
Time per request:       2751.672 [ms] (mean, across all concurrent requests)
Transfer rate:          0.12 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.0      1       1
Processing:  5002 5002   0.2   5002    5003
Waiting:     5002 5002   0.2   5002    5003
Total:       5003 5003   0.3   5003    5003

Percentage of the requests served within a certain time (ms)
  50%   5003
  66%   5003
  75%   5003
  80%   5003
  90%   5003
  95%   5003
  98%   5003
  99%   5003
 100%   5003 (longest request)

4.测试由tomcat提供的静态资源
[root@test8_hadoop_kaf ROOT]# cd /opt/app/code/cache
[root@test8_hadoop_kaf cache]# mv jack.html jack.html.bak

这时访问jack.html就会转到tomcat去处理

[root@test8_hadoop_kaf conf.d]# ab -n 2000 -c 2 http://es.chinasoft.com/jack.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 es.chinasoft.com (be patient)
Completed 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requests


Server Software:        nginx/1.12.2
Server Hostname:        es.chinasoft.com
Server Port:            80

Document Path:          /jack.html
Document Length:        114 bytes

Concurrency Level:      2
Time taken for tests:   1.561 seconds
Complete requests:      2000
Failed requests:        0
Write errors:           0
Total transferred:      708000 bytes
HTML transferred:       228000 bytes
Requests per second:    1281.20 [#/sec] (mean)
Time per request:       1.561 [ms] (mean)
Time per request:       0.781 [ms] (mean, across all concurrent requests)
Transfer rate:          442.92 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.3      0       7
Processing:     1    1   0.5      1       9
Waiting:        1    1   0.5      1       9
Total:          1    2   0.6      1      16
ERROR: The median and mean for the initial connection time are more than twice the standard
       deviation apart. These results are NOT reliable.
WARNING: The median and mean for the total time are not within a normal deviation
        These results are probably not that reliable.

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      2
  80%      2
  90%      2
  95%      2
  98%      3
  99%      5
 100%     16 (longest request)

二、系统与nginx的性能优化

主要考虑的方面:
网络、系统、服务、程序、数据库、底层服务

系统与nginx的性能优化

文件句柄

linux\unix 一切皆文件,文件句柄就是一个索引

设置方式
系统全局性修改、用户局部性修改、进程局部性修改

soft是不强制限制,超过发邮件通知
hard是操作系统强制限制,请求会受到影响

# 针对用户root的限制
root hard nofile 1000000
root soft nofile 1000000

# 全局限制,针对所有用户的限制
* hard nofile 1000000
* soft nofile 1000000

针对Nginx进程的文件句柄限制
[root@test8_hadoop_kaf conf.d]# vim /etc/nginx/nginx.conf
worker_rlimit_nofile 655350;

cpu的性能
top
按住1 可以显示cpu的核心数


1.nginx绑定cpu,每个进程指定cpu

表示cpu的方法:
有多少核就用多少个0表示,4核用4个0表示 0000,8核用8个0表示 00000000

# 修改nginx配置,这里是4核的CPU,开放4个进程
[root@node1 ~]# cat /etc/nginx/nginx.conf

user  nginx;
worker_processes  4;
worker_cpu_affinity 0001 0010 0100 1000;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

worker_rlimit_nofile 35535;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/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" "$request_uri"';

    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;

    include /etc/nginx/conf.d/*.conf;
}

[root@node1 ~]# /usr/sbin/nginx -s reload
[root@node1 ~]# ps -ef|grep nginx
root      18106      1  0 01:54 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     20301  18106  0 16:46 ?        00:00:00 nginx: worker process
nginx     20302  18106  0 16:46 ?        00:00:00 nginx: worker process
nginx     20303  18106  0 16:46 ?        00:00:00 nginx: worker process
nginx     20304  18106  0 16:46 ?        00:00:00 nginx: worker process
root      20306  18433  0 16:46 pts/0    00:00:00 grep --color=auto nginx

# 可以看到不同的进程绑定在了不同的cpu上面
[root@node1 ~]# ps -eo pid,args,psr |grep [n]ginx
 18106 nginx: master process /usr/   2
 20301 nginx: worker process         0
 20302 nginx: worker process         1
 20303 nginx: worker process         2
 20304 nginx: worker process         3


2.让work1和work2共用一个cpu

修改配置
# cat nginx.conf

user  nginx;
worker_processes  4;
worker_cpu_affinity 0010 0010 0100 1000

[root@node1 nginx]# ps -ef|grep nginx
root      18106      1  0 01:54 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     20322  18106  0 16:51 ?        00:00:00 nginx: worker process
nginx     20323  18106  0 16:51 ?        00:00:00 nginx: worker process
nginx     20324  18106  0 16:51 ?        00:00:00 nginx: worker process
nginx     20325  18106  0 16:51 ?        00:00:00 nginx: worker process
root      20327  18433  0 16:51 pts/0    00:00:00 grep --color=auto nginx

# 可以看到20322和20323都绑定在了编号为1的处理器上
[root@node1 nginx]# ps -eo pid,args,psr |grep [n]ginx
 18106 nginx: master process /usr/   2
 20322 nginx: worker process         1
 20323 nginx: worker process         1
 20324 nginx: worker process         2
 20325 nginx: worker process         3


3.配置nginx的worker交叉绑定cpu,很少使用
其中一个worker可以使用1,3...的cpu
另外一个使用2,4的cpu

[root@node1 nginx]# ps -ef|grep nginx
root      18106      1  0 01:54 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     20339  18106  0 16:57 ?        00:00:00 nginx: worker process
nginx     20340  18106  0 16:57 ?        00:00:00 nginx: worker process
root      20342  18433  0 16:57 pts/0    00:00:00 grep --color=auto nginx
[root@node1 nginx]# ps -eo pid,args,psr |grep [n]ginx
 18106 nginx: master process /usr/   2
 20339 nginx: worker process         1
 20340 nginx: worker process         0

4.自动选择cpu
配置为auto 
nginx1.9以后新增的配置

# nginx配置
worker_processes  4;
worker_cpu_affinity auto;

[root@node1 nginx]# ps -ef|grep nginx
root      18106      1  0 01:54 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     20362  18106  0 17:03 ?        00:00:00 nginx: worker process
nginx     20363  18106  0 17:03 ?        00:00:00 nginx: worker process
nginx     20364  18106  0 17:03 ?        00:00:00 nginx: worker process
nginx     20365  18106  0 17:03 ?        00:00:00 nginx: worker process
root      20367  18433  0 17:03 pts/0    00:00:00 grep --color=auto nginx
[root@node1 nginx]# ps -eo pid,args,psr |grep [n]ginx
 18106 nginx: master process /usr/   2
 20362 nginx: worker process         0
 20363 nginx: worker process         1
 20364 nginx: worker process         2
 20365 nginx: worker process         3

建议worker数量和cpu核心一致即可


常用的优化配置项

[root@node1 nginx]# cat nginx.conf

user  nginx;
worker_processes  4;        # 4个进程,建议和cpu核心一致
worker_cpu_affinity auto;    # 自动绑定cpu

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

worker_rlimit_nofile 35535;    # 文件句柄限制
events {
    worker_connections  10240;    # 每个进程最大10240个连接
}

http {
    include       /etc/nginx/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" "$request_uri"';

    #access_log  /var/log/nginx/access.log  main;
    access_log  off;

    sendfile        on;    # 开启buffer缓存
    #tcp_nopush     on;
    #tcp_nodeny        on;
    keepalive_timeout 65; # 长连接超时时间

    gzip  on;    # gzip压缩
    gzip_disable "MSIE [1-6]\."; # ie6不兼容gzip,需要disable,否则可能无法显示网页内容
    gzip_http_version 1.1;

    include /etc/nginx/conf.d/*.conf;
}

 

posted @ 2018-01-18 17:15  reblue520  阅读(826)  评论(0编辑  收藏  举报