nGrinder 는 각각의 성능테스트를 위한 내용을 Groovy, Jython 등의 스크립트를 이용해 작성할 수 있도록 지원한다.
아래의 스크립트는 Jython Script 로 JSON-RPC 서버를 호출하여 성능테스트를 하기위한 간단 샘플이다.
nGrinder 제공 클래스들은http://grinder.sourceforge.net/g3/script-javadoc/overview-summary.html
에서 확인이 가능하다.
(HTTP Request 바로 가기 : http://grinder.sourceforge.net/g3/script-javadoc/net/grinder/plugin/http/HTTPRequest.html)
# -*- 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 admin 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 HTTPClient import NVPair from java.util import Random #from org.json import JSONObject 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, "api.your_domain.com") request1 = HTTPRequest() # Make any method call on request1 increase TPS test1.record(request1) class TestRunner: # initlialize a thread def __init__(self): grinder.statistics.delayReports=True pass # test method def __call__(self): # headers = ( NVPair( "Content-Type", "application/json" ), ) # request1.setHeaders(headers) url = "http://api.your_domain.com/your_api_path" rndVal=str(Random().nextInt(2147483647)) rndVal=rndVal.rjust(11, '0') #random.randint(10000000000, 20000000000) jsonMessage = '{"id" : "1", "jsonrpc" : "2.0", "method" : "api.encrypt", "params" : {"type": "D", "val" : "' + (rndVal + "\"") + '}}' #""" #result = request1.POST(url, jsonMessage) result = request1.POST(url, jsonMessage, (NVPair('Content-Type', 'application/json'), )) #result = request1.GET("http://api.your_domain.com/your_api_path?id=1&jsonrpc=2.0&method=api.encrypt¶ms=%7B%22type%22%3A%22D%22%2C%22val%22%3A%220123456%22%7D")
#grinder.logger.info('---------------------------------------------------------------') grinder.logger.info(result.getText()) #grinder.logger.info('---------------------------------------------------------------')
# You get the message body using the getText() method. # if result.getText().find("HELLO WORLD") != -1 : # grinder.statistics.forLastTest.success = 1 # else : # grinder.statistics.forLastTest.success = 0
# if you want to print out log.. Don't use print keyword. Instead, use following. #grinder.logger.info("Hello World")
if result.getStatusCode() == 200 : grinder.statistics.forLastTest.success = 1 elif result.getStatusCode() in (301, 302) : grinder.logger.warn("Warning. The response may not be correct. The response code was %d." % result.getStatusCode()) grinder.statistics.forLastTest.success = 1 else : grinder.statistics.forLastTest.success = 0 |
* nGrinder 설치는 http://itraveler.tistory.com/16 참조.