最近在做壓力測驗,jemeter使用起來不穩定,而消耗電腦記憶體太大,loadrunner是收費的,雖有破解,但不太道德!后來使用了開源的locust
locust是基于協程的開源的壓力自動化測驗框架,網上我也找了大量的資料配置及示例,但都是基于舊版本的,在新版本一直報錯,后來通過查找官方網站的示例,總算知道了原因——是因為locust升級之后,一些引數及變數發生了變化(具體如何從官方網站說明檔案怎么研究本文不作贅述)
大部分網站上都有下述代碼:
class WebsiteUser(HttpLocust):
task_set = UserBehavior #呼叫自定義方法 UserBehavior
min_wait = 3000 #最小等待時間
max_wait = 6000 #最大等待時間
現在改成了:
class UserBehavior(HttpUser): #
wait_time = between(5, 15) #直接把等待時間范圍使用wait_time寫在了自定義方法UserBehavior里
且呼叫的時候在python檔案之后加上自定義方法的引數UserBehavior 即可
os.system("locust -f load_test.py UserBehavior --host=http://xxxx.xxxx.xxxx.xxxx:8090/YLAPI")
以下為完整代碼:
from locust import User, task, between, HttpUser
class UserBehavior(HttpUser):
wait_time = between(5, 15)
@task(1) #Post請求
def firstTest(self):
header = {"Content-Type": "application/json"}
payload = {
"versionNum": "10",
"platform": "android"
}
req = self.client.post("/Api/app/version/appCheck", data=https://www.cnblogs.com/heliqiang/p/payload, headers=header, verify=False)
if req.status_code == 200:
print("success")
else:
print("fails:" + str(req.status_code))
# @task(1) #get請求
# def about(self):
# self.client.get("/about/")
if __name__ == "__main__":
import os
#os.system("locust -f load_test.py UserBehavior")
os.system("locust -f load_test.py UserBehavior --host=http://xxxx.xxxx.xxxx.22:8090/YLAPI")
Enjoy your latest version locust script code on your hand :-)
reference:
1) https://www.cnblogs.com/lxmtx/p/12580031.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/239409.html
標籤:Python
上一篇:PHP設計模式之代理模式
