Locust學習筆記3——模擬登陸案例(非加密)

  引言

  當咱們接到壓測任務的時候,若是被測系統須要登陸的時候,首先須要處理登陸,而後再進行其餘接口壓測。javascript

  Taks屬性使用

  首先看一下官方文檔:html

  Using the @task decorator to declare tasks is a convenience, and usually the best way to do it. However, it’s also possible to define the tasks of a User or TaskSet by setting the tasks attribute (using the @task decorator will actually just populate the tasks attribute).java

The tasks attribute is either a list of Task, or a <Task : int> dict, where Task is either a python callable or a TaskSet class (more on that below). If the task is a normal python function they receive a single argument which is the User instance that is executing the task.python

Here is an example of a User task declared as a normal python function:web

from locust import User, constant

def my_task(l):
    pass

class MyUser(User):
    tasks = [my_task]
    wait_time = constant(1)

  If the tasks attribute is specified as a list, each time a task is to be performed, it will be randomly chosen from the tasks attribute. If however, tasks is a dict - with callables as keys and ints as values - the task that is to be executed will be chosen at random but with the int as ratio. So with a tasks that looks like this:api

{my_task: 3, another_task: 1}

  my_task would be 3 times more likely to be executed than another_task.dom

Internally the above dict will actually be expanded into a list (and the tasks attribute is updated) that looks like this:函數

[my_task, my_task, my_task, another_task]

  and then Python’s random.choice() is used pick tasks from the list.post

 

  上面總結出來就是:若是隨機權重,就用列表,若是指定權重,就用字段。性能

 

  官方案例

  普通寫法

from locust import TaskSet,HttpLocust,between

def login(x):
    x.client.post('/login',{'username':'admin','password':'123456'})

def logout(x):
    x.client.post('/logout',{'username':'admin','password':'123456'})

def home(x):
    x.client.get('/')

def systeminfo(x):
    x.client.get('systeminfo')

class TestLogin(TaskSet):

    tasks = {home: 3,systeminfo: 1}

    def on_start(self):
        login(self)

    def on_stop(self):
        logout(self)

class WebSiteUser(HttpLocust):
    task_set = TestLogin
    wait_time = between(3,6)

 

  實例:

from locust import TaskSet,HttpLocust,between,task

def login(x):
    url = '/api/private/v1/login'
    body = {
        "username": "admin",
        "password": "123456"
    }
    r = x.client.post(url=url, data=body)
    print(r.text)

def logout(x):
    print("退出系統")

def home(x):
    print("進入主頁")


def systeminfo(x):
    print("進入系統管理")

class TestLogin(TaskSet):

    tasks = {home: 1,systeminfo: 1}
    # @task
    def setup(self):
        print("開始...")

    def teardown(self):
        print("結束...")

    def on_start(self):
        print("啓動")
        login(self)
    # @task
    def on_stop(self):
        print("中止")
        logout(self)


class WebSiteUser(HttpLocust):
    def setup(self):
        print('locust setup')
    def teardown(self):
        print('locust teardown')
    task_set = TestLogin
    wait_time = between(1,2)

if __name__ == '__main__':
    import os
    os.system('locust -f locustfile.py --host=http://192.168.1.105:8899 --port=8089 ')

  

  說明:

  Locust類有setup和teardown,TaskSet類有setup、teardown、on_start、on_stop。

  每次啓動locust時運行setup方法,退出時運行teardown方法,locust執行TaskSet時運行TaskSet的setup方法,退出時運行teardown方法,每一個虛擬用戶執行操做時運行on_start方法,退出時執行on_stop方法,運行上面的腳本,執行順序以下:

  執行順序:Locust setup → TaskSet setup → TaskSet on_start → TaskSet tasks → TaskSet on_stop → TaskSet teardown → Locust teardown

 

  運行結果:

