Transfer: https: //www.cnblogs.com/nulige/p/9369700.html
1. Performance Optimization Overview
Before doing performance optimization, we need to consider the following
- 1. System Structure current bottleneck
- OUTCOME MEASURES
- pressure test
- 2. Understand the business model
- Interface type of business
- The system hierarchical structure
- 3. Performance and Safety
- Good safety performance weak
- Good low safety performance
2. stress testing tool
1. Install the pressure testing toolab
[root@nginx-lua ~]# yum install httpd-tools -y
2. use a decompression tool measuring
[root@nginx-lua ~]# ab -n 200 -c 2 http://127.0.0.1/ //-n总的请求次数 //-c并发请求数 //-k是否开启长连接
3. Configure Nginx
static website and tomcat
dynamic web environment
[root@nginx-lua conf.d]# cat jsp.conf
server {
server_name localhost;
listen 80;
location / {
root /soft/code;
try_files $uri @java_page;
index index.jsp index.html;
}
location @java_page{
proxy_pass http://192.168.56.20:8080; } } //分别给Nginx准备静态网站 [root@nginx-lua ~]# cat /soft/code/bgx.html <h1> Ab Load </h1> //给Tomcat准备静态网站文件 [root@tomcat-node1-20 ROOT]# cat /soft/tomcat-8080/webapps/ROOT/bgx.html <h1> Ab Load </h1>
4. Use ab
tool pressure test
//进行压力测试
[root@Nginx conf.d]# ab -n2000 -c2 http://127.0.0.1/bgx.html ... Server Software: nginx/1.12.2 Server Hostname: 127.0.0.1 Server Port: 80 Document Path: /bgx.html Document Length: 19 bytes Concurrency Level: 200 # 总花费总时长 Time taken for tests: 1.013 seconds # 总请求数 Complete requests: 2000 # 请求失败数 Failed requests: 0 Write errors: 0 Total transferred: 510000 bytes HTML transferred: 38000 bytes # 每秒多少请求/s(总请求出/总共完成的时间) Requests per second: 9333.23 [#/sec] (mean) # 客户端访问服务端, 单个请求所需花费的时间 Time per request: 101.315 [ms] (mean) # 服务端处理请求的时间 Time per request: 0.507 [ms] (mean, across all concurrent requests) # 判断网络传输速率, 观察网络是否存在瓶颈 Transfer rate: 491.58 [Kbytes/sec] received
5. nginx
Under the bgx
document is removed, the pressure will be measured again by the tomcat
processing
Concurrency Level: 200
Time taken for tests: 1.028 seconds Complete requests: 2000 Failed requests: 0 Write errors: 0 Total transferred: 510000 bytes HTML transferred: 38000 bytes Requests per second: 1945.09 [#/sec] (mean) Time per request: 102.823 [ms] (mean) Time per request: 0.514 [ms] (mean, across all concurrent requests) Transfer rate: 484.37 [Kbytes/sec] received
3. Impact Performance
Easy to affect the performance of the overall concern
- 1. Network
- Network traffic
- Whether the network packet loss
- These requests affect the calling of http
- 2. System
- There is no hardware disk corruption, disk rate
- System load, memory, system stability
- 3. Service
- Connection optimization, optimization request
- Do the corresponding service settings as business forms
- 4. Program
- Interface Performance
- Processing speed
- Program execution efficiency
- 5. Database
More or less have some association between each of the services and infrastructure services, we need the entire layered architecture, find the corresponding short-board systems or services, then optimize
4. The system performance optimization
- File handles, Linux everything is a file, the file handle is an index can be understood as
- With the file handle will call our process frequently increase
- There are restrictions on the default file handles, so that a process can not be unlimited calling
- Each process and the need to limit how much each service using the file handle
- File handle is required to be adjusted to optimize parameters
- Setting mode
- Modify global system
- Users modify locality
- Locality process modifications
vim /etc/security/limits.conf // root soft nofile 65535 root hard nofile 65535 // all users globally * soft nofile 25535 * hard nofile 25535 // for Nginx process worker_rlimit_nofile 65535 for the root user; // // root user soft remind // hard // nofile limit the number of files to configure the maximum size of items // 65535
Note: You can use the ulimit -a command to view open files 65535 system has a maximum number of open files value.
5.Nginx Performance Optimization
CPU
Between pro and reduce the frequent migration process will continue, reducing the performance loss
1. View the current CPU
physical status
[root@nginx ~]
# lscpu |grep "CPU(s)"
CPU(s): 24
On-line CPU(s) list: 0-23
NUMA node0 CPU(s): 0,2,4,6,8,10,12,14,16,18,20,22
NUMA node1 CPU(s): 1,3,5,7,9,11,13,15,17,19,21,23
|
//2颗物理cpu,没颗cpu12核心, 总共24核心
2. The Nginx worker
process tied to a different core
//
启动多少worker进程, 官方建议和cpu核心一致, 第一种绑定组合方式
#worker_processes 24;
#worker_cpu_affinity 000000000001 000000000010 000000000100 000000001000 000000010000 000000100000 000001000000 000010000000 000100000000 001000000000 010000000000 10000000000;
//
第二种方式
#worker_processes 2;
#worker_cpu_affinity 101010101010 010101010101;
//
最佳方式绑定方式
worker_processes auto;
worker_cpu_affinity auto;
|
3. Review the nginx worker
process is bound to the correspondingcpu
ps -eo pid,args,psr|grep [n]ginx
4. Nginx
General Optimization profile
[root@nginx ~]
# cat nginx.conf
user nginx;
worker_processes auto;
worker_cpu_affinity auto;
error_log
/var/log/nginx/error
.log warn;
pid
/run/nginx
.pid;
#调整至1w以上,负荷较高建议2-3w以上
worker_rlimit_nofile 65535;
events {
use epoll;
#限制每个进程能处理多少个连接请求,10240x16
worker_connections 10240;
}
http {
include
/etc/nginx/mime
.types;
default_type application
/octet-stream
;
# 统一使用utf-8字符集
charset utf-8;
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
/var/log/nginx/access
.log main;
# Core module
sendfile on;
# 静态资源服务器建议打开
tcp_nopush on;
# 动态资源服务建议打开,需要打开keepalived
tcp_nodelay on;
keepalive_timeout 65;
# Gzip module
gzip
on;
gzip_disable
"MSIE [1-6]\."
;
gzip_http_version 1.1;
# Virtal Server
include
/etc/nginx/conf
.d/*.conf;
}
|
1. Performance Optimization Overview
Before doing performance optimization, we need to consider the following
- 1. System Structure current bottleneck
- OUTCOME MEASURES
- pressure test
- 2. Understand the business model
- Interface type of business
- The system hierarchical structure
- 3. Performance and Safety
- Good safety performance weak
- Good low safety performance
2. stress testing tool
1. Install the pressure testing toolab
[root@nginx-lua ~]# yum install httpd-tools -y
2. use a decompression tool measuring
[root@nginx-lua ~]# ab -n 200 -c 2 http://127.0.0.1/ //-n总的请求次数 //-c并发请求数 //-k是否开启长连接
3. Configure Nginx
static website and tomcat
dynamic web environment
[root@nginx-lua conf.d]# cat jsp.conf
server {
server_name localhost;
listen 80;
location / {
root /soft/code;
try_files $uri @java_page;
index index.jsp index.html;
}
location @java_page{
proxy_pass http://192.168.56.20:8080; } } //分别给Nginx准备静态网站 [root@nginx-lua ~]# cat /soft/code/bgx.html <h1> Ab Load </h1> //给Tomcat准备静态网站文件 [root@tomcat-node1-20 ROOT]# cat /soft/tomcat-8080/webapps/ROOT/bgx.html <h1> Ab Load </h1>
4. Use ab
tool pressure test
//进行压力测试
[root@Nginx conf.d]# ab -n2000 -c2 http://127.0.0.1/bgx.html ... Server Software: nginx/1.12.2 Server Hostname: 127.0.0.1 Server Port: 80 Document Path: /bgx.html Document Length: 19 bytes Concurrency Level: 200 # 总花费总时长 Time taken for tests: 1.013 seconds # 总请求数 Complete requests: 2000 # 请求失败数 Failed requests: 0 Write errors: 0 Total transferred: 510000 bytes HTML transferred: 38000 bytes # 每秒多少请求/s(总请求出/总共完成的时间) Requests per second: 9333.23 [#/sec] (mean) # 客户端访问服务端, 单个请求所需花费的时间 Time per request: 101.315 [ms] (mean) # 服务端处理请求的时间 Time per request: 0.507 [ms] (mean, across all concurrent requests) # 判断网络传输速率, 观察网络是否存在瓶颈 Transfer rate: 491.58 [Kbytes/sec] received
5. nginx
Under the bgx
document is removed, the pressure will be measured again by the tomcat
processing
Concurrency Level: 200
Time taken for tests: 1.028 seconds Complete requests: 2000 Failed requests: 0 Write errors: 0 Total transferred: 510000 bytes HTML transferred: 38000 bytes Requests per second: 1945.09 [#/sec] (mean) Time per request: 102.823 [ms] (mean) Time per request: 0.514 [ms] (mean, across all concurrent requests) Transfer rate: 484.37 [Kbytes/sec] received
3. Impact Performance
Easy to affect the performance of the overall concern
- 1. Network
- Network traffic
- Whether the network packet loss
- These requests affect the calling of http
- 2. System
- There is no hardware disk corruption, disk rate
- System load, memory, system stability
- 3. Service
- Connection optimization, optimization request
- Do the corresponding service settings as business forms
- 4. Program
- Interface Performance
- Processing speed
- 程序执行效率
- 5.数据库
每个架构服务与服务之间都或多或少有一些关联, 我们需要将整个架构进行分层, 找到对应系统或服务的短板, 然后进行优化
4.系统性能优化
- 文件句柄, Linux一切皆文件,文件句柄可以理解为就是一个索引
- 文件句柄会随着我们进程的调用频繁增加
- 系统默认对文件句柄有限制,不能让一个进程无限的调用
- 需要限制每个进程和每个服务使用多大的文件句柄
- 文件句柄是必须要调整的优化参数
- 设置方式
- 系统全局性修改
- 用户局部性修改
- 进程局部性修改
vim /etc/security/limits.conf //针对root用户 root soft nofile 65535 root hard nofile 65535 //所有用户, 全局 * soft nofile 25535 * hard nofile 25535 //对于Nginx进程 worker_rlimit_nofile 65535; //root用户 //soft提醒 //hard限制 //nofile文件数配置项 //65535最大大小
备注:可以使用ulimit -a 命令查看open files 65535 系统最大打开文件数的值。
5.Nginx性能优化
CPU
亲和, 减少进程之间不断频繁迁移, 减少性能损耗
1.查看当前CPU
物理状态
[root@nginx ~]
# lscpu |grep "CPU(s)"
CPU(s): 24
On-line CPU(s) list: 0-23
NUMA node0 CPU(s): 0,2,4,6,8,10,12,14,16,18,20,22
NUMA node1 CPU(s): 1,3,5,7,9,11,13,15,17,19,21,23
|
//2颗物理cpu,没颗cpu12核心, 总共24核心
2.将Nginx worker
进程绑到不同的核心上
//
启动多少worker进程, 官方建议和cpu核心一致, 第一种绑定组合方式
#worker_processes 24;
#worker_cpu_affinity 000000000001 000000000010 000000000100 000000001000 000000010000 000000100000 000001000000 000010000000 000100000000 001000000000 010000000000 10000000000;
//
第二种方式
#worker_processes 2;
#worker_cpu_affinity 101010101010 010101010101;
//
最佳方式绑定方式
worker_processes auto;
worker_cpu_affinity auto;
|
3.查看nginx worker
进程绑定至对应cpu
ps -eo pid,args,psr|grep [n]ginx
4.Nginx
通用优化配置文件
[root@nginx ~]
# cat nginx.conf
user nginx;
worker_processes auto;
worker_cpu_affinity auto;
error_log
/var/log/nginx/error
.log warn;
pid
/run/nginx
.pid;
#调整至1w以上,负荷较高建议2-3w以上
worker_rlimit_nofile 65535;
events {
use epoll;
#限制每个进程能处理多少个连接请求,10240x16
worker_connections 10240;
}
http {
include
/etc/nginx/mime
.types;
default_type application
/octet-stream
;
# 统一使用utf-8字符集
charset utf-8;
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
/var/log/nginx/access
.log main;
# Core module
sendfile on;
# 静态资源服务器建议打开
tcp_nopush on;
# 动态资源服务建议打开,需要打开keepalived
tcp_nodelay on;
keepalive_timeout 65;
# Gzip module
gzip
on;
gzip_disable
"MSIE [1-6]\."
;
gzip_http_version 1.1;
# Virtal Server
include
/etc/nginx/conf
.d/*.conf;
}
|