分析Spring Boot项目启动慢原因之 《一》打印bean耗时

分析Spring Boot项目启动原因之 一 打印bean耗时

	由于项目比较久远,积累的代码犹如屎山,然后项目启动也特别慢,然后产生想要优化一下启动速度,故写了一个Bean启动时的耗时分析:
package com.tekrally.assetManagement.util;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.list.UnmodifiableList;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author chengshengwen
 * @Date 2023/6/15 11:41
 * @PackageName:com.tekrally.assetManagement.util
 * @ClassName: BeanInitMetrics
 * @Description: TODO  打印bean耗时的基础工具类
 * @Version 1.0
 */
@Service
@Slf4j
public class BeanInitMetrics implements BeanPostProcessor {

    private final Map<String, Long> stats = new HashMap<>();

    //private Map<String,Integer> metrics = new HashMap<>();


    private final List<Metric> metrics = new ArrayList<>();

    @Data
    public static class Metric{

        public Metric() {
        }

        public Metric(String name, Integer value) {
            this.name = name;
            this.value = value;
            this.createDate = new Date();
        }

        private String name; //bean名称打印

        private Integer value; //bean耗时,单位为毫秒

        private Date createDate; //bean耗时的创建时间
    }

    //重写初始化接口
    @Override
    public Object postProcessBeforeInitialization(@NotNull Object bean, @NotNull String beanName) throws BeansException {
        stats.put(beanName, System.currentTimeMillis());
        return bean;
    }

    //后处理后初始化
    @Override
    public Object postProcessAfterInitialization(@NotNull Object bean, @NotNull String beanName) throws BeansException {
        Long start = stats.get(beanName);
        if (start != null) {
            metrics.add(new Metric(beanName, Math.toIntExact(System.currentTimeMillis() - start)));
        }
        return bean;
    }

    //通过后处理后初始化 - 初始化的时间算出bean耗时
    public List<Metric> getMetrics() {
        metrics.sort((o1, o2) -> {
            try {
                return o2.getValue() - o1.getValue();
            }catch (Exception e){
                return 0;
            }
        });
        log.info("metrics {}", JSON.toJSONString(metrics));
        return UnmodifiableList.unmodifiableList(metrics); //只读的集合
    }

}

controller提供api调用:

package com.tekrally.assetManagement.controller;

import com.tekrally.assetManagement.util.BeanInitMetrics;
import com.tekrally.entity.Feedback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author chengshengwen
 * @Date 2023/6/15 11:47
 * @PackageName:com.tekrally.assetManagement.controller
 * @ClassName: BeanInitController
 * @Description: TODO 测试加载bean慢的原因的api层
 * @Version 1.0
 */
@RestController
@RequestMapping("beanInit")
public class BeanInitController {

    @Resource
    private BeanInitMetrics beanInitMetrics;

    @GetMapping("/getMetrics")
    public List<BeanInitMetrics.Metric> getMetrics() {
        return beanInitMetrics.getMetrics();
    }
}

测试打印(name为bean的名称,value为耗时单位,为毫秒,createDate为创建的时间戳):
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值