一.拉齐测试环境和生产环境参数
1.tomcat参数
server:
port: 9002
tomcat:
max-http-post-size: -1
#等待队列长度,默认100。
accept-count: 200
#最大工作线程数,默认200。按照1核200线程算(最多1核最多允许400)
max-threads: 1200
#最小工作空闲线程数,默认10。(适当增大一些,以便应对突然增长的访问量)
min-spare-threads: 100
2.数据库连接池参数(这里已dbcp2为例)
datasource:
url: jdbc:mysql://xxxxxx
username: xxxxx
password: xxxx
driver-class-name: xxxxxxxx
dbcp2:
maxIdle: 500
minIdle: 100
initialSize: 200
maxWaitMillis: -1
maxConnLifetimeMillis: 1140000
maxTotal: 500
3.redis连接池参数
redis:
database: 0
host: 127.0.0.1
port: 6379
timeout: 3000
jedis:
pool:
max-idle: 1600
min-idle: 100
max-active: 1600
max-wait: 500
4.拉齐实例数、cpu和内存
5.服务间调用(以restTemplate为例)
PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager();
poolingConnectionManager.setMaxTotal(2*cpu核数 + 3);
poolingConnectionManager.setDefaultMaxPerRoute(2*cpu核数);
二.分析每个操作耗时
1.统计每个操作耗时并以hash结构存入redis,item是当前时间
long startTime = System.currentTimeMillis();
//操作
long endTime = System.currentTimeMillis();
long timeOne = endTime - startTime
startTime = System.currentTimeMillis();
//操作
endTime = System.currentTimeMillis();
long timeTwo = endTime - startTime
redisUtils.hmset("test",String.valueOf(System.currentTimeMillis()),timeOne + "," + timeTwo);
2.写计算平均耗时方法
Map<Object, Object> test= redisUtils.hmget("test");
String testTime= "";
if (MapUtils.isNotEmpty(test)) {
int size = test.size();
long timeOneAll = 0;
long timeTwoAll = 0;
for(Object key : test.keySet()) {
String timeString = (String) test.get(key);
String[] splits = timeString.split(",");
timeOneAll = timeOneAll + Long.valueOf(splits[0]);
timeTwoAll = timeTwoAll + Long.valueOf(splits[1]);
}
testTime= " test第一个节点平均耗时:" + (timeOneAll/size) + ",第二个节点平均耗时:" + (timeTwoAll/size);
}
redisUtils.delete("test");
return testTime;
压测完调用这个方法就可以知道每一个步骤的耗时,针对优化
二.代码优化
1.配置类数据从redis中取
2.redis中不存大value
3.查询只查有用数据,不要select*,把一整条数据取出
4.逻辑上不循环插入和更新,对数据进行组装后进行一次操作
三.数据库方面
1.根据实际查询条件增加索引
2.去掉冗余索引
(待调整)