第一種 **********************************
from locust import HttpLocust, TaskSet, task
import json
class HangOutTaskSet(TaskSet):
header = {'charset': 'utf-8', 'content-type': 'application/json'}
@task
def hang_out(self):
request_url ="/v2.0/account/findByAccountId"
request_params={
"accountId":156324326702300100
}
header = {'charset': 'utf-8', 'content-type': 'application/json'}
request_result = self.client.get(url=request_url, params=(request_params), headers=header )
class PlayGameTaskSet(TaskSet):
@task
def getVasl(self):
request_url ='/v2.0/account/getAccountByAccountNo'
reqiest_params={
'accountNo':'xl0021720001563243267fbk02300110'
}
header = {'charset': 'utf-8', 'content-type': 'application/json'}
request_result = self.client.get(url=request_url, params=(reqiest_params), headers=header)
class HangOutTaskLoucst(HttpLocust):
weight = 10
host = '
http://10.1.1.36:25419'
task_set = HangOutTaskSet
min_wait = 1
max_wait = 5
class PlayGameTaskLoucst(HttpLocust):
weight = 90
host = '
http://10.1.1.36;25419'
task_set = PlayGameTaskSet
min_wait = 1
max_wait = 5
第二種*****************************************
from locust import HttpLocust, TaskSet, task
import uuid
import random
import json
def add_news(l):
# 參數進行初始化
order_no =random.randint(1000000,99999999)
order_no2 = random.randint(2000000,99999999)
post_data = {
"details": [
{
"accountId": 156506194607800103,
"accountingSummary": "充值壓力測試",
"amount": 2.26,
},
{
"accountId": 156506194607800103,
"accountingSummary": "充值壓力測試",
"amount": 2.26,
}
],
"orderNo": order_no,
"remark": "充值壓力測試",
"source": "lucust",
"thirdNo": order_no,
"tradeTime": 1564992540085
}
header = {'charset': 'utf-8', 'content-type': 'application/json'}
l.client.post("/record/entry", data= json.dumps(post_data), headers=header)
def publish_news(l):
pass
def login(l):
l.client.post("/login",{"username":"ellen_key","password":"education"})
class WebsiteTasks(TaskSet):
def on_start(self):
login(self)
tasks=[add_news, publish_news]
class WebSiteUser(HttpLocust):
host="
http://10.1.1.36:25419"
task_set = WebsiteTasks
'''每一個用戶執行兩個任務間隔時間的上下限(毫秒),若是不制定默認爲1秒'''
min_wait=1
max_wait =5
第三種**********************************************
from locust import HttpLocust, TaskSet, task
import json
class UserBehavior(TaskSet):
header = {'charset': 'utf-8', 'content-type': 'application/json'}
@task(10)
def getTagVals(self):
request_url ="/v2.0/account/findByAccountId"
request_params={
"accountId":156324326702300100
}
header = {'charset': 'utf-8', 'content-type': 'application/json'}
request_result = self.client.get(url=request_url, params=(request_params), headers=header )
@task(15)
def getVasl(self):
request_url ='/v2.0/account/getAccountByAccountNo'
reqiest_params={
'accountNo':'xl0021720001563243267fbk02300110'
}
header = {'charset': 'utf-8', 'content-type': 'application/json'}
request_result = self.client.get(url=request_url, params=(reqiest_params), headers=header)
class MobileLocust(HttpLocust):
host = '
http://10.1.1.36:25419'
task_set = UserBehavior
min_wait = 1
max_wait = 5
locust 中的 task
@task(10),10 表示locust實例被挑選執行的權重,數值越高,執行頻率越高
tasks 能夠是一個二元組列表後者dict :task ={ 任務1:10, 任務2:5,任務3:7 } 任務名:權重值
或者 task =[ (任務名 ,權重值),(任務名,權重值) ]
min_wait, max_wait 指用戶執行任務之間的等待時間下界和上界
taskset : 每一個用戶的任務集合,而後按照taskset裏面的每一個任務按照權重隨機的執行
taskset能夠進行嵌套
calss UserTask(TaskSet):
@task(2)
class stay(TaskSet):
@task(3)
def read(self):
pass
@task(8)
def sing(self):
pass
@task(1)
def leave(self):
pass
若是想從一個taskset的任務裏跳出來,須要引用self.interrupt()
on_start()函數,是每一個locust用戶進入TaskSet以後作的第一件事
執行流程:
先執行WebsiteTasks中的on_start(只執行一次),做爲初始化;
從WebsiteTasks中隨機挑選(若是定義了任務間的權重關係,那麼就是按照權重關係隨機挑選)一個任務執行;
根據Locust類中min_wait和max_wait定義的間隔時間範圍(若是TaskSet類中也定義了min_wait或者max_wait,以TaskSet中的優先),在時間範圍中隨機取一個值,休眠等待;
重複2~3步驟,直至測試任務終止。
單進程、多進程的執行
單進程: locust -H
https://debugtalk.com -f demo.py
多進程 :主: locust -H
https://debugtalk.com -f demo.py --master --port=8088
從:locust -H
https://debugtalk.com -f demo.py --slave
若是主從不在一臺機器上: locust -H
https://debugtalk.com -f demo.py --slave --master-host=<locust_machine_ip>
至關棒的一篇關於locust的文章: