一、性能优化概述
- 系统结构瓶颈: 观察指标、压力测试
- 了解业务模式: 接口业务类型、 系统层次化结构
- 性能与安全: 性能好安全弱、安全好性能低
二、压力测试工具
工具: 压测宝、 ab
[root@my-node51 ~]# ab
ab: wrong number of arguments
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
-n requests Number of requests to perform
-c concurrency Number of multiple requests to make at a time
-t timelimit Seconds to max. to spend on benchmarking, This implies -n 50000
-s timeout Seconds to max. wait for each response, Default is 30 seconds
-b windowsize Size of TCP send/receive buffer, in bytes
-B address Address to bind to when making outgoing connections
-p postfile File containing data to POST. Remember also to set -T
-u putfile File containing data to PUT. Remember also to set -T
-T content-type Content-type header to use for POST/PUT data, eg. 'application/x-www-form-urlencoded', Default is 'text/plain'
-v verbosity How much troubleshooting info to print
-w Print out results in HTML tables
-i Use HEAD instead of GET
-x attributes String to insert as table attributes
-y attributes String to insert as tr attributes
-z attributes String to insert as td or th attributes
-C attribute Add cookie, eg. 'Apache=1234'. (repeatable)
-H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip', Inserted after all normal header lines. (repeatable)
-A attribute Add Basic WWW Authentication, the attributes are a colon separated username and password.
-P attribute Add Basic Proxy Authentication, the attributes are a colon separated username and password.
-X proxy:port Proxyserver and port number to use
-V Print version number and exit
-k Use HTTP KeepAlive feature
-d Do not show percentiles served table.
-S Do not show confidence estimators and warnings.
-q Do not show progress when doing more than 150 requests
-l Accept variable document length (use this for dynamic pages)
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-r Don't exit on socket receive errors.
-m method Method name
-h Display usage information (this message)
-I Disable TLS Server Name Indication (SNI) extension
-Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
-f protocol Specify SSL/TLS protocol (SSL2, TLS1, TLS1.1, TLS1.2 or ALL)
-E certfile Specify optional client certificate chain and private key
[root@my-node51 ~]# ab -n 50 -c 20 http://www.myshop.com/index.html
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.myshop.com (be patient).....done
Server Software: nginx/1.22.1
Server Hostname: www.myshop.com
Server Port: 80
Document Path: /index.html
Document Length: 153 bytes
Concurrency Level: 20
Time taken for tests: 0.006 seconds
Complete requests: 50
Failed requests: 0
Non-2xx responses: 50
Total transferred: 15150 bytes
HTML transferred: 7650 bytes
Requests per second: 8368.20 [#/sec] (mean)
Time per request: 2.390 [ms] (mean)
Time per request: 0.120 [ms] (mean, across all concurrent requests)
Transfer rate: 2476.14 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 1
Processing: 0 1 0.7 1 4
Waiting: 0 1 0.7 1 4
Total: 1 2 0.6 1 4
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% 2
75% 2
80% 2
90% 2
95% 2
98% 4
99% 4
100% 4 (longest request)
三、 影响性能指标
- 网络: 网络流量;网络是否丢包; 这些会影响http的请求与调用
- 系统: 硬件有没有磁盘损坏、磁盘速率; 系统负载、内存、系统稳定性
- 服务: 连接优化、请求优化; 根据业务形态做对应的服务设置
- 程序: 接口性能; 处理速度; 程序执行效率
- 数据库:
四、 系统性能优化
文件句柄
[root@my-node51 ~]# vi /etc/security/limits.conf
root soft nofile 65535
root hard nofile 65535
* soft nofile 25535
* hard nofile 25535
[root@my-node51 ~]# vi /etc/nginx/nginx.conf
worker_rlimit_nofile 45535;
[root@my-node51 ~]# cat /proc/1632/limits
Limit Soft Limit Hard Limit Units
Max processes 7068 7068 processes
Max open files 65535 65535 files
五、Nginx性能优化
- worker_processes auto;
- worker_cpu_affinity auto; # CPU亲和性, 减少进程之间不断频繁迁移,减少性能损耗
- worker_rlimit_nofile 45535; # 文件描述符
- worker_connections 10240; # 限制每个进程能处理多少个连接请求
- charset utf-8; # 字符集统一为utf-8
- sendfile on;
- tcp_nopush on; # 静态资源服务器,建议打开
- tcp_nodelay on; # 动态资源服务器,建议打开,需要打开 keepalive
- keepalive_timeout 65;
- gzip on;