天天看点

如何获取JAVA代码里的full gc时间和如何获取接口的运行时间

获取JAVA代码块的运行时间,代码运行时间包含(执行时间+GC时间);在高并发大数据量的情况下,往往代码的GC时间会远远超过执行时间;

在HDFS的早期版本里出现过运行一段时间,HDFS服务器自动关闭,原因是代码里有超时退出系统的逻辑 ;正常超过30秒就退出,一般代码是不会有30秒运行时间,但是随着系统运行时间长,系统垃圾增多,GC的时间会线性的增长;

因此超出30秒退出的逻辑需要30+代码块运行的时间;

package LearnCases;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: suibin
 * @Date: 2021/06/08/17:07
 * @Description:
 */
public class FullGcTime {
    public static void main(String[] args) throws InterruptedException {
        FullGcTime fgt=new FullGcTime();
        fgt.start();//开始计时
        Thread.sleep(2000);
        long elapseTime=fgt.getElaspseTime();//停止计时
        System.out.println(elapseTime);


        //fgt.start();
        //Thread.sleep(2000);
        //elapseTime=fgt.getElaspseTime();
        //System.out.println(elapseTime);
    }
    //开始时间
    long start=0;
    //开始计时
    public FullGcTime start(){
        start=System.currentTimeMillis();
        return this;
    }
    //流逝的时间
    public long getElaspseTime(){
        return System.currentTimeMillis()-start;
    }
}
           

接口的时间,通过拦截器拦截接口注解。打印 

package com.lanyu.community.api.aop;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * web 接口日志
 *
 * @author : 隋斌
 * @since : 2019-03-28
 */
@Aspect
@Component
public class WebLogAspect {
    private static Logger log = LoggerFactory.getLogger(WebLogAspect.class);

    ThreadLocal<Long> startTime = new ThreadLocal<>();

    private final ObjectMapper mapper;

    @Autowired
    public WebLogAspect(ObjectMapper mapper) {
        this.mapper = mapper;
    }

    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void webLog() {
    }

    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) {
        //开始计时
        startTime.set(System.currentTimeMillis());

        for (Object object : joinPoint.getArgs()) {
            if (
                    object instanceof MultipartFile
                            || object instanceof HttpServletRequest
                            || object instanceof HttpServletResponse
            ) {
                continue;
            }
            try {
                if (log.isDebugEnabled()) {
                    log.debug(
                            joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName()
                                    + " : request parameter : " + mapper.writeValueAsString(object)
                    );
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @AfterReturning(returning = "response", pointcut = "webLog()")
    public void doAfterReturning(Object response) throws Throwable {
        if (response != null) {
            log.debug("response parameter : " + mapper.writeValueAsString(response));
            log.debug("方法执行时间:{}毫秒", (System.currentTimeMillis() - startTime.get()));
        }
    }
}