Grinder运行脚本 我所了解的有三种
1. 单个运行
这个适合在调试,测试代码的时候用,方便,快捷。
grinder.properties 文件如下
**grinder.properties** # # helloworld grinder.properties # #以下为你的环境配置 grinder.jvm.arguments=-Dpython.home=c::/devTools/jython grinder.jvm.arguments=-Dpython.path=c:/grinder/bin grinder.jvm.arguments=-Dpython.verbose=debug grinder.processes=1 grinder.threads=1 grinder.runs=1 #grinder.useConsole=false grinder.logDirectory=log grinder.numberOfOldLogs=0 #grinder.initialSleepTime=500 #grinder.sleepTimeFactor=0.01 #grinder.sleepTimeVariation=0.005 #sequence.py 就是你要运行的单个的脚步 grinder.script=sequence.py grinder.useConsole = true
2.Run test scripts in sequence(按顺序运行脚本)
这个方法适合完成一个project后,一起运行。
grinder.properties 文件如下
**grinder.properties** # # helloworld grinder.properties # grinder.jvm.arguments=-Dpython.home=c::/devTools/jython grinder.jvm.arguments=-Dpython.path=c:/grinder/bin grinder.jvm.arguments=-Dpython.verbose=debug grinder.processes=1 grinder.threads=1 grinder.runs=1 #grinder.useConsole=false grinder.logDirectory=log grinder.numberOfOldLogs=0 #grinder.initialSleepTime=500 #grinder.sleepTimeFactor=0.01 #grinder.sleepTimeVariation=0.005 #下面2个就是你要运行的脚步 script1=ebayUSForumsSimpleSearch script2=ebayUSForumsAdvancedSearch #mian.py 是控制顺序运行的脚步的脚步 grinder.script=main.py grinder.useConsole = true
main.py
# Run test scripts in sequence # # Scripts are defined in Python modules (helloworld.py, goodbye.py) # specified in grinder.properties: # # script1=helloworld # script2=goodbye from net.grinder.script.Grinder import grinder from java.util import TreeMap # TreeMap is the simplest way to sort a Java map. scripts = TreeMap(grinder.properties.getPropertySubset("script")) # Ensure modules are initialised in the process thread. for module in scripts.values(): exec("import %s" % module) def createTestRunner(module): exec("x = %s.TestRunner()" % module) return x class TestRunner: def __init__(self): self.testRunners = [createTestRunner(m) for m in scripts.values()] # This method is called for every run. def __call__(self): for testRunner in self.testRunners: testRunner()
这样的话,就可以顺序运行了。
3.Run test scripts in parallel(同时并行运行脚本)
个人认为这个方法有很多优点,可以更有效的进行压力测试
grinder.properties 文件如下
**grinder.properties** # # helloworld grinder.properties # grinder.jvm.arguments=-Dpython.home=c::/devTools/jython grinder.jvm.arguments=-Dpython.path=c:/grinder/bin grinder.jvm.arguments=-Dpython.verbose=debug grinder.processes=1 #如果你要同时运行几个脚本,那么下面的grinder.threads的值应该>=脚本数。 grinder.threads=4 grinder.runs=1 #grinder.useConsole=false grinder.logDirectory=log grinder.numberOfOldLogs=0 #grinder.initialSleepTime=500 #grinder.sleepTimeFactor=0.01 #grinder.sleepTimeVariation=0.005 grinder.script=main.py grinder.useConsole = true
main.py
from net.grinder.script.Grinder import grinder scripts = ["helloworld", "good"] #If you want to add new scripts you have two ways. # 1.you can insert directly such as scripts = ["helloworld", "good"] # 2.or you can do like this: scripts.append('xxx') # Ensure modules are initialised in the process thread. for script in scripts: exec("import %s" % script) # import the scripts files such as helloworld.py good.py def createTestRunner(script): exec("x = %s.TestRunner()" % script) return x class TestRunner: def __init__(self): #script = scripts[grinder.threadID % len(scripts)] # it's ok on Grinder 3.0.1, but is not ok on Grinder 3.1 script = scripts[grinder.getThreadNumber() % len(scripts)] #it's ok on 3.1 but not 3.0.1 #script = scripts[grinder.threadNumber % len(scripts)] #it's ok on 3.1 but not 3.0.1 # If you set grinder.threads=n(in grinder.properties), the the ID is 1,2,3. # If ID=1, then 1%len(scripts)==1( there are 3 elements in scripts, so len(scripts) is 3, so 1%3==1) # so script=scripts[1], so script='ebayUSForumsGuestViewForum' # so thread 1 run ebayUSForumsGuestViewForum.py # If ID=2, then 2%len(scripts)==2( there are 3 elements in scripts, so len(scripts) is 3, so 2%3==2) # so script=scripts[2], so script='ebayUSForumsGuestViewThread' # so thread 2 run ebayUSForumsGuestViewThread.py # If ID=3, then 3%len(scripts)==0( there are 3 elements in scripts, so len(scripts) is 3, so 3%3==0) # so script=scripts[0], so script='ebayUSForumsAdvancedSearch' # so thread 3 run ebayUSForumsAdvancedSearch.py self.testRunner = createTestRunner(script) # This method is called for every run. def __call__(self): self.testRunner()
Ok,到此为止,主要是给自己的看的,如果你有需要可以看,有问题可以问我.