sample.py


# -*- 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, HTTPPluginControl

from HTTPClient import Cookie, CookieModule

from java.util import Date

from HTTPClient import NVPair


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, "192.168.0.78")

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):

# Now let's add a new cookie.

threadContext = HTTPPluginControl.getThreadHTTPClientContext()

expiryDate = Date()

expiryDate.year += 10

cookie = Cookie("JSESSIONID", "5B586D37CC93E50CAEAECC726ECE11F4","192.168.0.78", "/", expiryDate, 0)

CookieModule.addCookie(cookie, threadContext)

        

request1.setHeaders([NVPair("x-requested-with", "XMLHttpRequest")])

result = request1.GET("http://192.168.0.78:8080/")

# 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




참조 - http://grinder.sourceforge.net/g3/script-gallery.html#cookies.py, http://junoyoon.tistory.com/entry/nGrinder-POST-%EB%A9%94%EC%8B%9C%EC%A7%80-%EB%B3%B4%EB%82%B4%EA%B8%B0

'IDE & Build > Test' 카테고리의 다른 글

ngrinder - POST, parameter, sleep  (0) 2014.12.10
Posted by linuxism
,