效能測試】【locust】場景效能測試
阿新 • • 發佈:2020-08-19
場景設計
- 實現登陸基本功能,輸出相應結果,指令碼通
- 多使用者實現隨機登陸
- 新增初始化方法on_start: 每個使用者只執行一次
- 新增檢查點: catch_responses = True
指令碼設計
# 匯入對應的庫 from locust import HttpUser, task, between import os # 任務類 class TestLogin(TaskSet): # 定義一個登陸任務 @task def to_login(self): """ 1. 登陸需要賬號密碼,body以json格式 2. 登陸是post請求 '/v1/token' 3. 列印res值 """ data = {"account": "[email protected]", "password": "123456"} res = self.client.post("/v1/token", json=data) print(res.text) class WebUser(HttpUser): tasks = [TestLogin] # 任務執行之間等待的時間,最小2秒,最大5秒 wait_time = between(2, 5) # 被測地址:這個介面是自己寫的一個登陸介面,實際可以需要自己抓包,更換真實環境中的地址 host = "http://127.0.0.1:5000" if __name__ == '__main__': os.system("locust -f testsm_demo.py") # 設定使用者1,每秒啟動使用者1,確定指令碼是通,從打印出來的內容我們可以看出,指令碼是通過的(這裡你也可以開啟locust webUI介面) [2020-08-19 10:53:19,249] md2bkpyc/INFO/locust.runners: All users hatched: WebUser: 1 (0 already running) { "error_code": 0, "token": "eyJhbGciOiJIUzUxMiIsImlhdCI6MTU5NzgwNTYwNCwiZXhwIjoxNTk3ODEyODA0fQ.eyJ1aWQiOjEsInNjb3BlIjpudWxsfQ.GS-lHngBcboPvnipWOl1ad5bkSEYweLwzSMa8fZpYVoz5ZDndV0gJOPvUiu8oiXIZi6AR7amLUcG7KKcBsf0Cg" }
多使用者實現隨機登陸
- 定義一組登陸賬號
- 匯入choice方法,從data中隨機選一個登陸
__author__ = 'wangxiao' # 匯入對應的庫 from locust import HttpUser, task, between, TaskSet import os from random import choice # 任務類 class TestLogin(TaskSet): @task def to_login(self): data = [{"account": "[email protected]", "password": "123456"}, {"account": "[email protected]", "password": "123456"}, {"account": "[email protected]", "password": "123456"} ] res = self.client.post("/v1/token", json=choice(data)) print(res.text) class WebUser(HttpUser): tasks = [TestLogin] wait_time = between(2, 5) host = "http://127.0.0.1:5000" if __name__ == '__main__': os.system("locust -f testsm_demo.py")
- 設計場景,總共5個使用者,每秒啟動5個,圖表
新增初始化方法on_start
- on_start 與on_stop方法對應,每個虛擬使用者執行操作時前會執行一次on_start 推遲執行 on_stop
- 重構方法,把data放到on_start中
__author__ = 'wangxiao' # 匯入對應的庫 from locust import HttpUser, task, between, TaskSet import os from random import choice # 任務類 class TestLogin(TaskSet): def on_start(self): self.data = [{"account": "[email protected]", "password": "123456"}, {"account": "[email protected]", "password": "123456"}, {"account": "[email protected]", "password": "123456"} ] def on_stop(self): print("task結束") @task def to_login(self): res = self.client.post("/v1/token", json=choice(self.data)) print(res.text) class WebUser(HttpUser): tasks = [TestLogin] wait_time = between(2, 5) host = "http://127.0.0.1:5000" if __name__ == '__main__': os.system("locust -f testsm_demo.py")
設定檢查點: catch_responses = True
- catch_response: 布林值,True or False, 用於傳送請求,作為with語句返回上下文管理器的引數,將根據不同的響應將請求內容標記成功or失敗,所以這邊用 catch_response來捕捉,注意要用with語句來返回上下文
- success() failure(): 響應類中充當了上下文管理器,他提供了兩個方法success() or failure()
- 指令碼例項
__author__ = 'wangxiao'
# 匯入對應的庫
from locust import HttpUser, task, between, TaskSet
import os
from random import choice
# 任務類
class TestLogin(TaskSet):
def on_start(self):
self.data = [{"account": "[email protected]", "password": "123456"},
{"account": "[email protected]", "password": "123456"},
{"account": "[email protected]", "password": "123456"},
{"account": "[email protected]", "password": "12334456"}
]
@task
def to_login(self):
with self.client.post("/v1/token", json=choice(self.data), catch_response=True) as response:
res = response.json()
if res['error_code'] == 0:
response.success()
else:
response.failure(res['msg'])
def on_stop(self):
print("task結束")
class WebUser(HttpUser):
tasks = [TestLogin]
wait_time = between(2, 5)
host = "http://127.0.0.1:5000"
if __name__ == '__main__':
os.system("locust -f testsm_demo.py")
- 圖表分析
- 錯誤資訊提示