Skip to content

Instantly share code, notes, and snippets.

@gitjul
Created May 11, 2017 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitjul/91e9c7f2027375a822b80c6862ec7503 to your computer and use it in GitHub Desktop.
Save gitjul/91e9c7f2027375a822b80c6862ec7503 to your computer and use it in GitHub Desktop.
# The Grinder 3.10
# HTTP script recorded by TCPProxy at 2013-08-22 15:23:15
from net.grinder.script import Test
from net.grinder.script.Grinder import grinder
from net.grinder.plugin.http import HTTPPluginControl, HTTPRequest
from HTTPClient import NVPair
connectionDefaults = HTTPPluginControl.getConnectionDefaults()
httpUtilities = HTTPPluginControl.getHTTPUtilities()
# To use a proxy server, uncomment the next line and set the host and port.
# connectionDefaults.setProxyServer("localhost", 8001)
# These definitions at the top level of the file are evaluated once
# when the worker process is started.
connectionDefaults.defaultHeaders = \
[ NVPair('Accept-Encoding', 'gzip, deflate'),
NVPair('Accept-Language', 'en-US,en;q=0.5'),
NVPair('User-Agent', 'Mozilla/5.0 (X11; Linux i686; rv:23.0) Gecko/20100101 Firefox/23.0'), ]
headers0= \
[ NVPair('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'), ]
headers1= \
[ NVPair('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
NVPair('Referer', 'http://example.com/'), ]
url0 = 'http://example.com:80'
url1 = 'http://example2.com:80'
# Create an HTTPRequest for each request, then replace the
# reference to the HTTPRequest with an instrumented version.
# You can access the unadorned instance using request101.__target__.
request101 = HTTPRequest(url=url0, headers=headers0)
request101 = Test(101, 'GET /').wrap(request101)
request102 = HTTPRequest(url=url0, headers=headers0)
request102 = Test(102, 'GET favicon.ico').wrap(request102)
request201 = HTTPRequest(url=url1)
request201 = Test(201, 'GET 994ba82a').wrap(request201)
request301 = HTTPRequest(url=url0, headers=headers1)
request301 = Test(301, 'POST user_sessions').wrap(request301)
request302 = HTTPRequest(url=url0, headers=headers1)
request302 = Test(302, 'GET home').wrap(request302)
request401 = HTTPRequest(url=url1)
request401 = Test(401, 'GET 994ba82a').wrap(request401)
class TestRunner:
"""A TestRunner instance is created for each worker thread."""
# A method for each recorded page.
def page1(self):
"""GET / (requests 101-102)."""
result = request101.GET('/')
grinder.sleep(183)
request102.GET('/favicon.ico')
return result
def page2(self):
"""GET 994ba82a (request 201)."""
self.token_a = \
'127127'
self.token_be = \
'19098'
self.token_qt = \
'14'
self.token_ap = \
'532'
self.token_dc = \
'678'
self.token_fe = \
'1917'
self.token_to = \
'blah'
self.token_v = \
'42'
self.token_jsonp = \
'NREUM.setToken'
result = request201.GET('/994ba82a' +
'?a=' +
self.token_a +
'&be=' +
self.token_be +
'&qt=' +
self.token_qt +
'&ap=' +
self.token_ap +
'&dc=' +
self.token_dc +
'&fe=' +
self.token_fe +
'&to=' +
self.token_to +
'&v=' +
self.token_v +
'&jsonp=' +
self.token_jsonp)
return result
def page3(self):
"""POST user_sessions (requests 301-302)."""
# Expecting 302 'Found'
result = request301.POST('/user_sessions',
( NVPair('utf8', '✓'),
NVPair('authenticity_token', 'token'),
NVPair('user_session[login]', 'user'),
NVPair('user_session[password]', 'password'),
NVPair('commit', 'Login'), ),
( NVPair('Content-Type', 'application/x-www-form-urlencoded'), ))
grinder.sleep(55)
request302.GET('/home')
return result
def page4(self):
"""GET 994ba82a (request 401)."""
self.token_be = \
'2156'
self.token_qt = \
'19'
self.token_ap = \
'1095'
self.token_dc = \
'21345'
self.token_fe = \
'22795'
self.token_to = \
'blah'
result = request401.GET('/994ba82a' +
'?a=' +
self.token_a +
'&be=' +
self.token_be +
'&qt=' +
self.token_qt +
'&ap=' +
self.token_ap +
'&dc=' +
self.token_dc +
'&fe=' +
self.token_fe +
'&to=' +
self.token_to +
'&v=' +
self.token_v +
'&jsonp=' +
self.token_jsonp)
return result
def __call__(self):
"""Called for every run performed by the worker thread."""
self.page1() # GET / (requests 101-102)
grinder.sleep(1780)
self.page2() # GET 994ba82a (request 201)
grinder.sleep(13973)
self.page3() # POST user_sessions (requests 301-302)
grinder.sleep(85)
self.page4() # GET 994ba82a (request 401)
def instrumentMethod(test, method_name, c=TestRunner):
"""Instrument a method with the given Test."""
unadorned = getattr(c, method_name)
import new
method = new.instancemethod(test.wrap(unadorned), None, c)
setattr(c, method_name, method)
# Replace each method with an instrumented version.
# You can call the unadorned method using self.page1.__target__().
instrumentMethod(Test(100, 'Page 1'), 'page1')
instrumentMethod(Test(200, 'Page 2'), 'page2')
instrumentMethod(Test(300, 'Page 3'), 'page3')
instrumentMethod(Test(400, 'Page 4'), 'page4')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment