openresty 性能优化,
openresty 性能优化,
openresty 性能优化
ab 压测
命令格式:ab [options] url
huli@hudeMacBook-Pro ~ % ab -h 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 * 响应超时时间,默认30s -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 (TLS1, TLS1.1, TLS1.2 or ALL) -E certfile Specify optional client certificate chain and private key
使用示例
huli@hudeMacBook-Pro ~ % ab -n 100 https://www.taobao.com/ This is ApacheBench, Version 2.3 <$Revision: 1879490 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.taobao.com (be patient).....done Server Software: Tengine Server Hostname: www.taobao.com Server Port: 443 SSL/TLS Protocol: TLSv1.2,ECDHE-ECDSA-AES128-GCM-SHA256,256,128 Server Temp Key: ECDH X25519 253 bits TLS Server Name: www.taobao.com Document Path: / Document Length: 86840 bytes Concurrency Level: 1 # 并发数 Time taken for tests: 24.865 seconds # 完成测试花费的时间 Complete requests: 100 # 完成发送的请求数 Failed requests: 0 # 失败的请求数 Total transferred: 8796159 bytes # 总传输数据量大小 HTML transferred: 8684000 bytes # html数据量大小 Requests per second: 4.02 [#/sec] (mean) # 平均每秒执行的请求数 Time per request: 248.646 [ms] (mean) # 平均每个请求执行时间 Time per request: 248.646 [ms] (mean, across all concurrent requests) # 所有并发请求,平均每个请求执行时间 Transfer rate: 345.47 [Kbytes/sec] received # 传输速率 Connection Times (ms) # 连接耗时:连接、处理、响应等待 min mean[+/-sd] median max Connect: 96 154 137.2 120 1034 Processing: 74 95 36.1 86 396 Waiting: 26 63 38.6 58 372 Total: 178 248 154.4 207 1125 Percentage of the requests served within a certain time (ms) # 请求执行时间分布 50% 207 # 50%的请求在207ms内执行完成 66% 218 75% 235 80% 245 90% 328 95% 467 98% 1117 99% 1125 100% 1125 (longest request)
连接数优化
*************
连接数限制
连接数超出系统限制,通常会报出如下错误:
too many open files
用户连接数:临时操作
# 查看当前用户描述符限制 [root@7623085b4b89 /]# ulimit -n 1048576 # 临时修改限制,重启后失效 [root@7623085b4b89 /]# ulimit -n 200000 [root@7623085b4b89 /]# ulimit -n 200000
用户连接数:永久操作
vi /etc/ssh/sshd_config, 添加如下内容: * hard nofile 102400 * soft nofile 102400 保存后退出,重新登录,即可生效 # 相关说明 *:表示所有用户 nofile:最大文件打开数
系统连接数(所有用户连接数总和):临时操作
设置连接数:sysctl -w fs.file-max=102400 查看连接数:sysctl fs.file-max # 示例 [root@ab103f74b163 /]# sysctl -w fs.file-max=102400 fs.file-max = 102400 [root@ab103f74b163 /]# sysctl fs.file-max fs.file-max = 102400
系统连接数:永久操作
# 设置文件描述符术 vi /etc/sysctl.conf ==> 添加 fs.file-max=102400 退出后,执行 ==> sysctl -p # 查看文件描述符数 sysctl fs.file-max cat /proc/sys/fs/file-max # 示例 [root@ab103f74b163 /]# sysctl -w fs.file-max=102400 fs.file-max = 102400 [root@ab103f74b163 /]# sysctl fs.file-max fs.file-max = 102400 [root@ab103f74b163 /]# cat /proc/sys/fs/file-max 102400 [root@ab103f74b163 /]# vi /etc/sysctl.conf [root@ab103f74b163 /]# sysctl -p fs.file-max = 100000 [root@ab103f74b163 /]# cat /proc/sys/fs/file-max 100000 [root@ab103f74b163 /]# sysctl fs.file-max fs.file-max = 100000
*************
openresty 设置
nginx.conf
worker_processes auto; # 工作进程个数,on:根据cpu数自动确定 wprker_rlimit_nofile 65535; # 最多打开的文件数量 events { worker_connections 65535; # 每个工作进程可接受的连接数 multi_accept on; # 是否允许一个工作进程响应多个请求 }
*************
示例
default.conf
server { listen 80; server_name localhost; location / { root /usr/local/openresty/nginx/html; index index.html index.htm; } location /test { echo $server_name $remote_addr; echo "test"; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/local/openresty/nginx/html; } }
nginx.conf
... events { worker_connections 1024; } ...
创建openresty容器
docker run -it -d --net fixed --ip 172.18.0.2 -p 9000:80 \ -v /Users/huli/lua/openresty/conn/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf \ -v /Users/huli/lua/openresty/conn/default.conf:/etc/nginx/conf.d/default.conf \ --name open-conn lihu12344/openresty
ab 测试:ab -n 2000 -c 1000 http://localhost:9000/test
# 请求全部执行成功 huli@hudeMacBook-Pro conn % ab -n 2000 -c 1000 http://localhost:9000/test This is ApacheBench, Version 2.3 <$Revision: 1879490 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking localhost (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: openresty/1.21.4.1 Server Hostname: localhost Server Port: 9000 Document Path: /test Document Length: 31 bytes Concurrency Level: 1000 Time taken for tests: 1.760 seconds Complete requests: 2000 # 发送请求2000 Failed requests: 0 # 失败的请求数为0 Total transferred: 348000 bytes HTML transferred: 62000 bytes Requests per second: 1136.21 [#/sec] (mean) Time per request: 880.123 [ms] (mean) Time per request: 0.880 [ms] (mean, across all concurrent requests) Transfer rate: 193.07 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 20 13.0 19 51 Processing: 53 719 479.1 437 1434 Waiting: 5 695 471.5 413 1429 Total: 56 739 471.4 459 1446 Percentage of the requests served within a certain time (ms) 50% 459 66% 1177 75% 1211 80% 1319 90% 1342 95% 1366 98% 1388 99% 1395 100% 1446 (longest request)
ab 测试:ab -n 2000 -c 2000 http://localhost:9000/test
# 部分请求执行失败 huli@hudeMacBook-Pro conn % ab -n 2000 -c 2000 http://localhost:9000/test This is ApacheBench, Version 2.3 <$Revision: 1879490 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking localhost (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: openresty/1.21.4.1 Server Hostname: localhost Server Port: 9000 Document Path: /test Document Length: 31 bytes Concurrency Level: 2000 Time taken for tests: 0.856 seconds Complete requests: 2000 # 发送请求2000 Failed requests: 448 # 448个请求执行失败 (Connect: 0, Receive: 0, Length: 448, Exceptions: 0) Total transferred: 270048 bytes HTML transferred: 48112 bytes Requests per second: 2337.75 [#/sec] (mean) Time per request: 855.525 [ms] (mean) Time per request: 0.428 [ms] (mean, across all concurrent requests) Transfer rate: 308.25 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 63 21.6 64 104 Processing: 108 488 150.2 383 730 Waiting: 0 401 254.8 378 724 Total: 109 550 131.9 470 779 Percentage of the requests served within a certain time (ms) 50% 470 66% 646 75% 695 80% 710 90% 727 95% 744 98% 762 99% 764 100% 779 (longest request)
nginx.conf:修改worker_connections = 2000,重启应用
... events { worker_connections 2000; } ...
ab 测试:ab -n 2000 -c 2000 http://localhost:9000/test
# 修改worker_connections =2000后,请求全部执行成功 huli@hudeMacBook-Pro conn % ab -n 2000 -c 2000 http://localhost:9000/test This is ApacheBench, Version 2.3 <$Revision: 1879490 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking localhost (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: openresty/1.21.4.1 Server Hostname: localhost Server Port: 9000 Document Path: /test Document Length: 26 bytes Concurrency Level: 2000 Time taken for tests: 1.126 seconds Complete requests: 2000 # 请求发送数2000 Failed requests: 0 # 请求失败数为0 Total transferred: 338000 bytes HTML transferred: 52000 bytes Requests per second: 1776.36 [#/sec] (mean) Time per request: 1125.901 [ms] (mean) Time per request: 0.563 [ms] (mean, across all concurrent requests) Transfer rate: 293.17 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 69 25.4 72 112 Processing: 117 607 216.5 586 993 Waiting: 5 602 217.6 583 982 Total: 118 676 199.1 672 1042 Percentage of the requests served within a certain time (ms) 50% 672 66% 788 75% 827 80% 875 90% 969 95% 991 98% 1002 99% 1013 100% 1042 (longest request)
客户端限制
限制同一个ip并发数
# 同一个ip地址限制10个并发连接 http { limit_conn_zone $binary_remote_addr zone=perip:10m; limit_conn perip 10; } # 相关说明 limit_conn_zone:开辟一块内存空间保存客户端ip,空间名称perip,空间大小10m limit_conn:限制客户端请求数 $binary_remote_addr:以二进制形式保存客户端ip limit_conn_zone、limit_conn也可设置在server、location中,实现不同级别的并发控制
限制虚拟主机的并发数
# 对虚拟主机限制10个并发连接 http { limit_conn_zone $server_name zone=perserver:10m; server { listen 80; server_name localhost; limit_conn perserver 10; } } # 相关说明 limit_conn_zone:开辟一块内存空间保存虚拟主机,空间名称perserver,空间大小10m limit_conn:限制客户端请求数 $server_name:获取虚拟主机
限制响应的传输速率
# 限制响应传输速率 http { limit_rate 100k; limit_rate_after 10m; ... } # 相关说明 limit_rate:限制传输速率为100kb/s limit_rate_after:如果设置了该指令,表示传输制定数据量(10m)后,开始限速 如果没有设置,则一开始就会进行限速
浏览器缓存
nginx 缓存响应头
Last_Modified:nginx自动生成 ETag:nginx自动生成 Cache-Control:缓存控制响应头 * Cache-Control:max-age=N ==> 缓存有效期N秒 * Cache-Control:no-cache ==> 本地保存缓存,如果没有更新使用本地缓存(304); 如果有更新,去服务器获取最新的数据(200),并更新本地缓存 * Cache-Control:no-store ==> 不存储缓存,每次请求都会访问服务器 * Cache-Control:max-age=N,must-revalidate ==> 缓存有效期N秒,过期前直接使用本地缓存 过期后,如果没有更新,直接使用本地缓存(304) 过期后,如果有更新,获取最新数据(200),并更新本地缓存 Expires:缓存过期时间
expires 指令设置缓存有效期
server { ... location \.(gif|jpg|jpeg|png|bmp|swf)$ { ... expires 10d; } location \.(css|js)$ { ... expires 12h; } } # 相关说明 即使mginx没有设置expires,浏览器也会自动缓存常见的静态资源(根据拓展名识别静态资源); 使用expires可控制浏览器缓存的有效时间 当浏览器需要更新静态资源时,可设置动态路径(*css?arg=random_int)更新资源
评论暂时关闭