安装
python 2.7:
python -m pip install locustio
python 3:
python3 -m pip install locustio
查看是否安装成功:
locust --help
编写脚本文件
from locust import HttpLocust, TaskSet, task
class UserBehavior(TaskSet):
def on_start(self):
print('before task is scheduled')
def on_stop(self):
print('task is stopping.')
@task(1) # 1为执行权重,多个任务时权重越大执行概率越大
def novel(self):
print('执行任务')
# self.client.get('/') 可以通过self.client发送http请求
class WebsiteUser(HttpLocust):
host = 'http://www.zongheng.com' # 如果启动时没有指定 --host 参数会用到
task_set = UserBehavior # 定义任务信息
min_wait = 1000 # 等待间隔下界
max_wait = 2000 # 等待间隔上界
"""
每个用户的执行过程:
1.执行on_start,进行初始化工作
2.从多个任务中随机挑选执行,执行概率和权重相关
3.根据时间范围(min_wait,max_wait)随机取一个值,等待
4.重复2,3步骤,直至点击结束
5.执行on_stop
"""
启动
命令行执行:
locust -f locustfile.py --host=http://www.zongheng.com
-f
是脚本文件,--host
是发送http请求的地址,如果脚本文件是当前目录下的locustfile.py
,可以不必添加-f
参数。
浏览器打开localhost:8089
输入参数后,点击start。