최종 프로젝트

QueryDSL vs 로컬 Elastic Search vs Open Search(AWS 퍼블릭)

phonebee 2025. 4. 25. 20:09

▶ 테스트 방식

10000건의 더미 데이터 생성 후 각 검색 기능 마다 검색 속도 비교

<테스트 코드>

@SpringBootTest
@ActiveProfiles("test")
public class AuctionSearchBenchmarkTest {

    @Autowired
    private AuctionRepository auctionRepository;

    @Autowired
    private AuctionSearchRepository auctionSearchRepository;

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private AuctionSearchService auctionSearchService;

    @Autowired
    private AuctionOpenSearchService auctionOpenSearchService;

    @BeforeEach
    void cleanUp(){
        auctionRepository.deleteAll();
        auctionSearchRepository.deleteAll();
        productRepository.deleteAll();
    }

    @Test
    @Transactional
    public void generateDummyAuctions(){
        User user = userRepository.findAll().stream().findFirst()
                .orElseThrow(()-> new RuntimeException("No user found"));

        for(int i=0; i<10000; i++){

            Product product = new Product(
                    user,
                    "Test Product "+i,
                    "content",
                    ProductCategory.SHOES
            );
            productRepository.save(product);

            Auction auction = new Auction(
                    product,
                    1000L + i,
                    LocalDateTime.now().plusHours(i),
                    60L
            );
            auctionRepository.save(auction);
            auctionSearchRepository.save(AuctionMapper.toDucument(auction));

            //OpenSearch 인덱싱 추가
            AuctionDocument document = AuctionDocument.builder()
                    .id(auction.getId())
                    .productName(auction.getProduct().getProductName())
                    .category(auction.getProduct().getCategory().name())
                    .minPrice(auction.getMinPrice())
                    .startTime(auction.getStartTime().toString())
                    .endTime(auction.getEndTime().toString())
                    .build();

            try{
                auctionOpenSearchService.save(document);
            }catch (IOException e){
                System.out.println("OpenSearch 인덱싱 실패: {"+e.getMessage()+"}");
            }
        }

        System.out.println("10000건의 더미 경매가 생성되었습니다.");
    }

    @Test
    public void benchmarkSearch() throws IOException {
        int size=10;
        int page=1;

        Pageable pageable = PageRequest.of(page-1, size);
        String keyword = "testproduct";
        String category = "SHOES";

        //QueryDSL을 사용한 검색 기능
        long startTime1 = System.currentTimeMillis();
        auctionRepository.findBySearch(keyword, category, pageable);
        long endTime1 = System.currentTimeMillis();
        System.out.println("QueryDSL 검색 소요 시간: "+(endTime1-startTime1)+"ms");

        //Elastic Search를 사용한 검색 기능
        long startTime2 = System.currentTimeMillis();
        auctionSearchService.searchAuctions(keyword, category, pageable);
        long endTime2 = System.currentTimeMillis();
        System.out.println("Elastic Search 검색 소요 시간: "+(endTime2-startTime2)+"ms");

        //OpenSearch를 사용한 검색 기능
        long startTime3 = System.currentTimeMillis();
        List<AuctionDocument> result = auctionOpenSearchService.search(keyword, category, page, size);
        long endTime3 = System.currentTimeMillis();
        System.out.println("OpenSearch 검색 소요 시간: "+(endTime3-startTime3)+"ms");
    }
}

 

▶ 테스트 결과

QueryDSL 검색 소요 시간: 388ms
Elastic Search 검색 소요 시간: 245ms
OpenSearch 검색 소요 시간: 528ms

 

여기서 드는 의문점은 어째서 OpenSearch의 소요 시간이 남들보다 훨씬 더 걸리는가 이다.

 

▶ 원인 분석

원인은 AWS 외부와 통신이 필요한 구조이기 때문이다.

로컬 Elastic Search은 로컬이기 때문에 네트워크 지연이 없지만

AWS 외부와 통신을 하기 때문에 외부 호출로 인해서 시간 소요가 증가한다.

 

※ 단, 퍼블릭이 아닌 EC2 내부에서 호출하게 되면 속도가 훨씬 개선된다고 한다.