Groovy script deep dive

nGrinder μŠ€ν¬λ¦½νŠΈμ—λŠ” λ‹€μ–‘ν•œ μ–΄λ…Έν…Œμ΄μ…˜μ΄ μ‚¬μš©λ©λ‹ˆλ‹€. 특히 @BeforeProcess, @BeforeThread, @Before, @Test λ“±μ˜ μ–΄λ…Έν…Œμ΄μ…˜μ΄ μ£Όμš”ν•˜κ²Œ μ‚¬μš©λ˜λ©°, 각각의 역할을 μ΄ν•΄ν•˜λŠ” 것이 μ€‘μš”ν•©λ‹ˆλ‹€.

κΈ°λ³Έ κ°œλ… 정리

ν”„λ‘œμ„ΈμŠ€μ™€ μŠ€λ ˆλ“œ

  1. ν”„λ‘œμ„ΈμŠ€:

    • μš΄μ˜μ²΄μ œμ—μ„œ λ…λ¦½μ μœΌλ‘œ μ‹€ν–‰λ˜λŠ” ν”„λ‘œκ·Έλž¨.

    • 각 ν”„λ‘œμ„ΈμŠ€λŠ” κ³ μœ ν•œ λ©”λͺ¨λ¦¬ 곡간을 κ°–κ³  μ‹€ν–‰λ©λ‹ˆλ‹€.

    • nGrinderμ—μ„œ ν•˜λ‚˜μ˜ λΆ€ν•˜ ν…ŒμŠ€νŠΈ 싀행은 ν•˜λ‚˜μ˜ ν”„λ‘œμ„ΈμŠ€λ‘œ κ°„μ£Όλ©λ‹ˆλ‹€.

  2. μŠ€λ ˆλ“œ:

    • ν”„λ‘œμ„ΈμŠ€ λ‚΄μ—μ„œ μ‹€ν–‰λ˜λŠ” 독립적인 μž‘μ—… 흐름.

    • ν•˜λ‚˜μ˜ ν”„λ‘œμ„ΈμŠ€μ—λŠ” μ—¬λŸ¬ 개의 μŠ€λ ˆλ“œκ°€ 포함될 수 μžˆμŠ΅λ‹ˆλ‹€.

    • nGrinder의 각 μŠ€λ ˆλ“œλŠ” λ³‘λ ¬λ‘œ λΆ€ν•˜λ₯Ό μƒμ„±ν•˜λŠ” λ‹¨μœ„μž…λ‹ˆλ‹€.

μ–΄λ…Έν…Œμ΄μ…˜ μ—­ν•  μ„€λͺ…

1. @BeforeProcess

  • μ—­ν• : ν”„λ‘œμ„ΈμŠ€ μ „μ²΄μ—μ„œ ν•œ 번만 μ‹€ν–‰λ©λ‹ˆλ‹€.

  • μ‚¬μš© μ‹œμ : λͺ¨λ“  μŠ€λ ˆλ“œκ°€ μ‹€ν–‰λ˜κΈ° 전에 ν”„λ‘œμ„ΈμŠ€ λ‹¨μœ„λ‘œ ν•„μš”ν•œ 섀정을 ν•  λ•Œ μ‚¬μš©.

  • μ˜ˆμ‹œ μ½”λ“œ:

@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.")
}

2. @BeforeThread

  • μ—­ν• : 각 μŠ€λ ˆλ“œκ°€ μ‹œμž‘λ˜κΈ° 전에 μ‹€ν–‰λ©λ‹ˆλ‹€.

  • μ‚¬μš© μ‹œμ : 각 μŠ€λ ˆλ“œλ³„ μ΄ˆκΈ°ν™” μž‘μ—…μ„ ν•  λ•Œ μ‚¬μš©.

  • μ˜ˆμ‹œ μ½”λ“œ:

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

