首页 > 技术文章 > Locust-快速入门

zhangboyi 2022-03-21 23:16 原文

安装

前提:需要安装python3.6以上的版本

  • pip安装 locust
- 安装locust
pip3 install locust
- 验证是否安装成功
locust -V
- 升级locust
pip3 install -U --pre locust
  • mac安装
brew install locust

Locust 测试本质上是一个 Python 程序,所以它非常地灵活以及特别擅长实现复杂的用户流。另外它也可以做简单的测试,所以让我们从这个开始:

from locust import HttpUser, task

class HelloWorldUser(HttpUser):
    @task
    def hello_world(self):
        self.client.get("/hello")
        self.client.get("/world")

该用户将一次又一次地向"/hello" 和 "/world "发出 HTTP 请求。有关完整的解释和更现实的示例,请参阅编写 locustfile。

Locust的Web页面

启动 Locust 后,打开浏览器并将其指向 http://localhost:8089。会展示以下页面:

命令行

除了可以使用Locust Web UI,还可以在命令行上提供负载参数并以文本形式获取结果报告:

$ locust --headless --users 10 --spawn-rate 1 -H http://your-server.com
[2021-07-24 10:41:10,947] .../INFO/locust.main: No run time limit set, use CTRL+C to interrupt.
[2021-07-24 10:41:10,947] .../INFO/locust.main: Starting Locust 2.5.1.dev22
[2021-07-24 10:41:10,949] .../INFO/locust.runners: Ramping to 10 users using a 1.00 spawn rate
Name              # reqs      # fails  |     Avg     Min     Max  Median  |   req/s failures/s
----------------------------------------------------------------------------------------------
GET /hello             1     0(0.00%)  |     115     115     115     115  |    0.00    0.00
GET /world             1     0(0.00%)  |     119     119     119     119  |    0.00    0.00
----------------------------------------------------------------------------------------------
Aggregated             2     0(0.00%)  |     117     115     119     117  |    0.00    0.00

[2021-07-24 10:44:42,484] .../INFO/locust.runners: All users spawned: {"HelloWorldUser"10} (10 total users)

推荐阅读