[Ngrinder] 스크립트 작성

may_yun·2023년 3월 17일
0

controller 스크립트 직접 작성

  1. 스크립트 작성
    두가지 방법 존재
  • Firefox 프록시 설정으로 TCPProxy Console 인터넷 액션을 수행하여 스크립트를 생성하는 방법
    (단 이방법은 Jython으로 생성되어 파악하기 힘들고, WAS만 테스트 하고 싶어도 .html, .js까지 모두 요청이 들어간다는 단점이 있다)

  • controller 스크립트 직접 작성

직접 작성

  1. nGrinder controller 웹에 접속하여 스크립트 만들기를 클릭한다.
  • 우상단 script 선택

  1. 스크립트 생성
    : 그럼 예시 스크립트가 나옵니다. 이를 커스텀해서 사용하면 됩니다. 자바를 사용하셨다면 생긴 것이 비슷하여 나름 쉽게 작성하실 수 있습니다.

  • 자동생성 스크립트
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, "Test1")
		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("http://please_modify_this.com", 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))
		}
	}
}

스크립트 작성

: nGrinder 3.2부터 스크립트 언어로써 Groovy를 지원하기 시작했습니다. Groovy 스크립트는 JUnit에 기반하여 동작합니다.

⭐️ 주의 할 점

  • nGrinder Groovy 테스트를 위해서는 클래스 위에 @RunWith(GrinderRunner) 애너테이션을 꼭 붙여주어야 합니다. 이 부분은 GroovyRunner가 JUnit의 행동을 제어하며 JUnit에 grinder context를 마운트 합니다.
  • @Test 애너테이션이 붙은 메서드에는 각자 수행되어할 행동을 정의합니다.
profile
개발 일지

0개의 댓글