Advertisement

Locust (一)性能测试为什么要用 locust ?为什么 locust 的教程用不了?

阅读量:

一、什么是locust?

Specify user behavior using Python code, and to swarm the system to handle millions of simultaneous users at the same time.

即,用Python代码定义用户行为,让数百万并发用户涌入您的系统。

二、为什么用locust?

相较于loadrunner、jmeter,locust的优势主要在于:

1、基于协程,避免了系统级资源调度,大幅提高了单机并发能力

2、开源免费,功能简单,学习成本低

使用Python进行开发,并且HTTP请求主要依赖于Requests库;还可以支持其他协议或定制请求;该方案具备较强的扩展性,并且理论上适用于任何系统

不过其缺点尤为显著的是一直被人诟病的极简风报告与分析 并且不支持资源监控

所以,locust的优缺点都很明显,如何取舍就看个人需求了。

具体对比如下:

LR Jmeter Locust
开发语言 C/JAVA JAVA PYTHON
脚本形式 C/JAVA GUI PYTHON
授权方式 商业收费 开源免费 开源免费
并发机制 进程/线程 线程 协程
单机并发能力
资源消耗
分布式 支持 支持 支持
报告与分析 全面 简单 简单
资源监控 支持 不支持 不支持
二次开发 不支持 支持 支持

三、为什么网上搜到的 locust 教程大都不能用了?

其实不可行的原因归结为技术革新带来的变化。具体而言,在现有网络环境下使用的教程多为"0.n"版本格式,在 locust 的发展过程中也经历了从"0.n"到"1.n"版本阶段(2020)。

而网上的教程大都还停留在0版本,版本不一致自然不能用。

那么0到1的版本变更都有哪些呢?

1、安装

安装命令统一为:pip install locust

如果使用 pip install locustio,就必须指定以前0开头的版本

2、参数

可以直接拉出帮助文档

C:\Users\Administrator>locust --help

Usage: locust [OPTIONS] [UserClass ...]

Common options:
-h, --help display this help message and terminate
-f LOCUSTFILE, --locustfile LOCUSTFILE
Python file for importing, such as '../other.py'.
Default: locustfile
--config CONFIG Config file path
-H HOST, --host HOST Please load the test using the following format:
http://10.21.32.33
-u NUM_USERS, --users NUM_USERS
Number of concurrent Locust users exclusively when combined with --headless
-r HATCH_RATE, --hatch-rate HATCH_RATE
The rate per second for spawning users; only used with --headless
-t RUN_TIME, --run-time RUN_TIME Stop execution after the specified duration (e.g., 300s or 20m). Used only with --headless
-l, --list Show list of possible User classes and terminate

Web UI 选项:

  • 使用 --web-host 选择 WEB_HOST 以绑定 Web 接入界面,默认为全局主机 )。
    (all interfaces)
  • 使用 --web-port 选择 WEB_PORT 或 -PWEB_PORT 指定 Web 网站运行端口。
  • 使用 --headless 关闭 Web 接入界面并立即启动负载测试。(此操作需配合使用 -u 和 -t 参数)
  • 使用 --web-auth 启用 Web 接入界面的基本认证功能。(请按照用户名:密码格式配置)
  • 使用 --tls-cert (可选)指定用于 HTTPS 通信的 TLS 证书路径(TLS_CERT)。
  • 使用 --tls-key (可选)指定用于 HTTPS 通信的 TLS 私钥路径(TLS_KEY)。

Configuration settings:
Setting up configurations for a Locust master node during distributed testing. A master node requires worker nodes to establish connections before initiating load testing.

--master Set locust to run in distributed mode with this process as master
--master-bind-host MASTER_BIND_HOST
When running this script, please note that this interface specifies the hostname and IP address that the locust master should bind to.
--master-bind-port MASTER_BIND_PORT
This port specifies the port that the locust master should bind to.
Defaults to * (all available interfaces).
--expect-workers EXPECT_WORKERS
This parameter indicates how many workers the locust master should expect to connect before starting the test (only when --headless is enabled).
Defaults to 5557.

Worker options:

Options for launching a Locust Worker node in a distributed Locust setup. Only the LOCUSTFILE option (-f flag) must be set when launching a Worker, as other flags like -u, -r, and -t are configured on the Master node.

