10 locust 场景设计1
1 场景
- 模拟75%用户顺序调用场景1的接口,每个接口执行一次
- 模拟25%用户顺序调用场景2的接口,每个接口执行一次
2 场景1:
import os import sys sys.path.append(os.getcwd()) # 文件所在路径加入环境变量,以免找不到 from locust import task, TaskSequence, seq_task class TestMubu(TaskSequence): @seq_task(1) @task(1) def mubu_index1(self): self.client.get('/', name="t21") @seq_task(2) @task(1) def mubu_index2(self): self.client.get('/', name="t22") @seq_task(3) @task(1) def mubu_index3(self): self.client.get('/', name="t23")
3 场景2:
import os import sys sys.path.append(os.getcwd()) from locust import task, TaskSequence, seq_task class TestMubu2(TaskSequence): @seq_task(1) @task(1) def mubu_index1(self): self.client.get('/', name="t21") @seq_task(2) @task(1) def mubu_index2(self): self.client.get('/', name="t22") @seq_task(3) @task(1) def mubu_index3(self): self.client.get('/', name="t23")
4 设置每个用户场景的权重
from
locust
import
TaskSet
from
.TestMubu
import
TestMubu
from
.TestMubu2
import
TestMubu2
class
TestSuit(TaskSet):
tasks
=
{TestMubu:
3
, TestMubu2:
1
, }
5 执行脚本
from locust import HttpLocust, between from config.conf import MAX_WAIT, MIN_WAIT from testcases.TestMubu import TestMubu from testcases.TestMubu2 import TestMubu2 from testcases.Testsuit import TestSuit # 访问设置 class RunTest(HttpLocust): # The behaviour of this user is defined by the task_set attribute task_set = TestSuit wait_time = between(1, 2) host = "http://www.mubu.com"
6 执行结果