Groovy Script Structure

nGrinder에서 기본 스크립트를 생성했을 때 나오는 기본 스크립트는 Java로 작성되었지만, Groovy 스타일로 보일 수 있다. 실제로 nGrinder는 JUnit을 기반으로 Java 언어를 사용하지만, Groovy 스타일로 작성된 GrinderRunner와 같은 주석 및 어노테이션을 포함하고 있다.

nGrinder Groovy 테스트를 위해서는 클래스 위에 @RunWith(GrinderRunner) 애너테이션을 꼭 붙여주어야 한다. 이 부분은 GroovyRunner가 JUnit의 행동을 제어하며 JUnit에 grinder context를 마운트 한다.

nGrinder는 기본적으로 Groovy 기반의 JUnit 테스트 프레임워크를 사용한다. 따라서 기본 스크립트는 Groovy와 Java의 조합으로 작성되며, Groovy 언어의 간결함과 Java의 강력함을 동시에 활용할 수 있다.

기본 스크립트 분석

스크립트 구조 및 분석:

import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
import net.grinder.script.GTest
import net.grinder.script.Grinder
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith

import org.ngrinder.http.HTTPRequest
import org.ngrinder.http.HTTPRequestControl
import org.ngrinder.http.HTTPResponse
import org.ngrinder.http.cookie.Cookie
import org.ngrinder.http.cookie.CookieManager

/**
 * A simple example using the HTTP plugin that shows the retrieval of a single page via HTTP.
 *
 * This script is automatically generated by ngrinder.
 *
 * @author admin
 */
@RunWith(GrinderRunner)
class TestRunner {

    public static GTest test
    public static HTTPRequest request
    public static Map<String, String> headers = [:]
    public static Map<String, Object> params = [:]
    public static List<Cookie> cookies = []

    @BeforeProcess
    public static void beforeProcess() {
        HTTPRequestControl.setConnectionTimeout(300000)
        test = new GTest(1, "airdrop-api.dev.memecore.org")
        request = new HTTPRequest()
        grinder.logger.info("before process.")
    }

    @BeforeThread
    public void beforeThread() {
        test.record(this, "test")
        grinder.statistics.delayReports = true
        grinder.logger.info("before thread.")
    }

    @Before
    public void before() {
        request.setHeaders(headers)
        CookieManager.addCookies(cookies)
        grinder.logger.info("before. init headers and cookies")
    }

    @Test
    public void test() {
        HTTPResponse response = request.GET("https://airdrop-api.dev.memecore.org", params)

        if (response.statusCode == 301 || response.statusCode == 302) {
            grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", response.statusCode)
        } else {
            assertThat(response.statusCode, is(200))
        }
    }
}

주요 부분 설명

  1. 어노테이션 및 클래스 주석

    • @RunWith(GrinderRunner): GrinderRunner를 사용해 테스트 실행.

    • @BeforeProcess, @BeforeThread, @Before, @Test 어노테이션을 사용하여 테스트 라이프사이클을 정의.

  2. 테스트 라이프사이클 함수

    • @BeforeProcess: 전체 프로세스 시작 전 한 번 실행.

    • @BeforeThread: 각 스레드 시작 전 한 번 실행.

    • @Before: 각 테스트 전마다 실행.

    • @Test: 테스트 함수로, 부하 테스트를 수행하는 핵심 코드 작성.

  3. 주요 객체 및 플러그인 사용

    • GTest: 테스트 객체 생성.

    • HTTPRequest, HTTPResponse: HTTP 요청 및 응답을 처리하는 객체.

    • Cookie, CookieManager: 쿠키 관리 객체.

요약:

  • 기본적으로 nGrinder에서 생성되는 스크립트는 Java와 Groovy 스타일의 조합으로, Groovy를 기반으로 JUnit 테스트 프레임워크를 사용.

  • GrinderRunner와 어노테이션을 통해 테스트 라이프사이클을 관리하며, 주요 플러그인과 객체를 활용해 부하 테스트를 작성.

Q1: 스크립트에서 부하 테스트 시나리오를 어떻게 확장할 수 있을까?

Q2: Groovy 스타일의 스크립트 작성 시 다른 라이브러리를 사용하려면 어떻게 해야 하나?

Q3: 각 어노테이션의 역할을 구체적으로 설명할 수 있나?

Last updated