import locust
#from gevent._semaphore import Semaphore
import gevent #集合點:多用戶同一時間并發操作(監控平臺設定的用戶數,如100,20,一共100個用戶,每秒增加5個,則會等到100個用戶的時候才一起發請求。
#三、集合點
all_locusts_spawned =gevent._semaphore.Semaphore() #集合類實體化
all_locusts_spawned.acquire()#加鎖
def on_hatch_complete(**kwargs):
all_locusts_spawned.release()#釋放鎖
# 掛在到locust鉤子函式(所有的Locust實體產生完成時觸發)
#老版本:locust.events.hatch_complete += on_hatch_complete
locust.events.spawning_complete.add_listener(on_hatch_complete())
#二、主函式呼叫的任務類,類名可隨意
class UserBehavior2(locust.TaskSet):
#1. 建構式,可放變數
def on_start(self):
self.index=0
print("======onstart begin :")
all_locusts_spawned.wait() # 集合點:創建鉤子方法
print("======onstart end:")
@locust.task
def test_visit(self):
url = "/getJoke?page=" + str(self.parent.share_data[2]) + "&count=" + str(self.index + 2)
res=self.client.get(url, name='getJoke分頁查詢')
res=res.text
#一.繼承locust.HttpUser的類為主函式,相當于main,類名可隨意
class WebSiteUser232323(locust.HttpUser): #locust.HttpLocust
#1.被測網站域名
host = 'https://api.apiopen.top'
#3.性能測驗任務類:登錄還是注冊
tasks=[UserBehavior2] #呼叫哪個任務
#4.等待時間設定
wait_time = locust.between(3,7)
#locust -f locustS.py --host=https://www.baidu.com localhost:8089
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/282199.html
標籤:軟件測試