--worker Configure locust to execute in a distributed manner using this process as a worker node.
--master-host MASTER_NODE_HOST
Indicates the host address or IP of the locust master for distributed load testing.
Only applicable when running with --worker.
Defaults value is 127.0.0.1.
--master-port MASTER_NODE_PORT
Indicates the port number to be utilized by the locust master for distributed load testing.
Only applicable when running with --worker.
Default value is 5557.

Tag configurations: Locust test tasks can be tagged with the @tag decorator. These settings allow specifying which tasks to include or exclude in a test scenario.

采用[TAG列表]作为参数
Tag列表用于包含在测试中,并确保只有与指定标签匹配的任务被执行
-E [TAG [...]], --exclude-tags [TAG [...]]
Tag列表用于排除测试中的所有匹配任务

Request statistics configuration:
The --csv option specifies a prefix for storing current request statistics in CSV format.
By enabling this setting, three CSV files will be created:
[CSV_PREFIX]_stats.csv for general statistics,
[CSV_PREFIX]_stats_history.csv for historical data, and
[CSV_PREFIX]_failures.csv for failure records.
The --csv-full-history option appends each individual statistic entry to _stats_history.csv.
The --print-stats option outputs summary statistics to the console.
The --only-summary option restricts output to only summary-level metrics.
Finally, the --reset-stats option erases all collected statistics once hatching is complete.
This configuration must be enabled on both the master node and all worker nodes when running in a distributed environment.

Logging options:

  • Use --skip-log-setup to disable Locust's logging configuration, which typically relies on the application or Python defaults.
  • Configure the log level with --loglevel, selecting from DEBUG/INFO/WARNING/ERROR/CRITICAL. The default setting is INFO.
  • Set the log file path using --logfile. If no path is specified, logs will be written to stdout and stderr.

Step load options:
--step-load Enable Step Load mode to monitor how performance
metrics varies when user load increases. Requires
--step-users and --step-time to be specified.
--step-users STEP_USERS
User count to increase by step in Step Load mode. Only
used together with --step-load
--step-time STEP_TIME
Step duration in Step Load mode, e.g. (300s, 20m, 3h,
1h30m, etc.). Only used together with --step-load

Other options:

要查看用户类任务执行比率的数据,请使用

要查看程序版本号并退出,请使用

设置进程退出码时请使用

Available user categories:
In the class, you can optionally specify which user categories should be used (the available user categories can be listed using -l or --list options).

3、代码

将 Locust 类改为 User,并更换 HttpLocust 类名为 HttpUser;同时更改 TaskSet 类的属性 locust 为 user

(2)删除 Locust 类中的 task_set 属性,并将其替代为 User 类中的 tasks 属性;同时严格限定 tasks 仅允许作为列表或字典形式存在。

(3)可以在运行过程中通过 -T --tags 参数增添所需标签,并通过 -E --exclude-tags 参数排除不需要的标签。

(4)可以在继承User类下,直接使用@task装饰器

(5)环境变量

· LOCUST_MASTER 重命名为 LOCUST_MODE_MASTER

· LOCUST_SLAVE 重命名为 LOCUST_MODE_WORKER

· LOCUST_MASTER_PORT 重命名为 LOCUST_MASTER_NODE_PORT

· LOCSUT_MASTER_HOST 重命名为 LOCUST_MASTER_NODE_HOST

· CSVFILEBASE 重命名为 LOCUST_CSV

在分布式环境中,在辅助进程中设置各项参数时,默认情况下采用了 '--worker' 选项和 '--expect-workers' 这两个配置项。

去除这两个方法后,请您采用on_test_start和on_test_stop来替代原来的Setup和TearDown配置。

(8) 高效类 TaskSequence 和 顺序装饰器@seq_task 进行整合后升级为 SequentialTaskSet

(9) 无图形模式启动参数,由 --no-web 更改为 --headless

(10)数据记录,csv文件,表头发生变化,增加统计,使表头意思更清晰明了

(11)结果写入csv文件的参数 --csv-base-name 被移除

控制台的日志记录不需要通过标准输出端口stdout(或标准错误端口stderr)来进行。为了实现日志记录的目的,可以通过调用print()函数来进行操作;此外还可以利用logging模块并配置相应的日志记录功能来完成。

全部评论 (0)

还没有任何评论哟~