记一次压测接口性能优化

本文详细介绍了拉齐测试环境中关于Tomcat、数据库连接池、Redis连接池的参数配置,并提供了服务间调用的优化策略。同时,文章还探讨了如何分析操作耗时以及代码和数据库的优化方法,包括从Redis取数据、避免大value存储、精简查询、逻辑优化和索引管理。
摘要由CSDN通过智能技术生成
一.拉齐测试环境和生产环境参数

1.tomcat参数

server:
  port: 9002
  tomcat:
    max-http-post-size: -1
    #等待队列长度,默认100。
    accept-count: 200
    #最大工作线程数,默认200。按照1200线程算(最多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.去掉冗余索引
(待调整)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值