python+locust性能测试
1.
# loginxn.py
# coding=utf-8
import time
from locust import HttpLocust, TaskSet, task
import json
from shaiqi_interfaceTest.xingnengtest.Valuesreplace import Valuesreplace
'''
实现场景:测试登录注册页面点击多次的功能
'''
class loginxn(TaskSet):
'''蝗虫行为类'''
def on_start(self):
pass
@task(1)
def loginset(self):
print("---登录注册提交多次---")
TimestampALL = lambda: int(round(time.time() * 1000))
Timestamp = str(TimestampALL())
h = {
"User-Agent": "okhttp/3.8.0",
"Content-Type": "application/json;charset=UTF-8",
"Accept-Encoding": "gzip",
"Connection": "keep-alive",
"Timestamp": Timestamp
}
body = {
"phone": "18XXXXX",
"code": "969381",
"client_id": "0f5138e5dXXXXXXXXXXXX",
"client_type": "android"
}
keys=['code']
#生成新的验证码并替换
bodydata = Valuesreplace().replace_target(keys, body,"1806XXXXXXX")
response = self.client.post("/auth/app/login", data=json.dumps(bodydata), headers=h, catch_response=True)
print(response.text)
if "8001" not in response.text:
response.failure("Failed!")
else:
response.success()
#
class WebsiteUser(HttpLocust):
task_set = loginxn
min_wait = 1000
max_wait = 2000
if __name__ == "__main__":
import os
#--host地址是和压测的地址连载一起就是压测的地址
os.system("locust -f loginxn.py --host=http://gateway.lierda.com")
2.模拟用户总数和每秒并发数