locust 接口压测

发布时间 2023-09-19 16:59:07作者: lytcreate

1.安装Locust:在终端中执行以下命令即可安装Locust:pip install locust

2.编写Locust脚本:创建一个名为locustfile.py的文件,并编写以下代码:

from locust import HttpUser, task, between


class MyUser(HttpUser):
    wait_time = between(5, 15)

    @task
    def my_task(self):
        self.client.get("/")

这个脚本定义了一个名为MyUser的类,它继承自HttpUser类。wait_time属性指定了用户在执行任务之间等待的时间范围。my_task方法定义了一个名为/的任务,它将调用客户端的get方法来访问网站。

Python环境内执行 locust -f locustfile.py --web-host=127.0.0.1
执行日志
DESKTOP-P63RGA1/INFO/locust.main: Starting web interface at http://127.0.0.1:8089
访问上述地址即可进行压测配置