性能优化(一 测试环境准备)

一。 使用vmware 创建CentOS 环境
安装 redis 使用tomcat 连接宿主机器上的mysql 使用jmater 进行压力测试

centos 配置 由于是vm 内存3G 处理2 cpu 宿主机 i5-9400F
jmeter 测试
1.创建线程
2. 使用阶段 使用一个api 进行请求
3. 起始使用 1500 进行查看

500 个请求
存在一些异常信息
取样结果
Thread Name:192.168.31.195测试 1-204
Sample Start:2020-09-22 13:50:03 CST
Load time:119
Connect Time:119
Latency:0
Size in bytes:2374
Sent bytes:0
Headers size in bytes:0
Body size in bytes:2374
Sample Count:1
Error Count:1
Data type ("text"|"bin"|""):text
Response code:Non HTTP response code: java.net.SocketException
Response message:Non HTTP response message: Broken pipe (Write failed)

HTTPSampleResult fields:
ContentType:
DataEncoding: null

响应数据
java.net.SocketException: Broken pipe (Write failed)
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
at org.apache.http.impl.io.SessionOutputBufferImpl.streamWrite(SessionOutputBufferImpl.java:124)
at org.apache.http.impl.io.SessionOutputBufferImpl.flushBuffer(SessionOutputBufferImpl.java:136)
at org.apache.http.impl.io.SessionOutputBufferImpl.flush(SessionOutputBufferImpl.java:144)
at org.apache.http.impl.BHttpConnectionBase.doFlush(BHttpConnectionBase.java:174)
at org.apache.http.impl.DefaultBHttpClientConnection.flush(DefaultBHttpClientConnection.java:183)
at org.apache.http.impl.conn.CPoolProxy.flush(CPoolProxy.java:167)
at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:241)
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl$2.doSendRequest(HTTPHC4Impl.java:454)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.executeRequest(HTTPHC4Impl.java:930)
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:641)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:66)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1281)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1270)
at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:630)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:558)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256)
at java.lang.Thread.run(Thread.java:748)

1000个 请求报错
org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:8888 [/127.0.0.1] failed: Operation timed out (Connection timed out)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:156)
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl$JMeterDefaultHttpClientConnectionOperator.connect(HTTPHC4Impl.java:401)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:401)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.executeRequest(HTTPHC4Impl.java:930)
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:641)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:66)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1281)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1270)
at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:630)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:558)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Operation timed out (Connection timed out)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)
at java.net.Socket.connect(Socket.java:606)
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
... 19 more

以上使用的配置为 默认配置
======开始优化=

1. 设置tomcat catalina.sh 添加 JAVA_OPTS=$JAVA_OPTS -server -Xms1024m -Xmx1024m
对 1000 请求异常 未见效果

2.配置nginx
events {
use epoll;
worker_connections 655350;
# worker_rlimit_nofile 65535;
multi_accept on;
}
配置后请求 仍旧无效
3. 查看 centos 系统可以打开的最大文件数 ulimit -n
执行命令
[root@www ~]# vi /etc/security/limits.conf

End of file

  • soft core unlimit

  • hard core unlimit

  • soft fsize unlimited

  • hard fsize unlimited

  • soft data unlimited

  • hard data unlimited

  • soft nproc 65535

  • hard nproc 63535

  • soft stack unlimited

  • hard stack unlimited

  • soft nofile 409600

  • hard nofile 409600

    以上的调整后 还是 对 1000个请求 没有影响
    4. 设置tomcat 的配置信息
    1> 开启后台管理
    在/conf/tomcat-users.xml文件中的标签里面添加如下内容






    如果是tomcat7,配置了tomcat用户就可以登录系统了,但是tomcat8中不行,还需要修改另一个配置文件,否则访问不了,提示403,打开webapps/manager/META-INF/context.xml文件

将以上的方式 保存退出后 重启 tomcat 访问8080 端口 点击 Server Status 输入上边配置的用户名 和 密码 就能查看当前 tomcat 的状态信息了

对于tomcat 进行优化
======== 创建线程池 增大线程池数量


修改连接池后重启tomcat 在观察 测试1000并发时的数据 结果是 对 1000 压力测试还是有一半 结果 还是原来的异常

修改tomcat 的 运行方式
bio
性能非常低下,没有经过任何优化处理和支持

nio
nio(new I/O),是Java SE 1.4及后续版本提供的一种新的I/O操作方式(即java.nio包及其子包)。Java nio是一个基于缓冲区、并能提供非阻塞I/O操作的Java API,因此nio也被看成是non-blocking I/O的缩写。它拥有比传统I/O操作(bio)更好的并发运行性能。Tomcat8默认使用nio运行模式。

apr
安装起来最困难,但是从操作系统级别来解决异步的IO问题,大幅度的提高性能

对于每种协议,Tomcat都提供了对应的I/O方式的实现,而且Tomcat官方还提供了在每种协议下每种I/O实现方案的差异, HTTP协议下的处理方式如下表

tomcat 中建议使用 nio tomcat8 默认使用的nio2 所以这个修改作用不大

调整nginx 的keepalive
upstream butterfly {
server 127.0.0.1:8080;
keepalive 20;
}

http {

设置 请求缓冲

client_header_buffer_size 128k;

}

设置请求缓冲中有 效果 异常信息 状态 由 49% --> 37%

nginx重新 优化
http{
sendfile on;
tcp_nopush on;

keepalive_timeout 120;
tcp_nodelay on;
}

nginx 设置错误日志 在全局配置对应的错误日志信息 error_log logs/error.log error;

centos 内核优化
内核配置文件 cat /etc/sysctl.conf

posted @ 2020-09-23 13:34  JC-0527  阅读(495)  评论(0)    收藏  举报