[2020-06-22 21:30:10,993] WIN10-804191526/INFO/locust.main: Starting web monitor at http://*:8089
[2020-06-22 21:30:10,993] WIN10-804191526/INFO/locust.main: Starting Locust 0.14.6
[2020-06-22 21:30:20,212] WIN10-804191526/INFO/locust.runners: Hatching and swarming 2 users at the rate 1 users/s (0 users already running)...
[2020-06-22 21:30:20,213] WIN10-804191526/INFO/stdout: locust setup
[2020-06-22 21:30:20,213] WIN10-804191526/INFO/stdout: 
[2020-06-22 21:30:20,213] WIN10-804191526/INFO/stdout: 開始...
[2020-06-22 21:30:20,213] WIN10-804191526/INFO/stdout: 
[2020-06-22 21:30:20,213] WIN10-804191526/INFO/stdout: 啓動
[2020-06-22 21:30:20,213] WIN10-804191526/INFO/stdout: 
[2020-06-22 21:30:21,213] WIN10-804191526/INFO/locust.runners: All locusts hatched: WebSiteUser: 2 (0 already running)
[2020-06-22 21:30:21,213] WIN10-804191526/INFO/stdout: 啓動
[2020-06-22 21:30:21,213] WIN10-804191526/INFO/stdout: 
[2020-06-22 21:30:41,220] WIN10-804191526/INFO/stdout: 
[2020-06-22 21:30:41,220] WIN10-804191526/INFO/stdout: 
[2020-06-22 21:30:41,220] WIN10-804191526/INFO/stdout: 進入主頁
[2020-06-22 21:30:41,220] WIN10-804191526/INFO/stdout: 
[2020-06-22 21:30:42,217] WIN10-804191526/INFO/stdout: 
[2020-06-22 21:30:42,217] WIN10-804191526/INFO/stdout: 
[2020-06-22 21:30:42,217] WIN10-804191526/INFO/stdout: 進入系統管理

 

  界面:

 

  項目實例

  標準寫法

from locust import TaskSet,HttpLocust,between,task



class TestLogin(TaskSet):

    def setup(self):
        print("開始...")

    def teardown(self):
        print("結束...")

    def _login(self):
        url = '/api/private/v1/login'
        body = {
            "username": "admin",
            "password": "123456"
        }
        r = self.client.post(url=url, data=body)
        print(r.text)
        assert "等1" in r.text

    def logout(self):
        print("退出系統")

    @task(1)
    def home(self):
        print("進入主頁")

    @task(1)
    def systeminfo(self):
        print("進入系統管理")

    def on_start(self):
        """ on_start is called when a Locust start before any task is scheduled """
        print("啓動")
        self._login()

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


class WebsiteUser(HttpLocust):
    task_set = TestLogin
    def setup(self):
        print('locust setup')

    def teardown(self):
        print('locust teardown')
    wait_time = between(5,6)


if __name__ == '__main__':
    import os
    os.system('locust -f locustfile_pro.py --host=http://192.168.1.105:8899 --port=8084')

  結果:

[2020-06-22 23:47:09,088] WIN10-804191526/INFO/locust.main: Starting web monitor at http://*:8084
[2020-06-22 23:47:09,088] WIN10-804191526/INFO/locust.main: Starting Locust 0.14.6
[2020-06-22 23:48:05,436] WIN10-804191526/INFO/locust.runners: Hatching and swarming 1 users at the rate 1 users/s (0 users already running)...
[2020-06-22 23:48:05,436] WIN10-804191526/INFO/stdout: locust setup
[2020-06-22 23:48:05,436] WIN10-804191526/INFO/stdout: 
[2020-06-22 23:48:05,436] WIN10-804191526/INFO/locust.runners: All locusts hatched: WebsiteUser: 1 (0 already running)
[2020-06-22 23:48:05,436] WIN10-804191526/INFO/stdout: 開始...
[2020-06-22 23:48:05,436] WIN10-804191526/INFO/stdout: 
[2020-06-22 23:48:05,436] WIN10-804191526/INFO/stdout: 啓動
[2020-06-22 23:48:05,436] WIN10-804191526/INFO/stdout: 
[2020-06-22 23:48:05,513] WIN10-804191526/INFO/stdout: 測試
[2020-06-22 23:48:05,513] WIN10-804191526/INFO/stdout: 
[2020-06-22 23:48:05,513] WIN10-804191526/INFO/stdout: 響應數據是:{"data":{"id":500,"rid":0,"username":"admin","mobile":"12345678","email":"adsfad@qq.com","token":"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE1OTI4NDA4ODUsImV4cCI6MTU5MjkyNzI4NX0.wN9CrRtyENoQgMWKgtaK5toa47fB3qz1nvki9PvqdVg"},"meta":{"msg":"登陸成功","status":200}}
[2020-06-22 23:48:05,513] WIN10-804191526/INFO/stdout: 
[2020-06-22 23:48:05,513] WIN10-804191526/INFO/stdout: 進入主頁
[2020-06-22 23:48:05,513] WIN10-804191526/INFO/stdout: 
[2020-06-22 23:48:11,277] WIN10-804191526/INFO/stdout: 進入系統管理

  界面:

 

   須要注意的是若是是V0.13.0版本以上就不能用min_wait和max_wait了。否則會報錯:

