最近公司上了一套手机办公系统,上线用户必须要出一份压力测试的报告。传统情况下一般都会用 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