1. 程式人生 > 其它 >locust工具學習筆記(四)

locust工具學習筆記(四)

技術標籤:效能測試Pythonlocustpythonlocust壓力測試

SequentialTaskSet 類

在locust中使用者(執行緒)執行任務是隨機的,如果需要讓任務執行有一定順序則可以將taskset繼承SequentialTaskSet 類來實現。

寫法一
from locust import User,SequentialTaskSet,task,constant

class MyUserBe(SequentialTaskSet):
    wait_time = constant(1)

    @task
    def my_task1(self):
        print("使用者行為1")
    @task
    def my_task2(self):
        print("使用者行為2")

    @task
    def my_task3(self):
        print("使用者行為3")

    @task
    def my_task4(self):
        print("使用者行為4")

class MyUser(User):
    wait_time = constant(1)
    tasks = [MyUserBe]
    
寫法二、
from locust import User,SequentialTaskSet,task,constant

class MyUser(User):
    wait_time = constant(1)
    @task
    class MyUserBe(SequentialTaskSet):
        wait_time = constant(1)

        @task
        def my_task1(self):
            print("使用者行為1")

        @task
        def my_task2(self):
            print("使用者行為2")

        @task
        def my_task3(self):
            print("使用者行為3")

        @task
        def my_task4(self):
            print("使用者行為4")    
    
#每一個啟動的使用者都會順序執行my_task1,my_task2,my_task3,my_task4
[2021-01-24 10:04:21,117] SKY-20210118WDX/INFO/locust.runners: Spawning 1 users at the rate 1 users/s (0 users already running)...
[2021-01-24 10:04:21,122] SKY-20210118WDX/INFO/locust.runners: All users spawned: MyUser: 1 (1 total running)
使用者行為1
使用者行為2
使用者行為3
使用者行為4
使用者行為1
使用者行為2
使用者行為3
使用者行為4
使用者行為1
使用者行為2
使用者行為3
使用者行為4
使用者行為1
使用者行為2
使用者行為3
使用者行為4
使用者行為1
使用者行為2
使用者行為3
使用者行為4

event hooks

event hooks位於該events屬性下的Environment例項上。但是,由於在匯入locustfile時尚未建立Environment例項,因此也可以通過locust.events變數在蝗蟲檔案的模組級別訪問event物件 。

設定事件監聽器的示例

from locust import events,HttpUser,constant,task
#@events.request_failure.add_listener()   #設定事件監聽器-請求失敗時將被執行
@events.request_success.add_listener   #設定事件監聽器-請求成功時將被執行
def process_success_requests(request_type,name,response_time,response_length,**kwargs):
    print('success Type: {}, Name: {}, Respose_Time: {}, Length: {}'.format(request_type,name,response_time,response_length))

class MyUser(HttpUser):
    wait_time = constant(1)

    @task
    def open_index(self):
        self.client.get("https://www.baidu.com/")
        
        
        
 H:\2021\PythonProject\Learning>locust -f H:\2021\PythonProject\Learning\locust_demo17.py
[2021-01-24 10:53:32,873] SKY-20210118WDX/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces)
[2021-01-24 10:53:32,889] SKY-20210118WDX/INFO/locust.main: Starting Locust 1.4.1
[2021-01-24 10:53:51,655] SKY-20210118WDX/INFO/locust.runners: Spawning 3 users at the rate 1 users/s (0 users already running)...
success Type: GET, Name: /, Respose_Time: 234.00000000037835, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 233.99999999946886, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 63.00000000010186, response_Length: 2443
[2021-01-24 10:53:53,664] SKY-20210118WDX/INFO/locust.runners: All users spawned: MyUser: 3 (3 total running)
success Type: GET, Name: /, Respose_Time: 108.99999999946886, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 46.99999999957072, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 46.000000000276486, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 14.999999999417923, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 30.99999999994907, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 32.000000000152795, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 16.000000000531145, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 15.99999999962165, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 32.000000000152795, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 15.000000000327418, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 14.999999999417923, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 15.000000000327418, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 15.99999999962165, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 16.000000000531145, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 15.000000000327418, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 78.99999999972351, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 46.99999999957072, response_Length: 2443
success Type: GET, Name: /, Respose_Time: 46.99999999957072, response_Length: 2443

with管理上下文

with…as …關鍵字的作用相當於try/finally。使用with時為了簡化程式碼使之更具可讀性。

from locust import User,task,constant

class MyUser(User):
    wait_time = constant(1)
    @task
    def my_task1(self):
        with self.client.get("/", catch_response=True) as response:   
            if response.text != "Success":
                response.failure("Got wrong response")
            elif response.elapsed.total_seconds() > 0.5:
                response.failure("Request took too long")

logging日誌

在廁所過程中如果需要列印日誌用於除錯,則就需要使用日誌管理logging。

logging模組是python標準庫中得模組。

from locust import User,SequentialTaskSet,task,constant
import logging as log

