1、關聯:通常在業務流程中有很多一系列的介面呼叫,從后面的介面依賴前邊介面的結果資料
from lxml import etree
from locust import TaskSet, task, HttpUser
class UserBehavior(TaskSet):
@staticmethod
def get_session(html):
tree = etree.HTML(html)
return tree.xpath("//div[@class='btnbox']/input[@name='session']/@value")[0] \
@task(10)
def test_login(self):
html = self.client.get('/login').text
username = '[email protected]'
password = '123456'
session = self.get_session(html)
payload = { 'username': username, 'password': password, 'session': session }
self.client.post('/login', data=https://www.cnblogs.com/codeobj/archive/2020/09/22/payload)
class WebsiteUser(HttpUser):
host ='http://debugtalk.com'
# task_set = UserBehavior
tasks = [UserBehavior ]
min_wait = 1000
max_wait = 3000
2、檢查點:用來判斷回傳值是否符合要求
from lxml import etree
from locust import TaskSet, task, HttpUser
class UserBehavior(TaskSet):
@staticmethod
def get_session(html):
tree = etree.HTML(html)
return tree.xpath("//div[@class='btnbox']/input[@name='session']/@value")[0] \
@task(10)
def test_login(self):
html = self.client.get('/login').text
assert "200" in html
username = '[email protected]'
password = '123456'
session = self.get_session(html)
payload = { 'username': username, 'password': password, 'session': session }
self.client.post('/login', data=https://www.cnblogs.com/codeobj/archive/2020/09/22/payload)
class WebsiteUser(HttpUser):
host ='http://debugtalk.com'
# task_set = UserBehavior
tasks = [UserBehavior ]
min_wait = 1000
max_wait = 3000
3、集合點:提高某個介面的并發度,當所有用戶運行到指定位置后集合等待,同時向下執行
from gevent._semaphore import Semaphore
from locust import TaskSet, events
from lxml import etree
all_locusts_spawned = Semaphore()
all_locusts_spawned.acquire()
def on_hatch_complete(**kwargs):
all_locusts_spawned.release() # 創建鉤子方法
events.hatch_complete += on_hatch_complete # 掛載到locust鉤子函式(所有的Locust實體產生完成時觸發)
class TestTask(TaskSet):
def on_start(self):
""" on_start is called when a Locust start before any task is scheduled """
all_locusts_spawned.wait() # 限制在所有用戶準備完成前處于等待狀態
self.login()
@staticmethod
def get_session(html):
tree = etree.HTML(html)
return tree.xpath("//div[@class='btnbox']/input[@name='session']/@value")[0] \
def login(self):
html = self.client.get('/login').text
username = '[email protected]'
password = '123456'
session = self.get_session(html)
payload = {'username': username, 'password': password, 'session': session}
self.client.post('/login', data=https://www.cnblogs.com/codeobj/archive/2020/09/22/payload)
個人博客 蝸牛
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/107536.html
標籤:其他
上一篇:性能測驗-Locust引數篇
下一篇:性能測驗-Locust分布式執行
