Stress Simulation
Code(groovy)
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
import HTTPClient.Cookie
import HTTPClient.CookieModule
import net.grinder.plugin.http.HTTPPluginControl
import groovy.json.JsonOutput
/**
* 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 HTTPRequest loginRequest
public static Map<String, String> headers = [:]
public static Map<String, String> loginHeaders = [:]
public Object cookies = []
public static String sessionId
@BeforeProcess
public static void beforeProcess() {
HTTPRequestControl.setConnectionTimeout(300000)
test = new GTest(1, "purchase test")
request = new HTTPRequest()
// Set header data
headers.put("Content-Type", "application/json")
test.record(request);
grinder.logger.info("before process.")
}
@BeforeThread
public void beforeThread() {
grinder.statistics.delayReports=true;
test.record(this, "test")
grinder.logger.info("before thread.");
}
@Before
public void before() {
// reset to the all cookies
def threadContext = HTTPPluginControl.getThreadHTTPClientContext()
cookies = CookieModule.listAllCookies(threadContext)
cookies.each {
CookieModule.removeCookie(it, threadContext)
}
Map<String, Object> paramData = new HashMap<>();
int randomNum = Math.abs(new Random().nextInt() % 10000) + 1 //
String email = Integer.toString(randomNum) + "@yousinsa.com" //
paramData.put("userEmail", email);
paramData.put("userPassword", "password");
String json = JsonOutput.toJson(paramData);
loginRequest.setHeaders(loginHeaders);
HTTPResponse response = request.POST("<http://49.50.164.137:8080/api/v1/users/sign-in>", json.getBytes())
cookies = CookieModule.listAllCookies(threadContext)
cookies.each {
CookieModule.addCookie(it ,threadContext);
grinder.logger.info("{}", it);
}
}
@Test
public void test() {
Map<String, Object> paramData = new HashMap<>();
int randomOptionId = Math.abs(new Random().nextInt() % 90000) + 1 //
int randomAmount = Math.abs(new Random().nextInt() % 10) + 1
paramData.put("productOptionId", randomOptionId);
paramData.put("purchaseOrderAmount", randomAmount);
String json = JsonOutput.toJson(paramData);
HTTPResponse response = request.POST("<http://49.50.164.137:8080/api/v1/orders>", json.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))
}
}
}
분석 결과
Data