Locust性能測試2-先登錄場景案例


前言

有很多網站不登錄的話,是無法訪問到里面的頁面的,這就需要先登錄了
實現場景:先登錄(只登錄一次),然后訪問頁面->我的地盤頁->產品頁->項目頁

官方案例

下面是一個簡單的locustfile.py的簡單示例:

from locust import HttpLocust, TaskSet

def login(l):
    l.client.post("/login", {"username":"ellen_key", "password":"education"})

def logout(l):
    l.client.post("/logout", {"username":"ellen_key", "password":"education"})

def index(l):
    l.client.get("/")

def profile(l):
    l.client.get("/profile")

class UserBehavior(TaskSet):
    tasks = {index: 2, profile: 1}

    def on_start(self):
        login(self)

    def on_stop(self):
        logout(self)

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 5000
    max_wait = 9000

這里我們定義了許多Locust任務,它們是帶有一個參數(Locust類實例)的普通Python callables 。這些任務收集在tasks屬性的TaskSet類下 。然后我們有一個代表用戶的 類,我們在其中定義模擬用戶在執行任務之間應該等待多長時間,以及哪個 類應該定義用戶的“行為”。 類可以繼承HttpLocust、TaskSet、TaskSet

HttpLocust類從繼承 Locust的類,並把它添加一個客戶端屬性,它是的一個實例 HttpSession,可用於使HTTP請求。

另一種我們可以聲明任務的方法,通常是更方便,就是使用 @task裝飾器。以下代碼與上述代碼相同:

from locust import HttpLocust, TaskSet, task

class UserBehavior(TaskSet):
    def on_start(self):
        """ on_start is called when a Locust start before any task is scheduled """
        self.login()

    def on_stop(self):
        """ on_stop is called when the TaskSet is stopping """
        self.logout()

    def login(self):
        self.client.post("/login", {"username":"ellen_key", "password":"education"})

    def logout(self):
        self.client.post("/logout", {"username":"ellen_key", "password":"education"})

    @task(2)
    def index(self):
        self.client.get("/")

    @task(1)
    def profile(self):
        self.client.get("/profile")

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 5000
    max_wait = 9000

在Locust類(以及HttpLocust 因為它是一個子類),也可以讓一個在指定最小和最大等待時間毫秒,每個模擬用戶之間的任務執行(min_wait和MAX_WAIT)以及其他用戶的行為。默認情況下,時間是在min_wait和max_wait之間統一隨機選擇的,但是可以通過將wait_function設置為任意函數來使用任何用戶定義的時間分布。例如,對於指數分布的等待時間平均為1秒:

import random

class WebsiteUser(HttpLocust):
    task_set = UserBehaviour
    wait_function = lambda self: random.expovariate(1)*1000

項目實例

上面的官方案例只是一些偽代碼,不能在真實的環境中跑起來,接下來把上面的理論執行用到真實的項目環境中
http協議是無狀態的,所以登錄請求和登錄后的請求它是獨立的,但是登錄后的請求需要依賴先登錄拿到cookies,才能保持登錄狀態,這個在之前python接口自動化里面可以用session來解決

s = requests.session()

HttpLocust類從繼承 Locust的類,並把它添加一個客戶端屬性,它是的一個實例 HttpSession,可用於使HTTP請求,這就相當於它自動使用了session機制,類似於client = requests.session()
所以后面的請求,直接拿client.get()、client.post()請求就可以了

# 保存為locustfile.py
# coding=utf-8
from locust import HttpLocust, TaskSet, task

'''
實現場景:先登錄(只登錄一次),然后訪問->我的地盤頁->產品頁->項目頁
訪問我的地盤頁面權重為2,產品頁和項目頁權重各為1
***作者:上海-悠悠 QQ群:588402570**
'''

class UserBehavior(TaskSet):
    '''蝗蟲行為類'''
    def _login(self):
        '''登錄方法'''
        # host = 'http://192.168.x.xx:80'  # 禪道的服務器地
        loginUrl ="/zentao/user-login.html/"
        h = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0",
            "Content-Type": "application/x-www-form-urlencoded",
            }
        body = {"account": "yoyo",  # 你自己的賬號
                "password": "******",    # 你自己的密碼
                "keepLogin[]": "on",
                "referer": "/zentao/my/"
                }
        r = self.client.post(loginUrl, data=body, headers=h)
        print(r.text)
        assert "parent.location='/zentao/index.html'" in r.text

    def on_start(self):
        '''任務開始准備工作:只登錄一次'''
        self._login()

    # 任務1-我的地盤
    @task(2)
    def zentao_my(self):
        print("---訪問頁面-我的地盤---")
        r = self.client.get("/zentao/my/")
        assert "我的地盤" in r.text

    # 任務2-產品頁
    @task(1)
    def zentao_product(self):
        print("---訪問頁面-產品頁---")
        r = self.client.get("/zentao/product-browse.html/")
        assert "需求列表" in r.text

    # 任務3-項目
    @task(1)
    def zentao_prject(self):
        print("---訪問頁面-項目---")
        r = self.client.get("/zentao/project/")
        assert "項目首頁" in r.text


class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 1000
    max_wait = 1000

if __name__ == "__main__":
    import os
    os.system("locust -f locustfile.py --host=http://192.168.x.xx:80")

設置1個虛擬用戶,每秒啟動1個服務,點start后運行結果

從結果可以看到登錄的請求只訪問了一次,然后是“我的地盤”頁面的次數差不多是產品頁、項目頁請求次數的2倍(這個只是概率上講是2倍,不完全等於2倍)
定義on_start()相當於用例的准備操作,當然還有on_stop用於數據清理操作

*作者:上海-悠悠 QQ群:588402570


免責聲明!

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



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