引言
当我们接到压测任务的时候,如果被测系统需要登录的时候,首先需要处理登录,然后再进行其他接口压测。
Taks属性使用
首先看一下官方文档:
Using the @task decorator to declare tasks is a convenience, and usually the best way to do it. However, it’s also possible to define the tasks of a User or TaskSet by setting the tasks
attribute (using the @task decorator will actually just populate the tasks attribute).
The tasks attribute is either a list of Task, or a <Task : int> dict, where Task is either a python callable or a TaskSet class (more on that below). If the task is a normal python function they receive a single argument which is the User instance that is executing the task.
Here is an example of a User task declared as a normal python function:
from locust import User, constant def my_task(l): pass class MyUser(User): tasks = [my_task] wait_time = constant(1)
If the tasks attribute is specified as a list, each time a task is to be performed, it will be randomly chosen from the tasks attribute. If however, tasks is a dict - with callables as keys and ints as values - the task that is to be executed will be chosen at random but with the int as ratio. So with a tasks that looks like this:
{my_task: 3, another_task: 1}
my_task would be 3 times more likely to be executed than another_task.
Internally the above dict will actually be expanded into a list (and the tasks
attribute is updated) that looks like this:
[my_task, my_task, my_task, another_task]
and then Python’s random.choice()
is used pick tasks from the list.
上面总结出来就是:如果随机权重,就用列表,如果指定权重,就用字段。
登录案例
普通写法:
from locust import TaskSet,HttpLocust,between def login(x): x.client.post('/login',{'username':'admin','password':'123456'}) def logout(x): x.client.post('/logout',{'username':'admin','password':'123456'}) def home(x): x.client.get('/') def systeminfo(x): x.client.get('systeminfo') class TestLogin(TaskSet): tasks = {home: 3,systeminfo: 1} def on_start(self): login(self) def on_stop(self): logout(self) class WebSiteUser(HttpLocust): task_set = TestLogin wait_time = between(3,6)
案例
from locust import TaskSet,HttpLocust,between,task def login(x): url = '/api/private/v1/login' body = { "username": "admin", "password": "123456" } r = x.client.post(url=url, data=body) print(r.text) def logout(x): print("退出系统") def home(x): print("进入主页") def systeminfo(x): print("进入系统管理") class TestLogin(TaskSet): tasks = {home: 1,systeminfo: 1} # @task def setup(self): print("开始...") def teardown(self): print("结束...") def on_start(self): print("启动") login(self) # @task def on_stop(self): print("停止") logout(self) class WebSiteUser(HttpLocust): def setup(self): print('locust setup') def teardown(self): print('locust teardown') task_set = TestLogin wait_time = between(1,2) if __name__ == '__main__': import os os.system('locust -f locustfile.py --host=http://192.168.1.105:8899 --port=8089 ')
说明:
Locust类有setup和teardown,TaskSet类有setup、teardown、on_start、on_stop。
每次启动locust时运行setup方法,退出时运行teardown方法,locust执行TaskSet时运行TaskSet的setup方法,退出时运行teardown方法,每个虚拟用户执行操作时运行on_start方法,退出时执行on_stop方法,运行上面的脚本,执行顺序如下:
执行顺序:Locust setup → TaskSet setup → TaskSet on_start → TaskSet tasks → TaskSet on_stop → TaskSet teardown → Locust teardown
运行结果:
[2020-06-22 21:30:10,993] WIN10-804191526/INFO/locust.main: Starting web monitor at http://*:8089 [2020-06-22 21:30:10,993] WIN10-804191526/INFO/locust.main: Starting Locust 0.14.6 [2020-06-22 21:30:20,212] WIN10-804191526/INFO/locust.runners: Hatching and swarming 2 users at the rate 1 users/s (0 users already running)... [2020-06-22 21:30:20,213] WIN10-804191526/INFO/stdout: locust setup [2020-06-22 21:30:20,213] WIN10-804191526/INFO/stdout: [2020-06-22 21:30:20,213] WIN10-804191526/INFO/stdout: 开始... [2020-06-22 21:30:20,213] WIN10-804191526/INFO/stdout: [2020-06-22 21:30:20,213] WIN10-804191526/INFO/stdout: 启动 [2020-06-22 21:30:20,213] WIN10-804191526/INFO/stdout: [2020-06-22 21:30:21,213] WIN10-804191526/INFO/locust.runners: All locusts hatched: WebSiteUser: 2 (0 already running) [2020-06-22 21:30:21,213] WIN10-804191526/INFO/stdout: 启动 [2020-06-22 21:30:21,213] WIN10-804191526/INFO/stdout: [2020-06-22 21:30:41,220] WIN10-804191526/INFO/stdout: [2020-06-22 21:30:41,220] WIN10-804191526/INFO/stdout: [2020-06-22 21:30:41,220] WIN10-804191526/INFO/stdout: 进入主页 [2020-06-22 21:30:41,220] WIN10-804191526/INFO/stdout: [2020-06-22 21:30:42,217] WIN10-804191526/INFO/stdout: [2020-06-22 21:30:42,217] WIN10-804191526/INFO/stdout: [2020-06-22 21:30:42,217] WIN10-804191526/INFO/stdout: 进入系统管理
界面: