扼要:
1、了解locust进行性能测试的实例;
2、独立编写locust性能测试代码;
本课程主要讲述用python的locust库进行性能测试。
Locust是一个python代码编写的开源负载测试工具。其支持模拟成千上万的用户,可分布式;报告、运行情况可支持网页观看。
Locust环境搭建
1、安装python,下载地址:https://www.python.org/downloads/
2、安装读取excel的库xlrd,安装命令:pip install xlrd
3、安装locust,安装命令:pip install locust==1.3.1(因为本教程用的locust版本为1.3.1)
简单语法说明
1、简单代码示例
# 基本都需要导入HttpUser, between, task
from locust import HttpUser, between, task
class WebsiteUser(HttpUser):
# 思考时间区间在5-15秒
wait_time = between(5, 15)
# 进行初始化的工作,每个Locust用户开始做的第一件事
def on_start(self):
self.client.get("/home")
# 通过@task()装饰的方法为一个事务,参数用于指定该行为的执行权重,参数越大每次被虚拟用户执行的概率越高,默认为1
@task
def index(self):
# self.client属性使用Python request库的所有方法,调用和使用方法和requests完全一致;
self.client.get("/user")
self.client.get("/details")
格式如下:
简单实例
2、运行脚本:
终端中--->进入到代码目录: locust -f ***.py --host=***.com
3、在浏览器输入:localhost:8089,即可访问locust的设置页面,如下图:
web操作
4、更多资料可参考官方文档:https://www.locust.io/
实例说明
本次采用locust读取Excel表格中的接口进行每个接口压测。
1、Excel表格的内容格式
1.1、接口格式如下: