본문 바로가기
Backend/JAVA

[JAVA] 자바에서 로직 수행시간 성능 속도 체크하기 - StopWatch

by couque 2023. 4. 13.
반응형

자바에서 비즈니스 로직 또는 특정 로직에 대해 수행 시간을 측정할때 사용할 수 있는 방법 두가지에 대해 알아보자.


# StopWatch

StopWatch는 스프링 프레임워크에서 제공해주기 때문에 스프링 환경을 사용한다면 사용하기 편리하다.

아래 코드처럼 prettyPrint()를 사용하면 보기좋게 로그를 찍을 수도 있다.

import org.springframework.util.StopWatch;

StopWatch sc = new StopWatch("Stop Watch Welcome");

sc.start("processing");

Thread.sleep(1000);

sc.stop();

System.out.println(sc.prettyPrint());

/*

StopWatch 'Stop Watch Welcom' : running time (millis) = 1011
-------------------------------------
ms         %         Task name
-------------------------------------
01011    100%        processing

*/

 

# currentTimeMillis()

스프링 프레임워크에서 제공하는 StopWatch()를 사용하지 않고 자바 System에서 제공하는 currentTimeMillis()로 

수행시간을 측정할 수도 있다.

long start = System.currentTimeMillis();
long end = System.currentTimeMillis();

System.out.println("end-start(ms) : " + (end-start) + "ms");
// end-start(ms) : 1002ms

System.out.println("end-start : " + (end-start) / 1000);
// end-start : 1

번외로 추가

# ip 가져오기

import java.net.InetAddress;

public static String getServerrAddr(){
	String ip = null;
    
    try{
    	InetAddress inet = InetAddress.getLocalHost();
        ip = inet.getHostAddress();
    }catch(Exception e){
    	e.printStackTrace();
        ip = e.getClass().getSimpleName();
    }
    
    return ip;
}

 

# 현재시간 가져오기

String now = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());

System.out.println("now : " + now);
// 20230413195520927

 

반응형

댓글