1戚宦、關(guān)聯(lián):通常在業(yè)務(wù)流程中有很多一系列的接口調(diào)用,從后面的接口依賴前邊接口的結(jié)果數(shù)據(jù)
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("http://div[@class='btnbox']/input[@name='session']/@value")[0] \
@task(10)
def test_login(self):
html = self.client.get('/login').text
username = 'user@compay.com'
password = '123456'
session = self.get_session(html)
payload = { 'username': username, 'password': password, 'session': session }
self.client.post('/login', data=payload)
class WebsiteUser(HttpUser):
host = 'http://debugtalk.com'
# task_set = UserBehavior
tasks = [UserBehavior ]
min_wait = 1000
max_wait = 3000
2锈嫩、檢查點(diǎn):用來(lái)判斷返回值是否符合要求
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("http://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 = 'user@compay.com'
password = '123456'
session = self.get_session(html)
payload = { 'username': username, 'password': password, 'session': session }
self.client.post('/login', data=payload)
class WebsiteUser(HttpUser):
host = 'http://debugtalk.com'
# task_set = UserBehavior
tasks = [UserBehavior ]
min_wait = 1000
max_wait = 3000
3受楼、集合點(diǎn):提高某個(gè)接口的并發(fā)度,當(dāng)所有用戶運(yùn)行到指定位置后集合等待呼寸,同時(shí)向下執(zhí)行
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() # 創(chuàng)建鉤子方法
events.hatch_complete += on_hatch_complete # 掛載到locust鉤子函數(shù)(所有的Locust實(shí)例產(chǎn)生完成時(shí)觸發(fā))
class TestTask(TaskSet):
def on_start(self):
""" on_start is called when a Locust start before any task is scheduled """
all_locusts_spawned.wait() # 限制在所有用戶準(zhǔn)備完成前處于等待狀態(tài)
self.login()
@staticmethod
def get_session(html):
tree = etree.HTML(html)
return tree.xpath("http://div[@class='btnbox']/input[@name='session']/@value")[0] \
def login(self):
html = self.client.get('/login').text
username = 'user@compay.com'
password = '123456'
session = self.get_session(html)
payload = {'username': username, 'password': password, 'session': session}
self.client.post('/login', data=payload)
個(gè)人博客 蝸牛