1.性能優化概述
在做性能優化前, 我們需要對如下進行考慮
- 1.當前系統結構瓶頸
- 觀察指標
- 壓力測試
- 2.了解業務模式
- 接口業務類型
- 系統層次化結構
- 3.性能與安全
- 性能好安全弱
- 安全好性能低
2.壓力測試工具
1.安裝壓力測試工具ab
[root@nginx-lua ~]# yum install httpd-tools -y
2.了解壓測工具使用方式
[root@nginx-lua ~]# ab -n 200 -c 2 http://127.0.0.1/
//-n總的請求次數
//-c並發請求數
//-k是否開啟長連接
3.配置Nginx
靜態網站與tomcat
動態網站環境
[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.使用ab
工具進行壓力測試
//進行壓力測試
[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
下的bgx
文件移走, 再次壓測會由tomcat
進行處理
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.影響性能指標
影響性能方便整體關注
- 1.網絡
- 網絡的流量
- 網絡是否丟包
- 這些會影響http的請求與調用
- 2.系統
- 硬件有沒有磁盤損壞,磁盤速率
- 系統負載、內存、系統穩定性
- 3.服務
- 連接優化、請求優化
- 根據業務形態做對應的服務設置
- 4.程序
- 接口性能
- 處理速度
- 程序執行效率
- 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; }