The Grinder试用记录三-脚本编辑
如果懂Jython语法,那么操作脚本(包括参数化和关联)是非常简单的事情,下面这个脚本向blog.163.com发送一个dwr请求,并把response内容保存到文件。
代码

1 from net.grinder.script import Test
2 from net.grinder.script.Grinder import grinder
3 from net.grinder.plugin.http import HTTPPluginControl, HTTPRequest
4 from HTTPClient import NVPair
5 connectionDefaults = HTTPPluginControl.getConnectionDefaults()
6 httpUtilities = HTTPPluginControl.getHTTPUtilities()
7
8 # To use a proxy server, uncomment the next line and set the host and port.
9 # connectionDefaults.setProxyServer("localhost", 8001)
10
11 # These definitions at the top level of the file are evaluated once,
12 # when the worker process is started.
13
14 connectionDefaults.defaultHeaders = \
15 ( NVPair('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)'), )
16
17
18 headers3= \
19 ( NVPair('Accept', '*/*'),
20 NVPair('Referer', 'http://blog.163.com/'),
21 NVPair('Accept-Language', 'zh-c'), )
22
23 url7 = 'http://blog.163.com:80'
24
25 # Create an HTTPRequest for each request, then replace the
26 # reference to the HTTPRequest with an instrumented version.
27 # You can access the unadorned instance using request101.__target__.
28
29 request1001 = HTTPRequest(url=url7, headers=headers3)
30 request1001 = Test(1001, 'POST UserBean.getProvinceAndCity.dwr').wrap(request1001)
31
32 class TestRunner:
33 """A TestRunner instance is created for each worker thread."""
34
35 def page10(self):
36 """POST UserBean.getProvinceAndCity.dwr (request 1001)."""
37 result = request1001.POST('/dwr/call/plaincall/UserBean.getProvinceAndCity.dwr',
38 '''callCount=1\n\
39 page=/\n\
40 httpSessionId=\n\
41 scriptSessionId=4FC528EBDD341FE22046F074D587F3733\n\
42 c0-scriptName=UserBean\n\
43 c0-methodName=getProvinceAndCity\n\
44 c0-id=0\n\
45 c0-param0=boolean:false\n\
46 c0-param1=boolean:false\n\
47 batchId=0\n\
48 ''',
49 ( NVPair('Content-Type', 'text/plain'), ))
50
51 return result
52
53
54
55 def __call__(self):
56 """This method is called for every run performed by the worker thread."""
57
58 result1=self.page10() # POST UserBean.getProvinceAndCity.dwr (request 1001)
59 writeToFile(result1.getText())
60
61
62 def writeToFile(text):
63 filename = grinder.getFilenameFactory().createFilename(
64 "page", "-%d.html" % grinder.runNumber)
65
66 file = open(filename, "w")
67 print >> file, text
68 file.close()
69
70
71
2 from net.grinder.script.Grinder import grinder
3 from net.grinder.plugin.http import HTTPPluginControl, HTTPRequest
4 from HTTPClient import NVPair
5 connectionDefaults = HTTPPluginControl.getConnectionDefaults()
6 httpUtilities = HTTPPluginControl.getHTTPUtilities()
7
8 # To use a proxy server, uncomment the next line and set the host and port.
9 # connectionDefaults.setProxyServer("localhost", 8001)
10
11 # These definitions at the top level of the file are evaluated once,
12 # when the worker process is started.
13
14 connectionDefaults.defaultHeaders = \
15 ( NVPair('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)'), )
16
17
18 headers3= \
19 ( NVPair('Accept', '*/*'),
20 NVPair('Referer', 'http://blog.163.com/'),
21 NVPair('Accept-Language', 'zh-c'), )
22
23 url7 = 'http://blog.163.com:80'
24
25 # Create an HTTPRequest for each request, then replace the
26 # reference to the HTTPRequest with an instrumented version.
27 # You can access the unadorned instance using request101.__target__.
28
29 request1001 = HTTPRequest(url=url7, headers=headers3)
30 request1001 = Test(1001, 'POST UserBean.getProvinceAndCity.dwr').wrap(request1001)
31
32 class TestRunner:
33 """A TestRunner instance is created for each worker thread."""
34
35 def page10(self):
36 """POST UserBean.getProvinceAndCity.dwr (request 1001)."""
37 result = request1001.POST('/dwr/call/plaincall/UserBean.getProvinceAndCity.dwr',
38 '''callCount=1\n\
39 page=/\n\
40 httpSessionId=\n\
41 scriptSessionId=4FC528EBDD341FE22046F074D587F3733\n\
42 c0-scriptName=UserBean\n\
43 c0-methodName=getProvinceAndCity\n\
44 c0-id=0\n\
45 c0-param0=boolean:false\n\
46 c0-param1=boolean:false\n\
47 batchId=0\n\
48 ''',
49 ( NVPair('Content-Type', 'text/plain'), ))
50
51 return result
52
53
54
55 def __call__(self):
56 """This method is called for every run performed by the worker thread."""
57
58 result1=self.page10() # POST UserBean.getProvinceAndCity.dwr (request 1001)
59 writeToFile(result1.getText())
60
61
62 def writeToFile(text):
63 filename = grinder.getFilenameFactory().createFilename(
64 "page", "-%d.html" % grinder.runNumber)
65
66 file = open(filename, "w")
67 print >> file, text
68 file.close()
69
70
71
参考资料:
1、官方介绍资料:http://grinder.sourceforge.net/g3/scripts.html
2、Script API :http://grinder.sourceforge.net/g3/script-javadoc/index.html
3、官方例子:http://grinder.sourceforge.net/g3/script-gallery.html
4、Jython学习资料:https://www6.software.ibm.com/developerworks/cn/education/java/j-jython2/tutorial/