介绍

性能优化需要考虑的点:

  • 当前系统结构瓶颈:观察指标(top,日志)、压力测试(ab压测工具)
  • 了解业务模式:接口业务类型(秒杀,抢购)、系统层次化结构
  • 性能与安全:设计防火墙功能。

一、ab压测工具
安装:

[root@node1 conf.d]# yum install httpd-tools

使用:

ab -n 2000 -c 2 http://127.0.0.1/
  • -n:总的请求书
  • -c:并发数
  • -k:是否开启长连接

ab工具演示

  1. nginx配置
server {listen       80;server_name  192.168.1.104 node1;#charset koi8-r;#access_log  /var/log/nginx/log/host.access.log  main;#root   /opt/app;location / {root /opt/app/code/cache/;try_files /cache $uri @java_page;}location @java_page{proxy_pass http://192.168.1.104:9090;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504 404  /50x.html;location = /50x.html {root   /usr/share/nginx/html;}
}
  1. 压测结果
[root@node1 conf.d]# ab -n 2000 -c 2 http://192.168.1.104/admin.html
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.1.104 (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 requestsServer Software:        nginx/1.16.1 // 工具探测到的软件信息
Server Hostname:        192.168.1.104
Server Port:            80Document Path:          /admin.html // 文件的路径
Document Length:        143 bytes // body,该文件返回的大小,143字节Concurrency Level:      2 // 并发级别
Time taken for tests:   0.652 seconds // 花费的总时间
Complete requests:      2000 // 总的完成的请求数
Failed requests:        0 // 失败的请求数
Write errors:           0
Total transferred:      750000 bytes
HTML transferred:       286000 bytes
Requests per second:    3065.32 [#/sec] (mean) // 每秒所能处理的请求QPS(总请求/总时间)
Time per request:       0.652 [ms] (mean) // 一个请求完成的时间,客户端访问服务端
Time per request:       0.326 [ms] (mean, across all concurrent requests) // 服务端处理一个请求的时间,不包括客户端与服务端网络传输的时间
Transfer rate:          1122.55 [Kbytes/sec] received // 速率,判断网络是否存在瓶颈Connection Times (ms)min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       4
Processing:     0    0   0.3      0       4
Waiting:        0    0   0.3      0       3
Total:          0    1   0.4      1       5Percentage of the requests served within a certain time (ms)50%      166%      175%      180%      190%      195%      198%      299%      2100%      5 (longest request)
  1. 测试动态接口,tomcat下部署jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML><HEAD><TITLE>JSP Test Page</TITLE></HEAD><BODY><%Thread.sleep(5000);Random rand = new Random();out.println("<h1>Random number:</h1>");out.println(rand.nextInt(99)+100);%></BODY>
</HTML>
  1. 启动tomcat
[root@node1 bin]# sh catalina.sh start;tail -f ../logs/catalina.out
[root@node1 bin]# netstat -luntp|grep java
tcp6       0      0 :::9090                 :::*                    LISTEN      6410/java
tcp6       0      0 127.0.0.1:9005          :::*                    LISTEN      6410/java
tcp6       0      0 127.0.0.1:9009          :::*                    LISTEN      6410/java
  1. 单个请求测试
[root@node1 bin]# curl -I http://127.0.0.1/sleepjava.jsp
HTTP/1.1 200
Server: nginx/1.16.1
Date: Mon, 06 Apr 2020 08:23:23 GMT
Content-Type: text/html;charset=utf-8
Connection: keep-alive
Set-Cookie: JSESSIONID=26F706E6ED964C1B9941128321DE4A4F; Path=/; HttpOnly
  1. ab测试20的请求
[root@node1 bin]# ab -n 20 -c 2 http://127.0.0.1/sleepjava.jsp
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 127.0.0.1 (be patient).....doneServer Software:        nginx/1.16.1
Server Hostname:        127.0.0.1
Server Port:            80Document Path:          /sleepjava.jsp
Document Length:        147 bytesConcurrency Level:      2
Time taken for tests:   55.117 seconds
Complete requests:      20
Failed requests:        0
Write errors:           0
Total transferred:      7540 bytes
HTML transferred:       2940 bytes
Requests per second:    0.36 [#/sec] (mean)
Time per request:       5511.728 [ms] (mean)
Time per request:       2755.864 [ms] (mean, across all concurrent requests)
Transfer rate:          0.13 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       1
Processing:  5004 5011   5.4   5008    5020
Waiting:     5004 5010   5.3   5008    5020
Total:       5004 5011   5.5   5008    5020Percentage of the requests served within a certain time (ms)50%   500866%   501175%   501680%   502090%   502095%   502098%   502099%   5020100%   5020 (longest request)

二、系统与Nginx性能优化

  • 网络:对于网络而言,我们要关注网络的流量是否有丢包
  • 系统:关注硬件有没有磁盘的损坏,磁盘的速率以及系统负载饱和,内存使用率以及系统的稳定性
  • 服务:更主要的就是服务本身,Nginx本身连接的优化以及内核性能的优化以及http请求的优化
  • 程序:接口性能,处理速度,程序的执行效率
  • 数据库、底层服务:分布式存储以及其他一些相关的服务的性能

三、文件句柄设置
linux/Unix一切皆文件,文件句柄就是一个索引。默认操作系统会设置为1024个。对于nginx来说就太小了。
设置方式:系统全局性修改,用户局部性修改,进程局部性修改。

[root@node1 /]# cd /etc/security/
[root@node1 security]# ls
limits.confroot soft nofile 65535 // 用户
root hard nofile 65535*    soft nofile 25535 // 全局*    hard nofile 25535
  • soft:发提醒,操作系统不会强制的限制
  • hard:操作系统会采取机制进行限制
  • 一般设置1W就足够了

针对进程设置:

[root@node1 nginx]# vim nginx.confworker_rlimit_nofile 35535; //进程

四、CPU亲和配置
CPU亲和,让进程通常不会在处理器之间频繁迁移进程,进程迁移频率小,减少性能损耗。
查看系统当前有多少个CPU:

[root@node1 nginx]# cat /proc/cpuinfo|grep "physical id"|sort|uniq|wc -l
1

查看CPU核心:

[root@node1 nginx]# cat /proc/cpuinfo|grep "cpu cores"|uniq
cpu cores  : 1

查看cpu总核心:

[root@node1 nginx]# cat /proc/cpuinfo|grep "processor"|wc -l
1

CPU亲和配置:nginx.conf,以16核心为例:

user  nginx;
worker_processes  16; // 当前启动的worker进程,官方建议与CPU核心一致
#worker_cpu_affinity 0000000000000010 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000;
#worker_cpu_affinity 1010101010101010 0101010101010101;
worker_cpu_affinity auto;

worker_cpu_affinity:表示当前是哪些CPU可以用的(worker_processes 2),第一个使用1,3,5,7,9…
查看当前Nginx所使用的CPU是哪一个:

[root@node1 nginx]# ps -eo pid,args,psr | grep [n]ginx2662 nginx: master process nginx   0 //虚拟机配置只有一个核心6233 nginx: worker process         0
  • ps -eo输出nginx这个进程
  • pid:nginx的pid
  • args:nginx的进程名
  • psr:nginx使用的cpu是哪一个

五、Nginx通用配置优化

user  nginx; // 建议配置普通用户nginx,禁止远程登陆
worker_processes  16;                             #数字和CPU个数一样
#worker_cpu_affinity 0000000000000010 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000;
#worker_cpu_affinity 1010101010101010 0101010101010101;
worker_cpu_affinity auto;error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;worker_rlimit_nofile 35535;events {use epoll;worker_connections  10240; //每个worker进程处理的个数
}http {include       /etc/nginx/mime.types;default_type  application/octet-stream;########Charsetcharset 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" "$request_uri"';access_log  /var/log/nginx/access.log  main;########Core modluesendfile        on;#tcp_nopush     on;#tcp_nodeny     on;keepalive_timeout  65;#########Gzip modulegzip  on; // gzip配置成全局gzip_disable "MSIE [1-6]\.";  // IE6浏览器不支持一些gzip压缩,对于IE1-6配置不进行gzip压缩gzip_http_version 1.1;#########Virtal Serverinclude /etc/nginx/conf.d/*.conf;
}

参考链接 :

Nginx性能优化 : https://www.jianshu.com/p/e3421399e634

Nginx性能优化 :
https://mp.weixin.qq.com/s/LwKyPwCqgeTGIqG_LVGFdQ

Nginx压测性能优化相关推荐

  1. 谷粒商城项目篇8_分布式高级篇_商城首页、性能压测、优化(Nginx动静分离)

    目录 商城首页 整合thymeleaf springmvc的WebMvcAutoConfiguration 首页三级分类渲染 Nginx代理 Nginx代理会丢掉host信息 压力测试 性能检测 性能 ...

  2. Java 应用压测性能问题定位经验分享

    什么是压测 压测,即压力测试,是确立系统稳定性的一种测试方法,通常在系统正常运作范围之外进行,以考察其功能极限和和可能存在的隐患. 压测主要用于检测服务器的承受能力,包括用户承受能力,即多少用户同时使 ...

  3. 为什么7层负载均衡压测性能低

    为什么80%的码农都做不了架构师?>>>    为什么7层负载均衡压测性能低 7层性能会比4层性能低 负 载均衡集群采用LVS和Tengine实现,其中4层监听经过LVS后直接到达后 ...

  4. 谷粒商城十二性能压测及优化

    在分布式开发中,我们将每一一种服务都抽取成一个独立的模块,微服务模块在真正的上线之前,甚至是上线以后,我们都要进行压力测试,才能投入正常的使用. 压力测试是为了我们的系统在当前软硬件环境下,最大的负荷 ...

  5. golang连接postgresql too many client_MySQL和PostgreSQL压测性能对比

    阅读使人充实,讨论使人敏捷,写作使人精确. >>> 压测业务场景文章属于互联网社区动态类场景核心功能压测案例.至于题目涉及的MySQL和PostgreSQL之间的关系,主要为业务选型 ...

  6. 【分布式事务】GitHub上分布式事务框架压测性能对比

    一.前言 随着项目逐步以微服务开发为趋势,逐渐呈现一个服务对应一个数据库.从中产生了分布式事务的问题:一个操作先后调用不同的服务,要保证服务间的事务一致性,这就是分布式事务解决的问题. 本次调研,根据 ...

  7. 基于 Nginx 的 HTTPS 性能优化实践

    前言 分享一个卓见云的较多客户遇到HTTPS优化案例. 随着相关浏览器对HTTP协议的"不安全".红色页面警告等严格措施的出台,以及向 iOS 应用的 ATS 要求和微信.支付宝小 ...

  8. 基于 Nginx 的 HTTPS 性能优化

    前言 分享一个卓见云的较多客户遇到HTTPS优化案例. 随着相关浏览器对HTTP协议的"不安全".红色页面警告等严格措施的出台,以及向 iOS 应用的 ATS 要求和微信.支付宝小 ...

  9. 【JAVA】跨机房压测性能问题分析

    参考博客:Java线程的6种状态及切换  JVM性能调优监控工具详解 深度理解Tomcat的参数 问题描述:压测部署在A和B两个机房的同样Java服务接口性能表现差别很大,接口内容为mget查询cod ...

最新文章

  1. css3 动画 火箭,CSS3 火箭发射动画 寓意创新起航
  2. sql server排序慢_用Nginx实现接口慢查询并可示化展示TOP 20
  3. ESP32的OTA详细过程逻辑
  4. make changes to the open source project?
  5. boost::tti模块测试函数模板
  6. wordpress 根据文章ID获取分类ID和标签ID
  7. 在php中源代码怎么查看,查看PHP Closure的源代码
  8. 深入理解typedef
  9. 关于用iframe大框架覆盖小框架的问题
  10. java面向对象程序练习_5本面向经验丰富的程序员的高级Java书籍
  11. Java软件开发中,常用的工具有哪些?
  12. python建立ip代理池_如何建Python爬虫的IP代理池?3步学会搭建IP代理池
  13. simulink积分器报错
  14. 雷电模拟器Android obb,exagear模拟器数据obb包
  15. 参加江大白手把手教你-----AidLux智慧安防AI训练营
  16. VIVADO软件介绍与使用
  17. JuliaFEM中的数据格式——fields.jl
  18. 2021云计算技能竞赛真题卷(附标准答案)
  19. 【资源帖】漏洞平台(国内外)+企业SRC整理-持续更新
  20. ZYNQ - 嵌入式Linux开发 -05- Linux C编程和Makefile

热门文章

  1. 文档智能理解:通用文档预训练模型与数据集
  2. 网易如何做新闻推荐:深度学习排序系统及模型
  3. 现在做网络推广,哪种方式最好?
  4. gpio stm8 管脚 配置工具_STM8S 外设模块的GPIO引脚应该如何配置
  5. java word2007_Java解析word2007、Excel2003和Excel2007
  6. jsp+mysql汽车维修管理系统的设计与开发_基于JSP+MySQl的计算机学院设备报修管理系统.doc...
  7. html怎么做成3d正方体,利用CSS3的3D效果制作正方体
  8. Java面试那些事--可能会问的那些问题(中高级篇)
  9. App应用之提交到各大市场渠道
  10. oracle linux 版本 uek,在运行 Oracle Linux 7.1 UEK3 或 7.2 或者 RHEL 7.1 或 7.2 的系统上,RDMA 服务无法启动...