Locust(python)性能测试库的安装与使用(学习总结1)

1、Locust是什么?

Locust是一个易于使用的分布式用户负载测试工具。它用于负载测试web站点(或其他系统),并计算出一个系统可以处理多少并发用户。
这个想法是,在测试期间,一群Locust会攻击你的网站。每个Locust(或者测试用户)的行为由您定义,集群过程由web UI实时监控。这将帮助您在让真正的用户进入之前进行测试并识别代码中的瓶颈。
Locust完全基于事件,因此可以在一台机器上支持数千个并发用户。与许多其他基于事件的应用程序相比,它不使用回调。相反,它使用轻量级进程,通过gevent。每个Locust都在自己的进程中运行(正确的说法是greenlet)。这允许您用Python编写非常有表现力的场景,而不用回调使代码复杂化。

2、Locust的安装

Win:

(1)for Python 2.7:

$ python -m pip install locustio

(2)for Python 3:

$ python -m pip install locustio     or python3  ...

(3)也可使用pip直接从git存储库安装。例如:

$ python3 -m pip install -e git://github.com/locustio/locust.git@master#egg=locustio

安装完成使用locust的shell命令检查是否安装成功。下图为安装成功。

locust --help

在这里插入图片描述

(4)Windows直接运行 pip install locustio 即可,如果不行,可以通过首先安装为pyzmq、gevent和greenlet预先构建的二进制包来修复它。非官方的windows预构建python包集合:http://www.lfd.uci.edu/~gohlke/pythonlibs/
下载好的 .whl 文件安装。

$ pip install name-of-file.whl

macOS:

1、安装 Homebrew
2、安装libev (gevent依赖):

brew install libev

3、在按照上面的说明做。

3、快速使用

(1)声明任务方式一

from locust import HttpLocust, TaskSet  
# HttpLocust 发送http请求,相当于requests的发送请求,有get和post方法。
# TaskSet  定义用户的行为,可以是单个登录接口,也可以是多个接口组成的场景

def login(l): # 登录方法
    l.client.post("/login", {"username":"ellen_key", "password":"education"})

def logout(l): # 登出方法
    l.client.post("/logout", {"username":"ellen_key", "password":"education"})

def index(l): # 某页面
    l.client.get("/")

def profile(l): # 某页面
    l.client.get("/profile")

class UserBehavior(TaskSet):
# 自定义类(用户行为)
    tasks = {index: 2, profile: 1}    # tasks相当于任务集,从中随机抽取执行(声明任务)

    def on_start(self):  # 在此类中最先执行
        login(self)

    def on_stop(self): # 在此类最后执行
        logout(self)

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 5000
    max_wait = 9000
    
   # min_wait 和max_wait 是任务之间的最小等待时间和最大等待时间。

(2)声明任务方式二

from locust import HttpLocust, TaskSet, task
# @task 声明任务的另一种方法。
class UserBehavior(TaskSet):
    def on_start(self):
        """ on_start is called when a Locust start before any task is scheduled """
        self.login()

    def on_stop(self):
        """ on_stop is called when the TaskSet is stopping """
        self.logout()

    def login(self):
        self.client.post("/login", {"username":"ellen_key", "password":"education"})

    def logout(self):
        self.client.post("/logout", {"username":"ellen_key", "password":"education"})

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

    @task(1)  # @task为另一种声明任务的方式(其中参数代表任务的执行的比例,参数越大比例越高)
    def profile(self):
        self.client.get("/profile")

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 5000
    max_wait = 9000

4、启动

(1)如果文件位于locust的不同子目录或名称下,使用 -f:

$ locust -f locust_files/my_locust_file.py --host=http://example.com

(2)运行分布在多个进程中的locust,通过指定 --master:

$ locust -f locust_files/my_locust_file.py --master --host=http://example.com

然后会启动任意数量的从进程:

$ locust -f locust_files/my_locust_file.py --slave --host=http://example.com

(3)在多台机器运行Locust,必须在启动从机是指定主主机(在一台机器运行不需要指定,因为主机默认为127.0.0.1)

$ locust -f locust_files/my_locust_file.py --slave --master-host=192.168.0.100 --host=http://example.com

启动成功(此图在pycharm的Terminal启动),打开浏览器并将其指向http://127.0.0.1:8089http://localhost:8089(如果您在本地运行Locust,默认端口8089)
在这里插入图片描述
在这里插入图片描述

未完待续。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老糊涂Lion

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值