<HEADER, COOKIES, PARAM> 스크립트

2022. 6. 10. 16:29 성능테스트/nGrinder

* 헤더와 쿠키, 파라미터를 추가하는 스크립트 입니다. 이 작업은 스크립트 페이지에서 처음 '스크립트 만들기'를 할때도 '상세 설정 보기'에서 추가할 수 있습니다.
일부(cookies)는 해당 api에서는 필요하지 않은 데이터지만 샘플이니까 넣어봤습니다.

# -*- coding:utf-8 -*-
  
# 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 성능팀
from net.grinder.script.Grinder import grinder
from net.grinder.script import Test
from net.grinder.plugin.http import HTTPRequest
from net.grinder.plugin.http import HTTPPluginControl
from java.util import Date
from HTTPClient import NVPair, Cookie, CookieModule
  
control = HTTPPluginControl.getConnectionDefaults()
# if you don't want that HTTPRequest follows the redirection, please modify the following option 0.
# control.followRedirects = 1
# if you want to increase the timeout, please modify the following option.
control.timeout = 6000
  
test1 = Test(1, "Test1")
request1 = HTTPRequest()
  
# 헤더를 세팅
headers = [] # Array of NVPair
headers.append(NVPair("wmp-user-agent-type", "PC"))
headers.append(NVPair("Accept", "application/json"))
  
# 파라미터를 세팅
params = [] # Array of NVPair
params.append(NVPair("dealNo", "600001110"))
 
# 쿠키 세팅
cookies = [] # Array of Cookie
cookies.append(Cookie("setGM", ",20f68e3fc86aa9e990fcbe6eaacf77a9fd19ab1bfdca264d5142ac450fa1450c431a57d13b0fe638949de49dfbc3d835a15ad63bd83f8adf658451c38694b28d5fadd41ad18206a4ec910dabf0138426a17dd0cf060667da71b8d4740aea621ad202293ac969382087f45dad808049979a7bd37b58235d2b777f22fb8754f4dedd3d263181de6cf052f2d81b37ef519de786bd29d46cdef06823f54e755ea8e2884ac9b57ec3a2635ceb13df1417ccc44a602cf436c157335eafa9d697d070a1c5a55e0a6c326bb6c8a0ca9550741b7a28af8de830b4a1976e75318f82e7b982", "", "", Date(32503647599000L), 0))
  
class TestRunner:
    # initlialize a thread
    def __init__(self):
        test1.record(TestRunner.__call__)
        grinder.statistics.delayReports=True
        pass
      
    def before(self):
        request1.headers = headers
        for c in cookies: CookieModule.addCookie(c, HTTPPluginControl.getThreadHTTPClientContext())
  
    # test method     
    def __call__(self):
        self.before()
          
        result = request1.GET("http://172.19.26.248:8080/front/getDeal", params)
          
        if result.getStatusCode() == 200 :
            # reslut data
            grinder.logger.info(result.getText())
            return
        elif result.getStatusCode() in (301, 302) :
            grinder.logger.warn("Warning. The response may not be correct. The response code was %d." %  result.getStatusCode())
            return
        else :
            raise