第一种 **********************************
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的文章:
https://debugtalk.com/post/head-first-locust-user-guide/