When using locust for performance testing in Python, method initialization and weight ratio setting

1. When using locust for performance testing, the initialization method is introduced

When using locust for interface performance testing, there will often be interface B that requires interface A to pass parameters. In this case, you need to execute interface A to get the response value during initialization, and then pass it to interface B.
Refer to the following example. The on_start method is the initialization method. The One and Two methods are performance test methods. In actual execution, on_start will be executed first, One, Two will be executed in the middle, and on_stop will be executed at the end of the
test. In order to test the output content, the method only writes the document operation, and the specific data can be viewed in the Report in the same directory. .csv file

#coding=utf-8
from locust import TaskSet, task,HttpUser

class MyTaskSet(TaskSet):
    def WriteCSV(self,Str):
        FilePath = "Report.csv"
        with open(FilePath,"a") as FileOBJ:
            FileData = FileOBJ.write(Str+"\n")

    def on_start(self):
        OnStartStr = "TaskSet on_start"
        self.WriteCSV(OnStartStr)
    @task
    def one(self):
        OneStr = "任务1"
        self.WriteCSV(OneStr)
    @task
    def two(self):
        TwoStr = "任务2"
        self.WriteCSV(TwoStr)
    def on_stop(self):
        OnStop = "TaskSet on_stop"
        self.WriteCSV(OnStop)

class WebsitUser(HttpUser):
    weight = 1
    tasks = [MyTaskSet]
    host = "http://www.baidu.com"
    min_wait = 5000
    max_wait = 6000
    stop_timeout = 10



'''
点击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


TaskSet on_start  # 每一次开始一个任务时执行
TaskSet tasks…
TaskSet on_stop   # 点击页面stop时,当前所有在执行中的TaskSet执行

'''

Second, when using locust for performance testing, the weight is introduced

  1. The way to use the task tag is to
    take the above code as an example. The weight of the One method is 80%, and the weight of the Two method is 20%. Set the corresponding proportions in the decorator of the method. Finally, check the output result of the document. Consistent proportion
#coding=utf-8
from locust import TaskSet, task,HttpUser

class MyTaskSet(TaskSet):
    def WriteCSV(self,Str):
        FilePath = "Report.csv"
        with open(FilePath,"a") as FileOBJ:
            FileData = FileOBJ.write(Str+"\n")

    def on_start(self):
        OnStartStr = "TaskSet on_start"
        self.WriteCSV(OnStartStr)
    @task(4)
    def one(self):
        OneStr = "任务1"
        self.WriteCSV(OneStr)
    @task(1)
    def two(self):
        TwoStr = "任务2"
        self.WriteCSV(TwoStr)
    def on_stop(self):
        OnStop = "TaskSet on_stop"
        self.WriteCSV(OnStop)

class WebsitUser(HttpUser):
    weight = 1
    tasks = [MyTaskSet]
    host = "http://www.baidu.com"
    min_wait = 5000
    max_wait = 6000
    stop_timeout = 10

Insert picture description here
2. Set the weight in the way of tasks dictionary The
code is as follows, and finally check the report file, the setting items are also effective

#coding=utf-8
from locust import TaskSet, task,HttpUser

class MyTaskSet(TaskSet):
    def WriteCSV(self,Str):
        FilePath = "Report.csv"
        with open(FilePath,"a") as FileOBJ:
            FileData = FileOBJ.write(Str+"\n")


    def on_start(self):
        OnStartStr = "TaskSet on_start"
        self.WriteCSV(OnStartStr)
    @task
    def one(self):
        OneStr = "任务1"
        self.WriteCSV(OneStr)
    @task
    def two(self):
        TwoStr = "任务2"
        self.WriteCSV(TwoStr)
    def on_stop(self):
        OnStop = "TaskSet on_stop"
        self.WriteCSV(OnStop)
    tasks = {
    
    one:4,two:1}


class WebsitUser(HttpUser):
    weight = 1
    tasks = [MyTaskSet]
    host = "http://www.baidu.com"
    min_wait = 5000
    max_wait = 6000
    stop_timeout = 10

Insert picture description here

Guess you like

Origin blog.csdn.net/zhuan_long/article/details/111219368