eprecationWarning: Usage of min_wait and max_wait is deprecated since version 0.13. Instead use: wait_time = between(5, 6)
), DeprecationWarning

  腳本解讀:

一、建立TestLogin()類繼承TaskSet類:  用於定義測試業務。
二、建立login()、logout()、home()、systeminfo()方法分別表示一個行爲,訪問http://127.0.0.1:8899。用@task() 裝飾該方法爲一個任務。一、2表示一個Locust實例被挑選執行的權重,數值越大,執行頻率越高。在當前TestLogin()行爲下的兩個方法得執行比例爲1:1
三、WebsiteUser()類: 用於定義模擬用戶。
四、task_set :  指向一個定義了的用戶行爲類。
五、host:   指定被測試應用的URL的地址
六、min_wait :   用戶執行任務之間等待時間的下界,單位:毫秒。
七、max_wait :   用戶執行任務之間等待時間的上界,單位:毫秒。

  備註:默認狀況下,時間是在min_wait和max_wait之間隨機選擇,可是能夠經過將wait_function設置爲任意函數來使用任何用戶定義的時間分佈。

  對於指數分佈的等待時間平均爲1秒:

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

 

 啓動程序

  最新文檔:

  歷史文檔:

在cmd窗口,進入到執行py文件的路徑下,進行start,默認的啓用方法以下:

>>> locust -f 須要執行的腳本.py --host=http://須要用到的url

官網針對啓用,專門的介紹以下:

To run Locust with the above Locust file, if it was named locustfile.py and located in the current working directory, we could run:

locust --host=http://example.com
If the Locust file is located under a subdirectory and/or named different than locustfile.py, specify it using -f:

locust -f locust_files/my_locust_file.py --host=http://example.com
To run Locust distributed across multiple processes we would start a master process by specifying --master:

locust -f locust_files/my_locust_file.py --master --host=http://example.com
and then we would start an arbitrary number of slave processes:

locust -f locust_files/my_locust_file.py --slave --host=http://example.com
If we want to run Locust distributed on multiple machines we would also have to specify the master host when starting the slaves (this is not needed when running Locust distributed on a single machine, since the master host defaults to 127.0.0.1):

locust -f locust_files/my_locust_file.py --slave --master-host=192.168.0.100 --host=http://example.com
You may wish to consume your Locust results via a csv file. In this case, there are two ways to do this.

First, when running the webserver, you can retrieve a csv from localhost:8089/stats/requests/csv and localhost:8089/stats/distribution/csv. Second you can run Locust with a flag which will periodically save the csv file. This is particularly useful if you plan on running Locust in an automated way with the --no-web flag:

locust -f locust_files/my_locust_file.py --csv=foobar --no-web -n10 -c1
You can also customize how frequently this is written if you desire faster (or slower) writing:
import locust.stats locust.stats.CSV_STATS_INTERVAL_SEC = 5 # default is 2 seconds

 

  總結

  關於Locust性能測試模擬案例介紹到此,有興趣的朋友能夠加入測開交流羣進行溝通與學習,一塊兒成長!

相關文章
相關標籤/搜索