Re: ngrinder的脚本可以实现参数化功能吗?

Posted by ziteng on
URL: http://ngrinder.373.s1.nabble.com/ngrinder-tp1467p2309.html

我的做法大致如下,实现了不同的 Vuser 取不同的值


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
import datetime
import random
import string
from org.json import JSONObject
import hashlib
log=grinder.logger.info

test_query_file = "./resources/configEnv.json"
file = open(test_query_file, "r")
fileStr = str(file.read())
file.close()

jsonObject=JSONObject(fileStr)

env = jsonObject.getString("Name")
#grinder.logger.info("Current Environment:"+env)
#Get the env sub jsonobject, so that can get the sub keys
envInfo = jsonObject.getJSONObject(env)
filepath = str(envInfo.getString("wx_pay_datafile"))

test1 = Test(1, url)
request1 = HTTPRequest(url=url)
# Make any method call on request1 increase TPS
test1.record(request1)

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

#################
numGrinderProcesses = int(grinder.properties.getProperty("grinder.processes"))
numGrinderThreads = int(grinder.properties.getProperty("grinder.threads")) 
# the below means 'Interval_file_row'
numGrinderRuns = int(grinder.properties.getProperty("grinder.runs"))
#################

class TestRunner:
	# initlialize a thread 
	def __init__(self):
		grinder.statistics.delayReports=True
		self.threadNumber = int(grinder.getThreadNumber())
		#grinder.logger.info('threadNumber: '+str(self.threadNumber))
		self.processNumber = int(grinder.getProcessNumber())
		#grinder.logger.info('processNumber: '+str(self.processNumber))
		self.agentNumber = int(grinder.getAgentNumber())
		self.curNumber = (self.agentNumber*numGrinderProcesses*numGrinderThreads)+(self.processNumber*numGrinderThreads)+self.threadNumber

	# test method		
	def __call__(self):
		# Open the file with the user information.
		self.runNumber = int(grinder.getRunNumber())
		userFile = open(filepath, 'r') 
		lines = userFile.readlines()
		userFile_rowCount = len(lines)
		current_line = self.curNumber*numGrinderRuns+self.runNumber
		# Start with a value in userFileLine so we can tell if we read an empty line or an end-of-file marker. 
		userFileLine = 'X' # 'X' is just a randomly picked value; it doesn't mean anything. 
		if userFile_rowCount:
			userFileLine = lines[current_line] 
			userFileLine = userFileLine.strip() 
			if userFileLine: 
				# the number starting from 0 within a single process.
				log('agent-process-thread-run: '+str(self.agentNumber)+'-'+str(self.processNumber)+'-'+str(self.threadNumber)+'-'+str(self.runNumber)+' curNumber:'+str(self.curNumber)+' Current_line:'+str(current_line))
				ch_details=userFileLine.split(',')
				############### do something #################

		userFile.close()