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