QuickPerf Testing with Junit5


들어가기전

QuickPerf 는 오픈소스 테스팅 라이브러리다.

  • Java Performance 검증
  • Hibernate 에서 실제로 쿼리가 몇 번 수행됐는지 테스트 코드 검증 지원

QuickPerf is a testing library for Java to quickly evaluate and improve some performance-related properties

QuickPerf 의 활용법은 다양하지만, 특히 N+1이 발생하는 조회 쿼리 튜닝할 때 용이하다.

Dependency

  • org.quickperf:quick-perf-junit5
  • org.quickperf:quick-perf-springboot2-sql-starter
<dependency>
	<groupId>org.quickperf</groupId>
	<artifactId>quick-perf-junit5</artifactId>
	<version>1.1.0</version>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>org.quickperf</groupId>
	<artifactId>quick-perf-springboot2-sql-starter</artifactId>
	<version>1.1.0</version>
	<scope>test</scope>
</dependency>

Config

설정 방법은 두 가지로 @ExtendWith 방법을 추천한다.

  • junit-platform.properties
  • @ExtendWith

junit-platform.properties

# src/test/resources/junit-platform.properties
junit.jupiter.extensions.autodetection.enabled=true

@ExtendWith Annotation

@ExtendWith(org.quickperf.junit5.QuickPerfTestExtension.class)

Test

@DataJpaTest

  • @Import org.quickperf.spring.sql.QuickPerfSqlConfig
  • @OverrideAutoConfiguration(enabled = true)
@DataJpaTest
@ExtendWith(QuickPerfTestExtension.class)
@Import(QuickPerfSqlConfig.class)
class BasicRepositoryTest { }

@DataJpaTest
@ExtendWith(QuickPerfTestExtension.class)
@OverrideAutoConfiguration(enabled = true)
class BasicRepositoryTest { }

@SpringBootTest

@SpringBootTest
@ExtendWith(QuickPerfTestExtension.class)
class BasicServiceTest { }

Sample Test Code

@DataJpaTest
@ExtendWith(QuickPerfTestExtension.class)
@Import(QuickPerfSqlConfig.class)
class TeamRepositoryTest extends InitTestDataExecutor {
  @Autowired
  TeamRepository repository;

  @Test
  @ExpectSelect(7)
  void testFindAll() {
    // given
    List<Team> teams = repository.findAll();

    // when then
    for (Team team : teams) {
      for (Member member : team.getMembers()) {
        MemberOption memberOption = member.getMemberOption();
        assertThat(memberOption.isEnabled()).isTrue();
      }
    }
  }
}

이외 다양한 테스트 샘플 코드는 Github QuickPerf 를 참고하자.

QuickPerf @Annotations

org.quickperf.sql.annotation.* 패키지의 어노테이션을 참고하자.

QuickPerf Github - wiki

  • @ExpectInsert
  • @ExpectMaxInsert
  • @ExpectUpdate
  • @ExpectUpdatedColumn
  • @ExpectMaxUpdate
  • @ExpectMaxUpdatedColumn
  • @ExpectSelect
  • @ExpectSelectedColumn
  • @ExpectMaxSelect
  • @ExpectMaxSelectedColumn
  • @ExpectDelete
  • @ExpectMaxDelete

Reference