from locust import HttpLocust,TaskSet,task
'''
點擊STOP,會停止測試,並調用所有當前執行的TaskSet的on_stop,但不會調用teardown函數
ctrl +c,表示停止locust運行,此時會調用TaskSet teardown # 停止locust運行時執行,Locust teardown # 停止locust運行時執行,(而不會調用TaskSet的on_stop,點擊STOP,會停止測試,並調用所有當前執行的TaskSet的on_stop,但不會調用teardown函數)
stop_timeout:Locust停止的秒數,如果為None,將不停止一直執行任務,單位為s秒
-H:指定測試的主機地址(注:會覆蓋Locust類指定的主機地址)
-f:指定測試腳本地址(注:腳本中必須包含一個Locust的衍生類)
--no-web:不啟動web網頁,而是直接開始運行測試,需提供屬性-c和-r
-c:並發的用戶數,與--no-web一起使用
-r:每秒啟動的用戶數,與--no-web一起使用
-t:運行時間(單位:秒),與--no-web一起使用
-L:日志級別,默認為INFO
調試命令:locust -f **.py --no-web -c 1 -t 1
運行命令:locust -f **.py
Locust setup # 第一次啟動Locust的時候執行
TaskSet setup # 第一次實例化任務集時執行
TaskSet on_start # 每一次開始一個任務時執行
TaskSet tasks…
TaskSet on_stop # 點擊頁面stop時,當前所有在執行中的TaskSet執行
TaskSet teardown # 停止locust運行時執行
Locust teardown # 停止locust運行時執行
'''
class mytaskset(TaskSet):
def setup(self):
print("TaskSet setup")
def on_start(self):
print("TaskSet on_start")
@task(1)
def one(self):
print("任務1")
@task(1)
def two(self):
print("任務2")
def on_stop(self):
print("TaskSet stop")
def teardown(self):
print("TaskSet teardown")
class websitUser(HttpLocust):
weight = 1
task_set = mytaskset
host = "http://www.baidu.com"
min_wait = 5000
max_wait = 6000
stop_timeout = 10
def setup(self):
print("locust set_up")
def teardown(self):
print("locust teardown")

