1.
# UserBehavior.py # coding=utf-8 import time from locust import HttpLocust, TaskSet, task import json import requests from interfaceTest.Common.getValuesreplace import Valuesreplace ''' 实现场景:测试登录注册页面点击多次的功能 ''' class UserBehavior(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": "15XXX75", "code": "969381", "client_id": "0f5XXXX95c285b", "client_type": "android" } #去数据库中获取数据code值再替换 keys=['code'] bodydata = Valuesreplace().replace_target(keys, eval(body)) get_url = "http://XXXX/auth/app/login" print("get_url:" + get_url) response_Code = requests.request('post', url=get_url, headers=h,body=bodydata) print(response_Code.text) if "8001" not in response_Code.text: response_Code.failure('Failed!') else: response_Code.success() # class WebsiteUser(HttpLocust): task_set = UserBehavior min_wait = 1000 max_wait = 2000 if __name__ == "__main__": import os #--host地址是和压测的地址连载一起就是压测的地址 os.system("locust -f loginxn.py --host=http://XXXX")
2.模拟用户总数和每秒并发数