当前位置:网站首页>Performance test ---locust's on_ Start and on_ Stop method

Performance test ---locust's on_ Start and on_ Stop method

2022-06-21 17:43:00 The evening wind blows

Tips : When the article is finished , Directories can be generated automatically , How to generate it, please refer to the help document on the right


Preface

user (TaskSets) Can declare a on_start Method or on_stop Method . on_start Call its method when it starts running , on_stop The method that calls it when it stops running . about TaskSet on_start Method simulates that the user starts executing when ,TaskSet on_stop Is called when the user stops execution

One 、 on_start

analysis :
Every time you start a task , Execute first on_start Method , Only once
Then carry out specific tasks

import time
from locust import HttpUser, task, between

class StartUser(HttpUser):
    wait_time = between(1, 5)

    @task
    def hello_world(self):
        self.client.get("/hello")
        self.client.get("/world")

    @task(3)
    def view_items(self):
        for item_id in range(10):
            self.client.get(f"/item?id={
      item_id}", name="/item")
            time.sleep(1)

    def on_start(self):
        self.client.post("/api/v1/login",{
    "username": "admin","password": "admin123"})
    
    def on_stop(self):
        time.sleep(5)
        
class WebsiteUser(HttpUser):
    tasks = [StartUser]
    host = "http://192.168.44.5:4444"

Two 、 on_stop

analysis :
Every time you start a task , Execute first on_start Method , Only once
Then carry out specific tasks
Go after the task on_stop

import time
from locust import HttpUser, task, between

class StartUser(HttpUser):
    wait_time = between(1, 5)

    @task
    def hello_world(self):
        self.client.get("/hello")
        self.client.get("/world")

    @task(3)
    def view_items(self):
        for item_id in range(10):
            self.client.get(f"/item?id={
      item_id}", name="/item")
            time.sleep(1)

    def on_start(self):
        self.client.post("/api/v1/login",{
    "username": "admin","password": "admin123"})
    
    def on_stop(self):
        time.sleep(5)
        
class WebsiteUser(HttpUser):
    tasks = [StartUser]
    host = "http://192.168.44.5:4444"

3、 ... and 、 Operation rules

Once the test starts , Each virtual user (Locust example ) The operation logic of will follow the following rules :
Execute first WebsiteTasks Medium on_start( Only once ), As initialization ;

from WebsiteTasks Middle random selection ( If you define the weight relationship between tasks , Then it is randomly selected according to the weight relationship ) A task execution ;

according to Locust Class min_wait and max_wait The interval range defined ( If TaskSet It's also defined in the class min_wait perhaps max_wait, With TaskSet Priority in the world ), Pick a random value in the time range , Sleep and wait ;

repeat 2~3 step , Until the test task terminates

原网站

版权声明
本文为[The evening wind blows]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211550322075.html