可以在命令行启动,cmd命令框,进入此文件的目录输入:
locust -f .\dash.py --host="https://litemall.hogwarts.ceshiren.com"
-f: 指定性能测试脚本文件的绝对路径。
–host: 指定被测试应用的URL的地址,就是测试项目的host地址。
Number of users:设置模拟用户数。(并发)
Spawn rate :每秒产生(启动)的虚拟用户数。(递增数)
Host:host地址。//可在WebsiteUser类下直接写host = "www.*.com",启动命令不加host参数,这里就不会显示,则默认走代码写死的host地址。
点击 “Start swarming” 按钮,立即开始运行性能测试。
改进前,账密未参数化:
import random
from locust import HttpUser, task, between, SequentialTaskSet, tag
class MyTaskCase(SequentialTaskSet):
# 初始化方法,相当于 setup
def on_start(self):
pass
# @task python中的装饰器,告诉下面的方法是一个任务,
# 这个装饰器和下面的方法被复制多次,改动一下,就能写出多个接口
# 装饰器后面带上(数字)代表在所有任务中,执行比例
# 要用这个装饰器,需要头部引入 从locust中,引入 task
@task(1) # 装饰器,说明下面是一个任务
def login_(self):
self.username = "hogwarts"
self.pwd = 'test12345'
self.headers = {"Content-Type": "application/json"}
url = '/admin/auth/login' # 接口请求的URL地址
data = {"username": self.username, "password": self.pwd, "code": ""}
with self.client.post(url,
json=data,
headers=self.headers,
catch_response=True) as rsp:
# 提取响应json 中的信息,定义为 类变量
self.token = rsp.json()['data']['token']
if rsp.status_code == 200 and rsp.json()['errno'] == 0:
rsp.success()
else:
rsp.failure('login_failure')
@task(2) # 装饰器,说明下面是一个任务
def getlist_(self):
url = '/admin/region/list' # 接口请求的URL地址
# 引用上一个任务的 类变量值 实现参数关联
headers = {"X-Litemall-Admin-Token": self.token}
# 使用self.client发起请求,请求的方法 选择 get
with self.client.get(url,
headers=headers,
catch_response=True) as rsp:
if rsp.status_code == 200 and rsp.json()['errno'] == 0:
rsp.success()
else:
rsp.failure('getlist_failure')
# 结束方法, 相当于teardown
def on_stop(self):
pass
# 定义一个运行类 继承HttpUser类, 所以要从locust中引入 HttpUser类
class UserRun(HttpUser):
tasks = [MyTaskCase]
# 设置运行过程中间隔时间 需要从locust中 引入 between
# wait_time = between(0.1, 3)
改进后,账密读取csv文件参数化:
import random
from locust import HttpUser, task, between, SequentialTaskSet, tag
class MyTaskCase(SequentialTaskSet):
# 初始化方法,相当于 setup
def on_start(self):
pass
# @task python中的装饰器,告诉下面的方法是一个任务,
# 这个装饰器和下面的方法被复制多次,改动一下,就能写出多个接口
# 装饰器后面带上(数字)代表在所有任务中,执行比例
# 要用这个装饰器,需要头部引入 从locust中,引入 task
@task(1) # 装饰器,说明下面是一个任务
def login_(self):
with open('user-pwd.csv', 'r', ) as ud:
for line in ud:
temp = line.strip().split(",")
self.username = temp[0]
self.pwd = temp[1]
self.headers = {"Content-Type": "application/json"}
url = '/admin/auth/login' # 接口请求的URL地址
data = {"username": self.username, "password": self.pwd, "code": ""}
with self.client.post(url,
json=data,
headers=self.headers,
catch_response=True) as rsp:
if rsp.status_code == 200 and rsp.json()['errno'] == 0:
rsp.success()
# 提取响应json 中的信息,定义为 类变量
self.token = rsp.json()['data']['token']
print("sucess", self.pwd, self.username)
else:
rsp.failure('login_failure')
print("fail", self.pwd, self.username)
@task(2) # 装饰器,说明下面是一个任务
def getlist_(self):
url = '/admin/region/list' # 接口请求的URL地址
# 引用上一个任务的 类变量值 实现参数关联
headers = {"X-Litemall-Admin-Token": self.token}
# 使用self.client发起请求,请求的方法 选择 get
with self.client.get(url,
headers=headers,
catch_response=True) as rsp:
if rsp.status_code == 200 and rsp.json()['errno'] == 0:
rsp.success()
else:
rsp.failure('getlist_failure')
# 结束方法, 相当于teardown
def on_stop(self):
pass
# 定义一个运行类 继承HttpUser类, 所以要从locust中引入 HttpUser类
class UserRun(HttpUser):
tasks = [MyTaskCase]
# 设置运行过程中间隔时间 需要从locust中 引入 between
# wait_time = between(0.1, 3)
把读取csv独立封装,结合标签--tags、给定运行时间-t来执行压测任务;
import os
import random
import csv
from locust import HttpUser, task, between, SequentialTaskSet, tag
class MyTaskCase(SequentialTaskSet):
# 初始化方法,相当于 setup
def on_start(self):
print("***压测开始***")
def get_csv_data(self, filename, delimiter=','):
with open(filename, mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file, delimiter=delimiter)
return [row for row in csv_reader]
@tag("auth_ppprtk")
@task(1) # 装饰器
# /cop-operation/public/sku/auth
def auth_ppprtk(self):
csv_data = self.get_csv_data('E:\压测相关\客户运营管理平台\测试账号清单_ppprtk_h.csv')
for row in csv_data:
self.account = row['ACCOUNT']
self.pwd = row['PWD']
self.headers = {"Content-Type": "application/json"}
url = '/cop-operation/public/sku/auth'
data = {"account": self.account, "password": self.pwd, "instCode": "NTRIP", "type": "Ntrip"}
with self.client.post(url,
json=data,
headers=self.headers,
catch_response=True) as rsp:
if rsp.status_code == 200 and rsp.json()['code'] == 200:
rsp.success()
print("sucess", self.pwd, self.account)
else:
rsp.failure('auth_failure')
# print("fail", self.pwd, self.account)
@tag("auth_nrtk")
@task(1) # 装饰器
# /cop-operation/public/sku/auth
def auth_nrtk(self):
with open('E:\压测相关\客户运营管理平台\测试账号清单_nrtk.csv', 'r', ) as ud:
for line in ud:
temp = line.strip().split(",")
self.account = temp[5]
self.pwd = temp[6]
self.headers = {"Content-Type": "application/json"}
url = '/cop-operation/public/sku/auth'
data = {"account": self.account, "password": self.pwd, "instCode": "NTRIP", "type": "Ntrip"}
with self.client.post(url,
json=data,
headers=self.headers,
catch_response=True) as rsp:
if rsp.status_code == 200 and rsp.json()['code'] == 200:
rsp.success()
# print("sucess", self.pwd, self.account)
else:
rsp.failure('auth_failure')
# print("fail", self.pwd, self.account)
# 结束方法, 相当于teardown
def on_stop(self):
print("***压测结束***")
# 定义一个运行类 继承HttpUser类, 所以要从locust中引入 HttpUser类
class UserRun(HttpUser):
tasks = [MyTaskCase]
# 运营管理平台的域名
host = 'http://cop-operation.xxxx.cn/'
if __name__ == '__main__':
file_path = os.path.abspath(__file__)
os.system(f"locust -f {file_path} --tags auth_ppprtk -t 5m")