# 保存为locustfile.py # coding=utf-8 from locust import HttpLocust, TaskSet, task ''' 实现场景:先登录(只登录一次),然后访问->我的地盘页->产品页->项目页 访问我的地盘页面权重为2,产品页和项目页权重各为1 ''' # token=None class UserBehavior(TaskSet): # token=None '''蝗虫行为类''' def _login(self): '''登录方法''' # host = 'https://trybindo.com' # 禅道的服务器地 loginUrl ="/api/v2/login" head = { 'Accept': 'application/json, text/plain, */*', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Connection': 'keep-alive', 'Content-Length': '139', 'Content-Type': 'application/json', 'Host': 'trybindo.com', 'Origin': 'https://dashboard.trybindo.com', 'Referer': 'https://dashboard.trybindo.com/login', 'User-Agent': 'Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1', 'X-Application': 'dashboard.trybindo.com' } body = { "client_id":"1clvjqb9fmv5bkjoq2akbc1h4", "client_secret":"1tfcglxmnjv4t263dji05wmjr", "password": "w111111", "username":"paul.wang@bindo.com" } r = self.client.post(loginUrl, json=body, headers=head) print(r.text) assert "access_token" in r.text # global token self.token=r.json()['user']['access_token'] def on_start(self): '''任务开始准备工作:只登录一次''' self._login() # # 任务1-我的地盘 # @task(2) # def zentao_my(self): # print("---访问页面-我的地盘---") # r = self.client.get("/zentao/my/") # assert "我的地盘" in r.text # 任务2-产品页 @task(1) def getxxx(self): print("---访问页面-customers---") headx={} headx['X-USER-ACCESS-TOKEN']=self.token r = self.client.get(url="/api/v2/stores/6889/customers?order_asc=desc&order_by=updated_at&page=1&per_page=25",headers=headx) print(r.text) assert r.status_code == 200 # # 任务3-项目 # @task(1) # def zentao_prject(self): # print("---访问页面-项目---") # r = self.client.get("/zentao/project/") # assert "项目首页" in r.text class WebsiteUser(HttpLocust): task_set = UserBehavior min_wait = 1000 max_wait = 2000 if __name__ == "__main__": import os os.system("locust -f locustfile.py --host=https://trybindo.com")