locust参数关联及批量注册

前言

前面【Locust性能测试2-先登录场景案例】讲了登录的案例,这种是直接传账号和密码就能登录了,有些登录的网站会复杂一点,
需要先从页面上动态获取参数,作为登录接口的请求参数,如【学信网:https://account.chsi.com.cn/passport/login】的登录接口请求参数

请求参数

需要先发个get请求,从返回的html页面中解析出需要的数据

  • lt : LT-277623-5ldGTLqQhP4foKihHUlgfKPeGGyWVI
  • execution: e1s1

备注:
lt 参数是每次打开浏览器,访问登录首页时服务端会返回一个新的数据
execution 参数是表示网站刷新次数,可以刷新下再登录,就变成 e2s1了

安装lxml,(python的script目录下先pip install  wheel  ,安装成功后,再安装pip install lxml )

前言

实现场景:所有并发虚拟用户共享同一份测试数据,并且保证虚拟用户使用的数据不重复。
例如,模拟10用户并发注册账号,总共有100个手机号,要求注册账号不重复,注册完毕后结束测试

准备数据

虚拟用户 locust1 locust2 locust3 locust4 locust5 locust6 locust7 locust8 locust9 locust10
共享数据 tel1 tel2 tel3 tel4 tel5 tel6 tel7 ...... tel99 tel100

虚拟用户数,可以在启动的时候设置,这里先装备好注册需要用到的手机号,可以用list生成

# 生成测试手机号
demo = 13812120000
teldatas = [str(demo+i) for i in range(100)]
print(teldatas)

将测试数据加到队列

import queue

# 生成测试手机号
demo = 13812120000
teldatas = [str(demo+i) for i in range(100)]
# print(teldatas)

# 添加到队列
telqueue = queue.Queue()
for i in teldatas:
    telqueue.put_nowait(i)

注册demo参考

以下是一个简单的demo模型,具体的注册接口替换过去就可以了

# 保存为 locustfile4.py
#  coding=utf-8
from locust import HttpLocust, TaskSet, task
import queue class test_taskset(TaskSet):  @task def register(self): try: tel = self.locust.telqueue.get() # 获取队列里的数据 print(tel) except queue.Empty: # 队列取空后,直接退出 print("no data exist") exit(0) print("当前注册手机号:%s" % tel) # body = { # "username": tel, # "psd": "123456", # } # self.client.post("/register", data=body) # POST方法发送请求 class test_run(HttpLocust): host = 'http://192.168.1.xxx:80' task_set = test_taskset # 生成测试手机号 teldatas = [str(13812120000+i) for i in range(100)] # 添加到队列 telqueue = queue.Queue() for i in teldatas: telqueue.put_nowait(i) if __name__ == "__main__": import os os.system("locust -f locustfile4.py")

cmd命令行启动

$ locust -f locustfile4.py

打开web页面,输入10个用户, 可以看到控制台的打印

猜你喜欢

转载自www.cnblogs.com/youtiaoge/p/10168532.html