Groovy script deep dive
nGrinder μ€ν¬λ¦½νΈμλ λ€μν μ΄λ
Έν
μ΄μ
μ΄ μ¬μ©λ©λλ€. νΉν @BeforeProcess
, @BeforeThread
, @Before
, @Test
λ±μ μ΄λ
Έν
μ΄μ
μ΄ μ£Όμνκ² μ¬μ©λλ©°, κ°κ°μ μν μ μ΄ν΄νλ κ²μ΄ μ€μν©λλ€.
κΈ°λ³Έ κ°λ
μ 리
νλ‘μΈμ€μ μ€λ λ
νλ‘μΈμ€:
μ΄μ체μ μμ λ 립μ μΌλ‘ μ€νλλ νλ‘κ·Έλ¨.
κ° νλ‘μΈμ€λ κ³ μ ν λ©λͺ¨λ¦¬ 곡κ°μ κ°κ³ μ€νλ©λλ€.
nGrinderμμ νλμ λΆν ν μ€νΈ μ€νμ νλμ νλ‘μΈμ€λ‘ κ°μ£Όλ©λλ€.
μ€λ λ:
νλ‘μΈμ€ λ΄μμ μ€νλλ λ 립μ μΈ μμ νλ¦.
νλμ νλ‘μΈμ€μλ μ¬λ¬ κ°μ μ€λ λκ° ν¬ν¨λ μ μμ΅λλ€.
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