class MyUserBe(SequentialTaskSet):
    wait_time = constant(1)

    @task
    def my_task1(self):
        log.error("使用者行為1")
    @task
    def my_task2(self):
        log.info("使用者行為2")

    @task
    def my_task3(self):
        log.debug("使用者行為3")

    @task
    def my_task4(self):
        log.warning("使用者行為4")

class MyUser(User):
    wait_time = constant(1)
    tasks = [MyUserBe]
#在當前目錄下回生產locust.log日誌檔案
#locust -f H:\2021\PythonProject\Learning\locust_demo20.py  --logfile=locust.log
[2021-01-24 14:09:15,737] SKY-20210118WDX/INFO/locust.runners: Spawning 5 users at the rate 1 users/s (0 users already running)...
[2021-01-24 14:09:15,745] SKY-20210118WDX/ERROR/root: 使用者行為1
[2021-01-24 14:09:16,745] SKY-20210118WDX/ERROR/root: 使用者行為1
[2021-01-24 14:09:16,817] SKY-20210118WDX/INFO/root: 使用者行為2
[2021-01-24 14:09:17,753] SKY-20210118WDX/INFO/root: 使用者行為2
[2021-01-24 14:09:17,753] SKY-20210118WDX/ERROR/root: 使用者行為1
[2021-01-24 14:09:18,755] SKY-20210118WDX/INFO/root: 使用者行為2
[2021-01-24 14:09:18,755] SKY-20210118WDX/ERROR/root: 使用者行為1
[2021-01-24 14:09:18,827] SKY-20210118WDX/WARNING/root: 使用者行為4
[2021-01-24 14:09:19,755] SKY-20210118WDX/INFO/locust.runners: All users spawned: MyUser: 5 (5 total running)
[2021-01-24 14:09:19,755] SKY-20210118WDX/WARNING/root: 使用者行為4
[2021-01-24 14:09:19,755] SKY-20210118WDX/ERROR/root: 使用者行為1
[2021-01-24 14:09:19,755] SKY-20210118WDX/INFO/root: 使用者行為2
[2021-01-24 14:09:19,827] SKY-20210118WDX/ERROR/root: 使用者行為1
[2021-01-24 14:09:20,763] SKY-20210118WDX/ERROR/root: 使用者行為1
[2021-01-24 14:09:20,763] SKY-20210118WDX/WARNING/root: 使用者行為4
[2021-01-24 14:09:20,763] SKY-20210118WDX/INFO/root: 使用者行為2
[2021-01-24 14:09:20,835] SKY-20210118WDX/INFO/root: 使用者行為2
[2021-01-24 14:09:21,771] SKY-20210118WDX/INFO/root: 使用者行為2
[2021-01-24 14:09:21,771] SKY-20210118WDX/ERROR/root: 使用者行為1
[2021-01-24 14:09:21,771] SKY-20210118WDX/WARNING/root: 使用者行為4
[2021-01-24 14:09:22,779] SKY-20210118WDX/INFO/root: 使用者行為2
[2021-01-24 14:09:22,779] SKY-20210118WDX/WARNING/root: 使用者行為4
[2021-01-24 14:09:22,779] SKY-20210118WDX/ERROR/root: 使用者行為1
[2021-01-24 14:09:22,850] SKY-20210118WDX/WARNING/root: 使用者行為4
[2021-01-24 14:09:23,786] SKY-20210118WDX/WARNING/root: 使用者行為4
[2021-01-24 14:09:23,786] SKY-20210118WDX/ERROR/root: 使用者行為1
[2021-01-24 14:09:23,786] SKY-20210118WDX/INFO/root: 使用者行為2
[2021-01-24 14:09:23,858] SKY-20210118WDX/ERROR/root: 使用者行為1
[2021-01-24 14:09:24,794] SKY-20210118WDX/ERROR/root: 使用者行為1
[2021-01-24 14:09:24,794] SKY-20210118WDX/WARNING/root: 使用者行為4
[2021-01-24 14:09:24,794] SKY-20210118WDX/INFO/root: 使用者行為2
[2021-01-24 14:09:24,866] SKY-20210118WDX/INFO/root: 使用者行為2
[2021-01-24 14:09:25,802] SKY-20210118WDX/INFO/root: 使用者行為2
[2021-01-24 14:09:25,802] SKY-20210118WDX/ERROR/root: 使用者行為1
[2021-01-24 14:09:25,802] SKY-20210118WDX/WARNING/root: 使用者行為4
[2021-01-24 14:09:26,810] SKY-20210118WDX/INFO/root: 使用者行為2
[2021-01-24 14:09:26,810] SKY-20210118WDX/WARNING/root: 使用者行為4
[2021-01-24 14:09:26,810] SKY-20210118WDX/ERROR/root: 使用者行為1
[2021-01-24 14:09:26,882] SKY-20210118WDX/WARNING/root: 使用者行為4

歡迎大家關注我的訂閱號,會定期分享一些關於測試相關的文章,有問題也歡迎一起討論學習!
在這裡插入圖片描述