You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Locust Web UI为多主机设置不同并发用户数

在Locust Web UI中为多用户类配置fixed_count参数

问题场景

我在用Locust做负载测试,需要同时测试多个主机,现有代码如下:

from locust import HttpUser, task, between
from tasks import TaskList

class FirstUser(HttpUser):
    wait_time = between(5, 15)
    host = "https://example.com"
    tasks = TaskList

class SecondUser(HttpUser):
    wait_time = between(5, 15)
    host = "https://anotherexample.com"
    tasks = TaskList

想通过Web UI给每个用户类设置fixed_count(固定用户数),知道Locust支持用extra_options扩展Web UI表单,但没找到具体配置方法,该怎么弄?

实现方案

1. 给Web UI添加自定义输入字段

通过web_ui事件注册额外表单字段,让两个用户类的fixed_count参数显示在测试启动页面:

from locust import events

@events.web_ui.add_listener
def add_fixed_count_fields(web_ui):
    web_ui.extra_options = [
        {
            "name": "first_user_fixed_count",
            "type": "number",
            "label": "First User 固定数量",
            "default": 0,
            "help": "FirstUser类的固定用户实例数(对应example.com)"
        },
        {
            "name": "second_user_fixed_count",
            "type": "number",
            "label": "Second User 固定数量",
            "default": 0,
            "help": "SecondUser类的固定用户实例数(对应anotherexample.com)"
        }
    ]

2. 同步命令行参数(可选但推荐)

为了让参数同时支持命令行启动和Web UI启动,添加对应命令行参数:

@events.init_command_line_parser.add_listener
def add_cli_options(parser):
    parser.add_argument("--first-user-fixed-count", type=int, default=0, help="Fixed count for FirstUser")
    parser.add_argument("--second-user-fixed-count", type=int, default=0, help="Fixed count for SecondUser")

3. 让用户类读取配置值

修改每个用户类,用类方法fixed_count来获取Web UI或命令行传入的参数:

class FirstUser(HttpUser):
    wait_time = between(5, 15)
    host = "https://example.com"
    tasks = TaskList

    @classmethod
    def fixed_count(cls):
        return cls.environment.runner.config.first_user_fixed_count

class SecondUser(HttpUser):
    wait_time = between(5, 15)
    host = "https://anotherexample.com"
    tasks = TaskList

    @classmethod
    def fixed_count(cls):
        return cls.environment.runner.config.second_user_fixed_count

4. 完整可运行代码

把所有部分整合起来:

from locust import HttpUser, task, between, events
from tasks import TaskList

# 添加Web UI自定义字段
@events.web_ui.add_listener
def add_fixed_count_fields(web_ui):
    web_ui.extra_options = [
        {
            "name": "first_user_fixed_count",
            "type": "number",
            "label": "First User 固定数量",
            "default": 0,
            "help": "FirstUser类的固定用户实例数(对应example.com)"
        },
        {
            "name": "second_user_fixed_count",
            "type": "number",
            "label": "Second User 固定数量",
            "default": 0,
            "help": "SecondUser类的固定用户实例数(对应anotherexample.com)"
        }
    ]

# 添加命令行参数支持
@events.init_command_line_parser.add_listener
def add_cli_options(parser):
    parser.add_argument("--first-user-fixed-count", type=int, default=0, help="Fixed count for FirstUser")
    parser.add_argument("--second-user-fixed-count", type=int, default=0, help="Fixed count for SecondUser")

class FirstUser(HttpUser):
    wait_time = between(5, 15)
    host = "https://example.com"
    tasks = TaskList

    @classmethod
    def fixed_count(cls):
        return cls.environment.runner.config.first_user_fixed_count

class SecondUser(HttpUser):
    wait_time = between(5, 15)
    host = "https://anotherexample.com"
    tasks = TaskList

    @classmethod
    def fixed_count(cls):
        return cls.environment.runner.config.second_user_fixed_count

使用方法

启动Locust后,在Web UI的测试启动页面能看到新增的两个数字输入框,输入需要的固定用户数后启动测试即可。如果用命令行启动,直接加--first-user-fixed-count 10 --second-user-fixed-count 20这类参数就行。

内容的提问来源于stack exchange,提问作者rubrics44

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 16:05:40