최종 프로젝트

벤치 마크를 사용하여 성능 비교

phonebee 2025. 4. 18. 14:12

● 테스트 주제

<조회를 100번 시도 했을 때의 평균 응답 속도>

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class AuctionServiceBenchmarkTest {

    @Autowired
    private AuctionService auctionService;

    @Test
    public void benchmarkGetAuctionsRedis(){
        int repeat = 100;
        long start = System.currentTimeMillis();

        for(int i=0;i<repeat;i++){
            auctionService.getAuctionsRedis(1,10);
        }

        long end = System.currentTimeMillis();
        System.out.println("[Redis] 평균 응답 시간: " + (end-start)/(double) repeat + "ms");
    }

    @Test
    public void benchmarkGetAuctionsCaffeine(){
        int repeat = 100;
        long start = System.currentTimeMillis();

        for(int i=0;i<repeat;i++){
            auctionService.getAuctionsCaffeine(1,10);
        }

        long end = System.currentTimeMillis();
        System.out.println("[Caffeine] 평균 응답 시간: " + (end-start)/(double) repeat + "ms");
    }

    @Test
    public void benchmarkGetAuctionsWithoutCache(){
        int repeat = 100;
        long start = System.currentTimeMillis();

        for(int i=0;i<repeat;i++){
            auctionService.getAuctions(1,10);
        }

        long end = System.currentTimeMillis();
        System.out.println("[No Cache] 평균 응답 시간: " + (end-start)/(double) repeat + "ms");
    }
}

 

● 테스트 결과

[No Cache] 평균 응답 시간: 5.26ms

[Redis] 평균 응답 시간: 10.05ms

[Caffeine] 평균 응답 시간: 0.12ms

 

Caffeine → No Cache → Redis 순으로 응답 속도가 빠르다.

 

● 의문점

1. 왜 Redis가 느린가?

Redis는 기본적으로 외부서버와 TCP통신을 하기 때문에

  1. Java 객체 → JSON 직렬화
  2. Redis로 네트워크 전송
  3. 다시 역직렬화해서 Java 객체로 복구

반면 Caffeine은 직접 JVM 메모리에서 객체를 바로 꺼내기 때문에 Redis보다 훨씬 빠르다

 

2. 그럼 No Cache보다 느린가?

  • 데이터가 작고 DB가 로컬에 있으면 단군 쿼리 실행이 Redis 통신보다 빠를 수도 있다.
  • Redis는 캐시할 때 Jackson 직렬화/역직렬화가 항상 개입하기 때문에 그만큼 시간이 더 걸리게 된다.
  • 캐시 미스가 발생해서 Redis가 무의미하게 조회되고 있을 수도 있다

일단 속도만을 비교하면 Redis보다 Caffeine이 좀 더 우위에 있음을 알 수 있다.