阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

性能测试框架 locust 入门教程

132次阅读
没有评论

共计 2019 个字符,预计需要花费 6 分钟才能阅读完成。

1、安装
这个工具是用 Python 写的,首先我们要安装框架

首先升级 pip,不然可能会报错
pip install –upgrade pip

然后安装 Locust
pip install Locust

安装完成

2、编写 Locust file

这里是压测软件的主要功能

vim locustfile.py

from locust import HttpLocust, TaskSet

# 登陆操作
def login(l):
    l.client.post(“/phpadmin/index.php”, {“pma_username”:”hugw”, “pma_password”:”RedHat”})

# 登出操作
def logout(l):
    l.client.post(“/phpadmin/logout.php”, {“db”:”&”, “token”:”be2cb767a5829a398ef5a4c0dcafe504″})

# 请求 index
def index(l):
    l.client.get(‘/phpadmin/index.php’)

class UserBehavior(TaskSet):
    tasks = {index}

# 执行登陆和登出
    def on_start(self):
        login(self)

    def on_stop(self):
        logout(self)

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 3000
    max_wait = 6000

启动压测程序
如果配置文件名字是 locustfile.py,那么直接运行就好
启动成功会监听 8089 端口,
这里的 http://192.168.1.9 是要压测的网站域名

[root@slave locust]# locust -H http://192.168.1.9
[2018-04-12 19:41:37,086] slave/INFO/locust.main: Starting web monitor at *:8089
[2018-04-12 19:41:37,086] slave/INFO/locust.main: Starting Locust 0.8.1

配置压测集群(可选)

启动压测主程序,跟上面一样,后面加上参数 –master
启动成功会监听 8089 端口,
这里的 http://192.168.1.9 是要压测的网站域名

[root@slave locust]# locust -H http://192.168.1.9 –master
[2018-04-12 22:20:12,889] slave/INFO/locust.main: Starting web monitor at *:8089
[2018-04-12 22:20:12,891] slave/INFO/locust.main: Starting Locust 0.8.1
[2018-04-12 22:20:47,669] slave/INFO/locust.runners: Client ‘k8s_cf9d5e7c4238f74363180e5b3dcb0ae4’ reported as ready. Currently 1 clients ready to swarm.
[2018-04-12 22:21:02,066] slave/INFO/locust.runners: Sending hatch jobs to 1 ready clients
[2018-04-12 22:21:12,069] slave/INFO/locust.runners: Resetting stats

启动 slave
先把 locustfile.py 分发到 slave 机器上,安装 locust,然后启动

[root@k8s locust]# locust -H http://192.168.1.9 –slave –master-host=master_IP
[2018-04-12 02:21:48,045] k8s/INFO/locust.main: Starting Locust 0.8.1
[2018-04-12 02:22:02,487] k8s/INFO/locust.runners: Hatching and swarming 20 clients at the rate 2 clients/s…
[2018-04-12 02:22:12,509] k8s/INFO/locust.runners: All locusts hatched: WebsiteUser: 20
[2018-04-12 02:22:12,509] k8s/INFO/locust.runners: Resetting stats

然后登陆 master 压测就行了

3、开始压测

填写你要模拟的用户数量,这里填 500 用户,以每秒 50 数量增加,点击 start 开始

性能测试框架 locust 入门教程

查看压测请求情况

性能测试框架 locust 入门教程

查看图表

性能测试框架 locust 入门教程

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

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计2019字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中