性能测试进阶:(一)性能测试工具Locust

An open source load testing tool.

一个开源性能测试工具。

define user behaviour with python code, and swarm your system with millions of simultaneous users.

使用Python代码来定义用户行为。用它可以模拟百万计的并发用户访问你的系统。

为何突然关注性能测试工具?其实,我只是单纯对Locust工具本身感兴趣而已。

1、它与目前主流的LoadRunner和Jmeter玩法都不一样。2、它完全基于Python开发,用Python来编写用户行为。

嗯,如果想用好它的话,你必须对Web开发有一定的认识。而且还要熟悉Python开发。

官方网站:http://locust.io/

一、Locust安装                                                         

1、安装Python:

官方:https://www.python.org/

安装Python3

2、安装Locust

2.1 通过pip命令安装 /> pip install locustio

2.2  通过GitHub上克隆项目安装(Python3推荐):https://github.com/locustio/locust

3、安装 pyzmq

If you intend to run Locust distributed across multiple processes/machines, we recommend you to also install pyzmq.

如果你打算运行Locust 分布在多个进程/机器,建议你也安装pyzmq。

通过pip命令安装: pip install pyzmq

4、安装成功,CMD敲入命令验证:locust --help

二、编写简单的性能测试脚本                                         

创建load_test.py文件,通过Python编写性能测试脚本。

复制代码
from locust import HttpLocust, TaskSet, task

class UserBehavior(TaskSet):

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



class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 3000
    max_wait = 6000
复制代码

创建UserBehavior()类继承TaskSet类,为用户行为。

创建baidu() 方法表示一个行为,访问百度首页。用@task() 装饰该方法为一个任务。1表示一个Locust实例被挑选执行的权重,数值越大,执行频率越高。在当前UserBehavior()行为下只有一个baidu()任务,所以,这里的权重设置为几,并无影响。

WebsiteUser()类用于设置性能测试。

task_set :指向一个定义了的用户行为类。

min_wait :用户执行任务之间等待时间的下界,单位:毫秒。

max_wait :用户执行任务之间等待时间的上界,单位:毫秒。

三、运行性能测试                                                            

切换到性能测试脚本所在的目录,启动性能测试:

 -------------------------------------------------------------------------------------------------------------------

locust -f load_test.py --host=https://www.baidu.com

[2020-03-15 22:38:16,967] fnngj-PC/INFO/locust.main: Starting web monitor at *:8089

[2020-03-15 22:38:16,967] fnngj-PC/INFO/locust.main: Starting Locust 0.7.5

 --------------------------------------------------------------------------------------------------------------------

load_test.py 为测试脚本,https://www.baidu.com 为测试的网站。

打开浏览器访问:http://127.0.0.1:8089

Number of users to simulate 设置模拟用户数

Hatch rate (users spawned/second)  孵化率?不知道怎么翻译,每秒产生(启动)的用户数。

点击Start swarming 开始运行性能测试。

如果引起了你的兴趣,剩下的你自个玩吧!难点在性能测试脚本的编写上。

参考文档:http://docs.locust.io/en/latest/quickstart.html

----------------------------------------------------------------------------------------------------

四、Locust 系列教程

Locust 介绍

Locust 安装

Locust 创建性能测试

Locust no-web模式

Locust 参数说明

Locust 分布式运行

Locust 类和方法

Locust 设置断言

Locust 参数化

猜你喜欢

转载自www.cnblogs.com/Kevin6317/p/12501368.html