3.3.4.3 Locust中的参数化
假如我们要定义一个惟一取值的参数化变量,首先我们定义一个全局变量序列,按1进行递加更新字典,一旦被使用就改变其use关键字中的标识。
from locust import HttpLocust, TaskSet, task
only = [{"username" : "[email protected]","use" : 0},\
{"username" : "[email protected]","use" : 0},\
{"username" : "[email protected]","use" : 0},\
{"username" : "[email protected]","use" : 0},\
{"username" : "[email protected]","use" : 0},\
{"username" : "[email protected]","use" : 0},\
{"username" : "[email protected]","use" : 0},\
{"username" : "[email protected]","use" : 0},\
{"username" : "[email protected]","use" : 0},\
{"username" : "[email protected]","use" : 0},\
{"username" : "[email protected]","use" : 0},\
{"username" : "[email protected]","use" : 0},\
{"username" : "[email protected]","use" : 0},\
{"username" : "[email protected]","use" : 0}]
def read_only_one():
len_only = len(only)
i = 0
while 1:
if len_only == i:
return None
# 可以return[i][‘username’],即返回最后一个值
# 如想循环使用改变这里的逻辑即可
if only[i]['use'] == 0:
getName = only[i]['username']
# update
only[i]['use'] = 1
return getName
i += 1
def md5(str):
import hashlib
m = hashlib.md5()
m.update(str)
return m.hexdigest()
class UserBehavior(TaskSet):
def on_start(self):
""" on_start is called when a locust start before any task is scheduled"""
# self.loginTag = self.login()
pass
@task(1)
def login(self):
""" post login ,always the first step. """
username = read_only_one()
postLoginBody = {"clientId":"xxx",\
"consideringTheReusability":"true",
"password":md5('q123456'),\
"grantType":"password",\
"service":"authorize",\
"version":"1.0.0.150109-PRD",\
"clientSecret":"xA99D24A874B",\
"username":username}
print 'username=%s', (username)
# 可以加上if username:
# with....
# 即实现使用到最后一个可以使用的值后就停止继续进行请求
with self.client.post('/oauth?service=authorize&version=', postLoginBody, catch_response= True) as response:
if response.status_code == 200:
rc = response.json()
if response.json()['errorCode'] != '':
response.success()
# return rc
else:
response.failure('response value is not right')
print 'request = %s, request body = %s, response =%s', ('/oauth?service=authorize&version=', postLoginBody, rc)
else:
response.failure('return is not 400')
class ApiFosUser(HttpLocust):
task_set = UserBehavior
host = 'http://10.16.40.11:59002'
min_wait = 0
max_wait = 0
根据日志信息可以看到其使用到[email protected]时,后面继续用的是None了,即实现了取惟一值的情况。
[2015-08-04 17:26:23,166] kali/INFO/stdout: ('/oauth?service=authorize&version=', {'username': '[email protected]', 'clientSecret': 'x05E3182A99D24A874B', 'consideringTheReusability': 'true', 'grantType': 'password', 'version': '1.0.0.150109-PRD', 'service': 'authorize', 'password': 'c51cd8e64b0aeb778364765013df9ebe', 'clientId': 'foscloud'}, {u'errorCode': u'030060', u'failureDetails': u'username not exists'})
[2015-08-04 17:26:23,167] kali/INFO/stdout:
[2015-08-04 17:26:23,167] kali/INFO/stdout: username=%s
[2015-08-04 17:26:23,167] kali/INFO/stdout:
[2015-08-04 17:26:23,167] kali/INFO/stdout: [email protected]
[2015-08-04 17:26:23,167] kali/INFO/stdout:
[2015-08-04 17:26:23,182] kali/INFO/stdout: request = %s, request body = %s, response =%s
[2015-08-04 17:26:23,182] kali/INFO/stdout:
[2015-08-04 17:26:23,183] kali/INFO/stdout: ('/oauth?service=authorize&version=', {'username': '[email protected]', 'clientSecret': 'x5E3182A99D24A874B', 'consideringTheReusability': 'true', 'grantType': 'password', 'version': '1.0.0.150109-PRD', 'service': 'authorize', 'password': 'c51cd8e64b0aeb778364765013df9ebe', 'clientId': 'foscloud'}, {u'errorCode': u'030060', u'failureDetails': u'username not exists'})
[2015-08-04 17:26:23,183] kali/INFO/stdout:
[2015-08-04 17:26:23,183] kali/INFO/stdout: username=%s
[2015-08-04 17:26:23,183] kali/INFO/stdout:
[2015-08-04 17:26:23,183] kali/INFO/stdout: None
[2015-08-04 17:26:23,183] kali/INFO/stdout:
[2015-08-04 17:26:23,191] kali/INFO/stdout: request = %s, request body = %s, response =%s
[2015-08-04 17:26:23,191] kali/INFO/stdout:
[2015-08-04 17:26:23,191] kali/INFO/stdout: ('/oauth?service=authorize&version=', {'username': None, 'clientSecret': 'x5E3182A99D24A874B',
3.3.4.4 Locust中的事务
在request 请求中加name参数的值即可。
3.3.4.5多个类的比例分布
class ForumPage(TaskSet): @task(20) def read_thread(self): pass @task(1) def new_thread(self): pass @task(5) def stop(self): self.interrupt()class UserBehaviour(TaskSet): tasks = {ForumPage:10} @task def index(self): pass
灵活的类分布比例、以及方法分布比例,使得脚本模拟更加倾于实现模拟用户的真实操作方式。