Locust 压力测试工具学习(一)

本文介绍如何使用Python的Locust库进行压力测试,包括安装配置、编写测试代码、启动程序、分布式部署以及查看测试结果。适用于需要进行手机办公系统压力测试的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近公司上了一套手机办公系统,上线用户必须要出一份压力测试的报告。传统情况下一般都会用 LoadRunner 来处理,但考虑自己正在学习 Python 运维;所以打算使用基于 Python 的产品来处理。

根据自己在网上搜索的情况,最终选定了 Locust 来进行压力测试工作。

官方文档

模块安装

安装 locustio 模块包

[root@centos7 ~]# pip install locustio

安装过程中遇到如下提醒信息:DEPRECATION: Uninstalling a distutils installed project (requests) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.

安装 ZeroMQ 模块包

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

[root@centos7 ~]# pip install pyzmq 

模块调试

测试代码

Main.py 文件

from locust import HttpLocust, TaskSet, task

class UserBehavior(TaskSet):
    def login(self):
        response = self.client.post("/portal/Login/enter.jsp", {"j_username": "admin", "j_password": "power11"})
        print response.status_code

    @task(1)
    def index(self):
        self.client.get("/portal/stPortal/default.jsp")

    @task(2)
    def profile(self):
        self.client.get("/portal/stPortal/default.jsp?DashboardID=0003&DashboardType=0")

    def on_start(self):
        self.login()

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

启动程序

[root@centos7 ~]# locust -f Main.py --host=http://10.45.1.14

or

[root@centos7 ~]# locust -f Main.py --host=http://10.45.1.14 --no-web -c 3000 -r 20 --print-stats

测试结果及说明

这里写图片描述

这里写图片描述

分布式部署

分别在准备用于压测的多台服务器上面(建议测试时使用 windows 环境,正式工作时使用 linux 环境)安装 locust 模块;且将测试脚本上传至所有服务器上面

master

[root@centos7 LoadTesting]# locust -f LoadTestingApp.py --master
[2017-03-02 12:18:36,210] centos7/INFO/locust.main: Starting web monitor at *:8089
[2017-03-02 12:18:36,211] centos7/INFO/locust.main: Starting Locust 0.8a2
[2017-03-02 12:18:36,391] centos7/INFO/locust.runners: Client u'salt.node2_a15146db68a3ed9fe11096957f995f75' reported as ready. Currently 1 clients ready to swarm.
[2017-03-02 12:19:09,554] centos7/INFO/locust.runners: Client u'centos7_6df7ba727ddcbc3813682d4033778e52' reported as ready. Currently 2 clients ready to swarm.

slave

如果采用一机多进程模式,则不需要指定 master 地址

[root@centos7 LoadTesting]# locust -f LoadTestingApp.py --slave
[2017-03-02 12:19:09,550] centos7/INFO/locust.main: Starting Locust 0.8a2

如果采用多机分布式模式,则需要通过 --master-ip 参数指定 master 地址

[root@salt LoadTesting]# locust -f LoadTestingApp.py --slave --master-host=192.168.137.101
[2017-03-02 12:18:10,180] salt.node2/INFO/locust.main: Starting Locust 0.8a2

界面效果

这里写图片描述

推荐学习链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值