3. @Before

  • μ—­ν• : 각 ν…ŒμŠ€νŠΈ λ©”μ„œλ“œ μ‹€ν–‰ 전에 μ‹€ν–‰λ©λ‹ˆλ‹€.

  • μ‚¬μš© μ‹œμ : 각 ν…ŒμŠ€νŠΈμ—μ„œ κ³΅ν†΅μ μœΌλ‘œ ν•„μš”ν•œ 섀정을 ν•  λ•Œ μ‚¬μš©.

  • μ˜ˆμ‹œ μ½”λ“œ:

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

4. @Test

  • μ—­ν• : μ‹€μ œ ν…ŒμŠ€νŠΈλ₯Ό μˆ˜ν–‰ν•˜λŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€.

  • μ‚¬μš© μ‹œμ : ν…ŒμŠ€νŠΈν•˜κ³ μž ν•˜λŠ” λΆ€ν•˜ ν…ŒμŠ€νŠΈ μ½”λ“œλ₯Ό μž‘μ„±ν•  λ•Œ μ‚¬μš©.

  • μ˜ˆμ‹œ μ½”λ“œ:

@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))
    }
}

λΆ€ν•˜ ν…ŒμŠ€νŠΈ ꡬ쑰 μš”μ•½

  • ν”„λ‘œμ„ΈμŠ€: ν•˜λ‚˜μ˜ ν…ŒμŠ€νŠΈ μ‹€ν–‰ λ‹¨μœ„.

    • @BeforeProcess둜 초기 μ„€μ • μˆ˜ν–‰.

  • μŠ€λ ˆλ“œ: ν”„λ‘œμ„ΈμŠ€ λ‚΄μ—μ„œ λ³‘λ ¬λ‘œ μ‹€ν–‰λ˜λŠ” λΆ€ν•˜ λ‹¨μœ„.

    • @BeforeThread둜 μŠ€λ ˆλ“œλ³„ μ΄ˆκΈ°ν™” μž‘μ—… μˆ˜ν–‰.

  • ν…ŒμŠ€νŠΈ: 각 μŠ€λ ˆλ“œκ°€ μˆ˜ν–‰ν•˜λŠ” κ°œλ³„ ν…ŒμŠ€νŠΈ μž‘μ—….

    • @Before둜 각 ν…ŒμŠ€νŠΈμ— ν•„μš”ν•œ 곡톡 μ„€μ • μˆ˜ν–‰.

    • @Test둜 μ‹€μ œ λΆ€ν•˜ ν…ŒμŠ€νŠΈ μ½”λ“œλ₯Ό μž‘μ„±.

μ–΄λ…Έν…Œμ΄μ…˜μ„ μ΄μš©ν•œ ν…ŒμŠ€νŠΈ 슀크립트 μ˜ˆμ‹œ

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.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

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

@RunWith(GrinderRunner)
class TestRunner {

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

    @BeforeProcess
    public static void beforeProcess() {
        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([])
        grinder.logger.info("Before Test. Initialize 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))
        }
    }
}

μΆ”κ°€ 질문

Q1: μŠ€ν¬λ¦½νŠΈμ—μ„œ ν”„λ‘œμ„ΈμŠ€μ™€ μŠ€λ ˆλ“œμ˜ 개수λ₯Ό μ–΄λ–»κ²Œ μ‘°μ ˆν•˜λ‚˜?

Q2: 각 ν…ŒμŠ€νŠΈ λ©”μ„œλ“œμ˜ μ‹€ν–‰ κ²°κ³Όλ₯Ό λ ˆν¬νŠΈμ— ν‘œμ‹œν•˜λŠ” 방법은 무엇인가?

Q3: nGrinderμ—μ„œ μŠ€λ ˆλ“œ κ°„ 데이터λ₯Ό κ³΅μœ ν•˜κ±°λ‚˜ ν”„λ‘œμ„ΈμŠ€ κ°„ 데이터λ₯Ό κ³΅μœ ν•  수 μžˆλ‚˜?

Last updated