标签:first client path tar except ESS color 方式 password
原文链接:https://debugtalk.com/post/head-first-locust-advanced-script/
在某些请求中,需要携带之前从Server端返回的参数,因此在构造请求时需要先从之前的Response中提取出所需的参数。
1 from lxml import etree 2 from locust import TaskSet, task, HttpLocust 3 4 class UserBehavior(TaskSet): 5 6 @staticmethod 7 def get_session(html): 8 tree = etree.HTML(html) 9 return tree.xpath("//div[@class=‘btnbox‘]/input[@name=‘session‘]/@value")[0] 10 11 @task(10) 12 def test_login(self): 13 html = self.client.get(‘/login‘).text 14 username = ‘user@compay.com‘ 15 password = ‘123456‘ 16 session = self.get_session(html) 17 payload = { 18 ‘username‘: username, 19 ‘password‘: password, 20 ‘session‘: session 21 } 22 self.client.post(‘/login‘, data=payload) 23 24 class WebsiteUser(HttpLocust): 25 host = ‘https://debugtalk.com‘ 26 task_set = UserBehavior 27 min_wait = 1000 28 max_wait = 3000
所有并发虚拟用户共享同一份测试数据,各虚拟用户在数据列表中循环取值。
例如,模拟3用户并发请求网页,总共有100个URL地址,每个虚拟用户都会依次循环加载这100个URL地址;加载示例如下表所示。
1 from locust import TaskSet, task, HttpLocust 2 3 class UserBehavior(TaskSet): 4 def on_start(self): 5 self.index = 0 6 7 @task 8 def test_visit(self): 9 url = self.locust.share_data[self.index] 10 print(‘visit url: %s‘ % url) 11 self.index = (self.index + 1) % len(self.locust.share_data) 12 self.client.get(url) 13 14 class WebsiteUser(HttpLocust): 15 host = ‘https://debugtalk.com‘ 16 task_set = UserBehavior 17 share_data = [‘url1‘, ‘url2‘, ‘url3‘, ‘url4‘, ‘url5‘] 18 min_wait = 1000 19 max_wait = 3000
所有并发虚拟用户共享同一份测试数据,并且保证虚拟用户使用的数据不重复。
例如,模拟3用户并发注册账号,总共有9个账号,要求注册账号不重复,注册完毕后结束测试;加载示例如下表所示。
1 rom locust import TaskSet, task, HttpLocust 2 import queue 3 4 class UserBehavior(TaskSet): 5 6 @task 7 def test_register(self): 8 try: 9 data = self.locust.user_data_queue.get() 10 except queue.Empty: 11 print(‘account data run out, test ended.‘) 12 exit(0) 13 14 print(‘register with user: {}, pwd: {}‘15 .format(data[‘username‘], data[‘password‘])) 16 payload = { 17 ‘username‘: data[‘username‘], 18 ‘password‘: data[‘password‘] 19 } 20 self.client.post(‘/register‘, data=payload) 21 22 class WebsiteUser(HttpLocust): 23 host = ‘https://debugtalk.com‘ 24 task_set = UserBehavior 25 26 user_data_queue = queue.Queue() 27 for index in range(100): 28 data = { 29 "username": "test%04d" % index, 30 "password": "pwd%04d" % index, 31 "email": "test%04d@debugtalk.test" % index, 32 "phone": "186%08d" % index, 33 } 34 user_data_queue.put_nowait(data) 35 36 min_wait = 1000 37 max_wait = 3000
所有并发虚拟用户共享同一份测试数据,保证并发虚拟用户使用的数据不重复,并且数据可循环重复使用。
例如,模拟3用户并发登录账号,总共有9个账号,要求并发登录账号不相同,但数据可循环使用;加载示例如下表所示。
该种场景的实现方式与上一种场景基本相同,唯一的差异在于,每次使用完数据后,需要再将数据放入队列中。
1 from locust import TaskSet, task, HttpLocust 2 import queue 3 4 class UserBehavior(TaskSet): 5 6 @task 7 def test_register(self): 8 try: 9 data = self.locust.user_data_queue.get() 10 except queue.Empty: 11 print(‘account data run out, test ended.‘) 12 exit(0) 13 14 print(‘register with user: {}, pwd: {}‘15 .format(data[‘username‘], data[‘password‘])) 16 payload = { 17 ‘username‘: data[‘username‘], 18 ‘password‘: data[‘password‘] 19 } 20 self.client.post(‘/register‘, data=payload) 21 self.locust.user_data_queue.put_nowait(data) 22 23 class WebsiteUser(HttpLocust): 24 host = ‘https://debugtalk.com‘ 25 task_set = UserBehavior 26 27 user_data_queue = queue.Queue() 28 for index in range(100): 29 data = { 30 "username": "test%04d" % index, 31 "password": "pwd%04d" % index, 32 "email": "test%04d@debugtalk.test" % index, 33 "phone": "186%08d" % index, 34 } 35 user_data_queue.put_nowait(data) 36 37 min_wait = 1000 38 max_wait = 3000
标签:first client path tar except ESS color 方式 password
原文地址:https://www.cnblogs.com/tahitimoon/p/14947706.html