python locust 性能測試:locust 關聯---提取返回數據並使用


from locust import HttpLocust, TaskSet, task
import json
from common import readConfig


class UserBehavior(TaskSet): # 定義用戶行為

def on_start(self): # 當模擬用戶開始執行TaskSet類的時候,on_start方法會被調用
        pass

def get_headers(self):
   """會員登錄"""
headers = {
"Content-Type": "application/json",
"channel": "SHOP"
}
body = {
"unionid": readConfig.unionid # 這樣使用會報錯,沒有 common ...
}
res = self.client.post('/customers/login', headers=headers, data=json.dumps(body)).text
assert '成功' in res # 斷言,判斷接口返回是否成功
res = json.loads(res)
uid = res['data']['uid']
ukey = res['data']['ukey']
return [uid, ukey] # 會員登錄返回的uid和ukey

@task(1) # @task() 裝飾該方法為一個任務;1表示一個Locust實例被挑選執行的權重,數值越大,執行頻率越高;
def members_can_withdraw_account_information(self): # 一個行為
"""使會員獲取會員狀態"""
res = self.get_headers()
headers = {
"channel": "SHOP",
"uid": str(res[0]),
"ukey": res[1]
}
response = self.client.put("/customers/customer_state",
headers=headers).text # client.get()用於指請求的路徑,可加headers,params,body參數;
assert '成功' in response

@task(1)
def get_member_account_withdrawal_details(self):
"""獲取會員賬戶提現明細"""
res = self.get_headers()
headers = {
"channel": "SHOP",
"uid": str(res[0]),
"ukey": res[1]
}
params = {
"page": "10",
"size": "10"
}
response = self.client.get("/customers/account/withdraw/log", headers=headers, params=params).text
assert '成功' in response


class WebsiteUser(HttpLocust): # 設置性能測試;
host = "http://dev.sign.lixiaofeng.com"
task_set = UserBehavior # 指向一個定義了的用戶行為類;
min_wait = 3000 # 用戶執行任務之間等待時間的下界,單位:毫秒;
max_wait = 6000 # 用戶執行任務之間等待時間的上界,單位:毫秒;

參考:http://debugtalk.com/post/head-first-locust-advanced-script/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM