• openresty 性能优化



    openresty 性能优化

             

                     

                                     

    ab 压测

             

    命令格式:ab  [options]  url

    1. huli@hudeMacBook-Pro ~ % ab -h
    2. Usage: ab [options] [http[s]://]hostname[:port]/path
    3. # 可选参数
    4. Options are:
    5. -n requests Number of requests to perform
    6. * 发送的请求总数
    7. -c concurrency Number of multiple requests to make at a time
    8. * 请求并发数
    9. -t timelimit Seconds to max. to spend on benchmarking
    10. This implies -n 50000
    11. * 压测最多执行的时间
    12. -s timeout Seconds to max. wait for each response
    13. Default is 30 seconds
    14. * 响应超时时间,默认30s
    15. -b windowsize Size of TCP send/receive buffer, in bytes
    16. -B address Address to bind to when making outgoing connections
    17. -p postfile File containing data to POST. Remember also to set -T
    18. -u putfile File containing data to PUT. Remember also to set -T
    19. -T content-type Content-type header to use for POST/PUT data, eg.
    20. 'application/x-www-form-urlencoded'
    21. Default is 'text/plain'
    22. -v verbosity How much troubleshooting info to print
    23. -w Print out results in HTML tables
    24. -i Use HEAD instead of GET
    25. -x attributes String to insert as table attributes
    26. -y attributes String to insert as tr attributes
    27. -z attributes String to insert as td or th attributes
    28. -C attribute Add cookie, eg. 'Apache=1234'. (repeatable)
    29. -H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
    30. Inserted after all normal header lines. (repeatable)
    31. -A attribute Add Basic WWW Authentication, the attributes
    32. are a colon separated username and password.
    33. -P attribute Add Basic Proxy Authentication, the attributes
    34. are a colon separated username and password.
    35. -X proxy:port Proxyserver and port number to use
    36. -V Print version number and exit
    37. -k Use HTTP KeepAlive feature
    38. -d Do not show percentiles served table.
    39. -S Do not show confidence estimators and warnings.
    40. -q Do not show progress when doing more than 150 requests
    41. -l Accept variable document length (use this for dynamic pages)
    42. -g filename Output collected data to gnuplot format file.
    43. -e filename Output CSV file with percentages served
    44. -r Don't exit on socket receive errors.
    45. -m method Method name
    46. -h Display usage information (this message)
    47. -I Disable TLS Server Name Indication (SNI) extension
    48. -Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
    49. -f protocol Specify SSL/TLS protocol
    50. (TLS1, TLS1.1, TLS1.2 or ALL)
    51. -E certfile Specify optional client certificate chain and private key

                 

    使用示例

    1. huli@hudeMacBook-Pro ~ % ab -n 100 https://www.taobao.com/
    2. This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
    3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    4. Licensed to The Apache Software Foundation, http://www.apache.org/
    5. Benchmarking www.taobao.com (be patient).....done
    6. Server Software: Tengine
    7. Server Hostname: www.taobao.com
    8. Server Port: 443
    9. SSL/TLS Protocol: TLSv1.2,ECDHE-ECDSA-AES128-GCM-SHA256,256,128
    10. Server Temp Key: ECDH X25519 253 bits
    11. TLS Server Name: www.taobao.com
    12. Document Path: /
    13. Document Length: 86840 bytes
    14. Concurrency Level: 1 # 并发数
    15. Time taken for tests: 24.865 seconds # 完成测试花费的时间
    16. Complete requests: 100 # 完成发送的请求数
    17. Failed requests: 0 # 失败的请求数
    18. Total transferred: 8796159 bytes # 总传输数据量大小
    19. HTML transferred: 8684000 bytes # html数据量大小
    20. Requests per second: 4.02 [#/sec] (mean) # 平均每秒执行的请求数
    21. Time per request: 248.646 [ms] (mean) # 平均每个请求执行时间
    22. Time per request: 248.646 [ms] (mean, across all concurrent requests)
    23. # 所有并发请求,平均每个请求执行时间
    24. Transfer rate: 345.47 [Kbytes/sec] received # 传输速率
    25. Connection Times (ms) # 连接耗时:连接、处理、响应等待
    26. min mean[+/-sd] median max
    27. Connect: 96 154 137.2 120 1034
    28. Processing: 74 95 36.1 86 396
    29. Waiting: 26 63 38.6 58 372
    30. Total: 178 248 154.4 207 1125
    31. Percentage of the requests served within a certain time (ms)
    32. # 请求执行时间分布
    33. 50% 207 # 50%的请求在207ms内执行完成
    34. 66% 218
    35. 75% 235
    36. 80% 245
    37. 90% 328
    38. 95% 467
    39. 98% 1117
    40. 99% 1125
    41. 100% 1125 (longest request)

                      

                               

                                     

    连接数优化

             

    *************

    连接数限制

             

    连接数超出系统限制,通常会报出如下错误:

    too many open files

                  

    用户连接数:临时操作

    1. # 查看当前用户描述符限制
    2. [root@7623085b4b89 /]# ulimit -n
    3. 1048576
    4. # 临时修改限制,重启后失效
    5. [root@7623085b4b89 /]# ulimit -n 200000
    6. [root@7623085b4b89 /]# ulimit -n
    7. 200000

                 

    用户连接数:永久操作

    1. vi /etc/ssh/sshd_config, 添加如下内容:
    2. * hard nofile 102400
    3. * soft nofile 102400
    4. 保存后退出,重新登录,即可生效
    5. # 相关说明
    6. *:表示所有用户
    7. nofile:最大文件打开数

                  

    系统连接数(所有用户连接数总和):临时操作

    1. 设置连接数:sysctl -w fs.file-max=102400
    2. 查看连接数:sysctl fs.file-max
    3. # 示例
    4. [root@ab103f74b163 /]# sysctl -w fs.file-max=102400
    5. fs.file-max = 102400
    6. [root@ab103f74b163 /]# sysctl fs.file-max
    7. fs.file-max = 102400

                

    系统连接数:永久操作

    1. # 设置文件描述符术
    2. vi /etc/sysctl.conf
    3. ==> 添加 fs.file-max=102400
    4. 退出后,执行 ==> sysctl -p
    5. # 查看文件描述符数
    6. sysctl fs.file-max
    7. cat /proc/sys/fs/file-max
    8. # 示例
    9. [root@ab103f74b163 /]# sysctl -w fs.file-max=102400
    10. fs.file-max = 102400
    11. [root@ab103f74b163 /]# sysctl fs.file-max
    12. fs.file-max = 102400
    13. [root@ab103f74b163 /]# cat /proc/sys/fs/file-max
    14. 102400
    15. [root@ab103f74b163 /]# vi /etc/sysctl.conf
    16. [root@ab103f74b163 /]# sysctl -p
    17. fs.file-max = 100000
    18. [root@ab103f74b163 /]# cat /proc/sys/fs/file-max
    19. 100000
    20. [root@ab103f74b163 /]# sysctl fs.file-max
    21. fs.file-max = 100000

                

    *************

    openresty 设置

             

    nginx.conf

    1. worker_processes auto; # 工作进程个数,on:根据cpu数自动确定
    2. wprker_rlimit_nofile 65535; # 最多打开的文件数量
    3. events {
    4. worker_connections 65535; # 每个工作进程可接受的连接数
    5. multi_accept on; # 是否允许一个工作进程响应多个请求
    6. }

              

    *************

    示例

             

    default.conf

    1. server {
    2. listen 80;
    3. server_name localhost;
    4. location / {
    5. root /usr/local/openresty/nginx/html;
    6. index index.html index.htm;
    7. }
    8. location /test {
    9. echo $server_name $remote_addr;
    10. echo "test";
    11. }
    12. error_page 500 502 503 504 /50x.html;
    13. location = /50x.html {
    14. root /usr/local/openresty/nginx/html;
    15. }
    16. }

                   

    nginx.conf

    1. ...
    2. events {
    3. worker_connections 1024;
    4. }
    5. ...

                 

    创建openresty容器

    1. docker run -it -d --net fixed --ip 172.18.0.2 -p 9000:80 \
    2. -v /Users/huli/lua/openresty/conn/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf \
    3. -v /Users/huli/lua/openresty/conn/default.conf:/etc/nginx/conf.d/default.conf \
    4. --name open-conn lihu12344/openresty

           

    ab 测试:ab -n 2000 -c 1000 http://localhost:9000/test

    1. # 请求全部执行成功
    2. huli@hudeMacBook-Pro conn % ab -n 2000 -c 1000 http://localhost:9000/test
    3. This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
    4. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    5. Licensed to The Apache Software Foundation, http://www.apache.org/
    6. Benchmarking localhost (be patient)
    7. Completed 200 requests
    8. Completed 400 requests
    9. Completed 600 requests
    10. Completed 800 requests
    11. Completed 1000 requests
    12. Completed 1200 requests
    13. Completed 1400 requests
    14. Completed 1600 requests
    15. Completed 1800 requests
    16. Completed 2000 requests
    17. Finished 2000 requests
    18. Server Software: openresty/1.21.4.1
    19. Server Hostname: localhost
    20. Server Port: 9000
    21. Document Path: /test
    22. Document Length: 31 bytes
    23. Concurrency Level: 1000
    24. Time taken for tests: 1.760 seconds
    25. Complete requests: 2000 # 发送请求2000
    26. Failed requests: 0 # 失败的请求数为0
    27. Total transferred: 348000 bytes
    28. HTML transferred: 62000 bytes
    29. Requests per second: 1136.21 [#/sec] (mean)
    30. Time per request: 880.123 [ms] (mean)
    31. Time per request: 0.880 [ms] (mean, across all concurrent requests)
    32. Transfer rate: 193.07 [Kbytes/sec] received
    33. Connection Times (ms)
    34. min mean[+/-sd] median max
    35. Connect: 0 20 13.0 19 51
    36. Processing: 53 719 479.1 437 1434
    37. Waiting: 5 695 471.5 413 1429
    38. Total: 56 739 471.4 459 1446
    39. Percentage of the requests served within a certain time (ms)
    40. 50% 459
    41. 66% 1177
    42. 75% 1211
    43. 80% 1319
    44. 90% 1342
    45. 95% 1366
    46. 98% 1388
    47. 99% 1395
    48. 100% 1446 (longest request)

                

    ab 测试:ab -n 2000 -c 2000 http://localhost:9000/test

    1. # 部分请求执行失败
    2. huli@hudeMacBook-Pro conn % ab -n 2000 -c 2000 http://localhost:9000/test
    3. This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
    4. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    5. Licensed to The Apache Software Foundation, http://www.apache.org/
    6. Benchmarking localhost (be patient)
    7. Completed 200 requests
    8. Completed 400 requests
    9. Completed 600 requests
    10. Completed 800 requests
    11. Completed 1000 requests
    12. Completed 1200 requests
    13. Completed 1400 requests
    14. Completed 1600 requests
    15. Completed 1800 requests
    16. Completed 2000 requests
    17. Finished 2000 requests
    18. Server Software: openresty/1.21.4.1
    19. Server Hostname: localhost
    20. Server Port: 9000
    21. Document Path: /test
    22. Document Length: 31 bytes
    23. Concurrency Level: 2000
    24. Time taken for tests: 0.856 seconds
    25. Complete requests: 2000 # 发送请求2000
    26. Failed requests: 448 # 448个请求执行失败
    27. (Connect: 0, Receive: 0, Length: 448, Exceptions: 0)
    28. Total transferred: 270048 bytes
    29. HTML transferred: 48112 bytes
    30. Requests per second: 2337.75 [#/sec] (mean)
    31. Time per request: 855.525 [ms] (mean)
    32. Time per request: 0.428 [ms] (mean, across all concurrent requests)
    33. Transfer rate: 308.25 [Kbytes/sec] received
    34. Connection Times (ms)
    35. min mean[+/-sd] median max
    36. Connect: 0 63 21.6 64 104
    37. Processing: 108 488 150.2 383 730
    38. Waiting: 0 401 254.8 378 724
    39. Total: 109 550 131.9 470 779
    40. Percentage of the requests served within a certain time (ms)
    41. 50% 470
    42. 66% 646
    43. 75% 695
    44. 80% 710
    45. 90% 727
    46. 95% 744
    47. 98% 762
    48. 99% 764
    49. 100% 779 (longest request)

                

    nginx.conf:修改worker_connections = 2000,重启应用

    1. ...
    2. events {
    3. worker_connections 2000;
    4. }
    5. ...

              

    ab 测试:ab -n 2000 -c 2000 http://localhost:9000/test

    1. # 修改worker_connections =2000后,请求全部执行成功
    2. huli@hudeMacBook-Pro conn % ab -n 2000 -c 2000 http://localhost:9000/test
    3. This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
    4. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    5. Licensed to The Apache Software Foundation, http://www.apache.org/
    6. Benchmarking localhost (be patient)
    7. Completed 200 requests
    8. Completed 400 requests
    9. Completed 600 requests
    10. Completed 800 requests
    11. Completed 1000 requests
    12. Completed 1200 requests
    13. Completed 1400 requests
    14. Completed 1600 requests
    15. Completed 1800 requests
    16. Completed 2000 requests
    17. Finished 2000 requests
    18. Server Software: openresty/1.21.4.1
    19. Server Hostname: localhost
    20. Server Port: 9000
    21. Document Path: /test
    22. Document Length: 26 bytes
    23. Concurrency Level: 2000
    24. Time taken for tests: 1.126 seconds
    25. Complete requests: 2000 # 请求发送数2000
    26. Failed requests: 0 # 请求失败数为0
    27. Total transferred: 338000 bytes
    28. HTML transferred: 52000 bytes
    29. Requests per second: 1776.36 [#/sec] (mean)
    30. Time per request: 1125.901 [ms] (mean)
    31. Time per request: 0.563 [ms] (mean, across all concurrent requests)
    32. Transfer rate: 293.17 [Kbytes/sec] received
    33. Connection Times (ms)
    34. min mean[+/-sd] median max
    35. Connect: 0 69 25.4 72 112
    36. Processing: 117 607 216.5 586 993
    37. Waiting: 5 602 217.6 583 982
    38. Total: 118 676 199.1 672 1042
    39. Percentage of the requests served within a certain time (ms)
    40. 50% 672
    41. 66% 788
    42. 75% 827
    43. 80% 875
    44. 90% 969
    45. 95% 991
    46. 98% 1002
    47. 99% 1013
    48. 100% 1042 (longest request)

                  

                       

                                     

    客户端限制

             

    限制同一个ip并发数

    1. # 同一个ip地址限制10个并发连接
    2. http {
    3. limit_conn_zone $binary_remote_addr zone=perip:10m;
    4. limit_conn perip 10;
    5. }
    6. # 相关说明
    7. limit_conn_zone:开辟一块内存空间保存客户端ip,空间名称perip,空间大小10m
    8. limit_conn:限制客户端请求数
    9. $binary_remote_addr:以二进制形式保存客户端ip
    10. limit_conn_zone、limit_conn也可设置在server、location中,实现不同级别的并发控制

               

    限制虚拟主机的并发数

    1. # 对虚拟主机限制10个并发连接
    2. http {
    3. limit_conn_zone $server_name zone=perserver:10m;
    4. server {
    5. listen 80;
    6. server_name localhost;
    7. limit_conn perserver 10;
    8. }
    9. }
    10. # 相关说明
    11. limit_conn_zone:开辟一块内存空间保存虚拟主机,空间名称perserver,空间大小10m
    12. limit_conn:限制客户端请求数
    13. $server_name:获取虚拟主机

                 

    限制响应的传输速率

    1. # 限制响应传输速率
    2. http {
    3. limit_rate 100k;
    4. limit_rate_after 10m;
    5. ...
    6. }
    7. # 相关说明
    8. limit_rate:限制传输速率为100kb/s
    9. limit_rate_after:如果设置了该指令,表示传输制定数据量(10m)后,开始限速
    10. 如果没有设置,则一开始就会进行限速

                 

                     

                                     

    浏览器缓存

             

    nginx 缓存响应头

    1. Last_Modified:nginx自动生成
    2. ETag:nginx自动生成
    3. Cache-Control:缓存控制响应头
    4. * Cache-Control:max-age=N ==> 缓存有效期N秒
    5. * Cache-Control:no-cache ==> 本地保存缓存,如果没有更新使用本地缓存(304);
    6. 如果有更新,去服务器获取最新的数据(200),并更新本地缓存
    7. * Cache-Control:no-store ==> 不存储缓存,每次请求都会访问服务器
    8. * Cache-Control:max-age=N,must-revalidate
    9. ==> 缓存有效期N秒,过期前直接使用本地缓存
    10. 过期后,如果没有更新,直接使用本地缓存(304)
    11. 过期后,如果有更新,获取最新数据(200),并更新本地缓存
    12. Expires:缓存过期时间

              

    expires 指令设置缓存有效期

    1. server {
    2. ...
    3. location \.(gif|jpg|jpeg|png|bmp|swf)$ {
    4. ...
    5. expires 10d;
    6. }
    7. location \.(css|js)$ {
    8. ...
    9. expires 12h;
    10. }
    11. }
    12. # 相关说明
    13. 即使mginx没有设置expires,浏览器也会自动缓存常见的静态资源(根据拓展名识别静态资源);
    14. 使用expires可控制浏览器缓存的有效时间
    15. 当浏览器需要更新静态资源时,可设置动态路径(*css?arg=random_int)更新资源

              

                     

  • 相关阅读:
    无胁科技-TVD每日漏洞情报-2022-8-11
    【多线程】线程安全问题
    PyTorch安装与配置教程(2022.11)
    能ping通,TCP就一定能连通吗?
    Miniconda简单操作说明
    java计算机毕业设计摄影网站源码+系统+数据库+lw文档+mybatis+运行部署
    Linux线程基础
    什么是视频内容推荐引擎?
    yolo各模块详解
    毕设 C2C网上拍卖系统论文
  • 原文地址:https://blog.csdn.net/weixin_43931625/article/details/126057578