[Spring] 메소드 실행시간 측정

Kyungmin·2024년 4월 10일
0

Spring

목록 보기
12/39

테스트 코드를 작성하면서 성능개선을 위해 메소드들의 실행시간을 측정할 일이 생겼다. 메소드 실행 시간을 측정하는 방법들을 알아보자

1. StopWatch - 스프링에서 제공

@Test
public void testSearchStorePerformance() {
        int start = 0;
        int size = 100;
        String keyword = "치킨";


        StopWatch stopWatch = new StopWatch();

        // 키워드 검색 테스트
        stopWatch.start("searchByKeyword(DSL 사용 X)");
        StorePageDto.Info resultByKeyword = storeService.searchStoreByCategory(keyword, start, size);
        stopWatch.stop();
        System.out.println("검색 결과 (DSL 사용 X): " + resultByKeyword.getStores().size());

        // 실행 시간 출력
        System.out.println(stopWatch.prettyPrint());
    }

검색 결과 (DSL 사용 X): 100
StopWatch '': 0.252218666 seconds

----------------------------------------
Seconds       %       Task name
----------------------------------------
0.252218666   100%    searchByKeyword(DSL 사용 X)

2. System.currentTimeMillis()

 @Test
public void benchmarkGetBookmarks2() {
  
        User user = userRepository.findById(1L).orElseThrow();

        // 두 번째 로직 (Querydsl) 실행 시간 측정
        long startTime = System.currentTimeMillis();
        List<BookMarkDto.UserBookmarkDto> bookmarksQuerydslMethod = bookMarkService.getBookmarks(user);
        long endTime = System.currentTimeMillis();
        System.out.println("Querydsl 실행시간 측정 : " + (endTime - startTime) + "ms");
    }

Querydsl 실행시간 측정 : 75ms

profile
Backend Developer

0개의 댓글

관련 채용 정보