locust框架模板

from locust import HttpLocust, TaskSet, task
import Queue class UserBehavior(TaskSet): def setup(self):
print('task setup') def teardown(self):
print('task teardown') def on_start(self): # key = self.login()
request_url = "/api/user/getkey"
request_json = {"username": "qxzn", "userpwd": "qx6688"} response = self.client.post(url=request_url, json=request_json)
# print response.status_code
# print response.text
if response.status_code != 200:
print u"返回异常,请求返回状态码:", response.status_code
elif response.status_code == 200:
print u"返回正常:", response.status_code
global key_1, key_2
key_1 = response.text.split('''appkey":"''')[1].split('"')[0]
key_2 = response.text.split('''infokey":"''')[1].split('"')[0]
print key_1 + '-' + key_2
data = self.locust.user_data_queu
print data def on_stop(self):
# 虚拟用户结束Task时运行
print('end') @task(1)
def resList(self): # key = self.login()
header = {'Accept': 'application/json, text/plain, */*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0',
'Content-Type': 'application/json;charset=utf-8',
'Authorization': key_1 + '-' + key_2}
data = {"num": 15, "current_page": 1, "province": "", "city": "", "type": ""}
request_url = "/api/desk_device/devicelist" response = self.client.post(url=request_url, headers=header, json=data)
if response.status_code != 200:
print u"返回异常,请求返回状态码:", response.status_code
elif response.status_code == 200:
print u"返回正常", response.status_code class WebsiteUser(HttpLocust): def setup(self):
print('locust setup') def teardown(self):
print('locust teardown') user_data_queue = Queue.Queue()
task_set = UserBehavior
host = "http://xxxx.com:8095"
min_wait = 5000
max_wait = 8000
05-07 15:25