프로젝트를 진행하면서 프론트가 실수로 한번에 요청을 몇천번 보내는 일이 잦았고, 그로인해 서버의 cpu가 100퍼를 찍으며 죽는 일이 자주 발생하였다. 서버에 많은 요청이 들어와도 안정성을 높이기 위해 성능테스트가 필요하다고 판단 하였고 ngrinder를 이용하여 성능테스트를 진행하기로 했다.
https://github.com/naver/ngrinder/releases
war 파일을 다운로드 한다.
java -jar ngrinder-controller-3.5.5-p1.war --port 7070
로 실행하면
localhost:7070 으로 접속하면

초기 아이디: admin
초기 비번: admin
으로 접속
// 압축 해제
tar -xvf ngrinder-agent-{version}-localhost.tar
// 폴더로 이동
cd ngrinder-agent
// 쉘파일 실행
./run_agent.sh

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 String body = "{\n \"questionId\": 1,\n \"pickedId\": 4\n}"
public static List<Cookie> cookies = []
@BeforeProcess
public static void beforeProcess() {
HTTPRequestControl.setConnectionTimeout(300000)
test = new GTest(1, "43.203.223.211")
request = new HTTPRequest()
// Set header data
headers.put("Content-Type", "application/json")
headers.put("Authorization", "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI0Iiwicm9sZSI6IkFETUlOIiwiaWF0IjoxNzMxMjI5NTg4LCJleHAiOjE3MzQ4Mjk1ODh9.eT42SAcdxGgOr-rPbWIf7HZwJ0QQhp0_hLaqQ9IizYU")
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.POST("http://43.203.223.211:8080/api/answer/common", body.getBytes())
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))
}
}
}
위와 같이 스크립트를 작성하여 테스트 할 수 있다.
groovy로 제작 하였고, api와 헤더를 넣을 수 있다.

이후 Performance Test에서 Create Test 를 누르면 셋팅할 수 있는 화면을 보여준다.
각각은 다음과 같다

위와 같이 성능 테스트가 돌아가는 것을 확인 할 수 있다.