Locust性能-零基础入门系列(9)-SequentialTaskSet应用

第一部分:
在流量压力情况下,怎么保证多用户(线程)在执行多个测试任务时遵循一定的顺序,而不是一种随机的现象。这种需求在Locust中也是可以实现的,可以将taskset继承TaskSet的子类SequentialTaskSet。以一个例子来说明这种用法:

from locust import User,SequentialTaskSet,task,between

def function_task(l):
    print("This is the function task")

class MyUser(User):
    wait_time = between(5,9)
    @task
    class SequenceOfTasks(SequentialTaskSet):
        @task
        def first_task(self):
            print("this is the first task")

        tasks = [function_task]

        @task
        def second_task(self):
            print("this is the second task")

        @task
        def third_task(self):
            print("this is the third task")

以上代码例子中,SequeneOfTasks类是嵌入到MyUser类中的。从SequentialTaskSet设定的意义来看,本例子的任务执行顺序应该如下:

  • first_task

  • function_task

  • second_task

  • third_task

通过脚本执行,其控制台输出如下:

 All users hatched: MyUser: 1 (0 already running)
this is the first task
This is the function task
this is the second task
this is the third task
this is the first task
This is the function task
this is the second task
this is the third task
this is the first task
This is the function task
...

第二部分:
如何配置虚拟用户开始和结束都会执行的代码块呢?我们知道在之前的工具LoadRunner中,可以设置lr_start和lr_end。那么在Locust中也是可以的。这里会讲解两种方式,一种是User类级别的,一种是module级别的。接下来会详细介绍这两种方式。

1> User类级别

先上代码:

from locust import User,task,between

class MyUser(User):
    wait_time = between(5,8)

    def on_start(self):
        print("A test will start...")

    def on_stop(self):
        print("A test is ending...")

    @task
    def task_1(self):
        print("it is task1")

    @task
    def task_2(self):
        print("it is task2")

从以上代码可以看出,所有的虚拟用户在setup时都会执行on_start 函数,在teardown时都会执行on_stop函数。我们从运行结果来看(5虚拟用户运行场景20秒),其控制台的输出如下:

Hatching and swarming 5 users at the rate 5 users/s (0 users already running)...
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

A test will start...
it is task1
A test will start...
it is task1
A test will start...
it is task2
A test will start...
it is task2
[2020-11-20 08:52:18,681] jasondeMacBook-Pro.local/INFO/locust.runners: All users hatched: MyUser: 5 (0 already running)
A test will start...
it is task2
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

it is task1
it is task2
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

it is task1
it is task2
it is task2
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

it is task1
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

it is task2
it is task2
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

it is task1
it is task2
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

it is task2
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

it is task2
it is task2
it is task1
[2020-11-20 08:52:37,696] jasondeMacBook-Pro.local/INFO/locust.main: Time limit reached. Stopping Locust.
A test is ending...
A test is ending...
A test is ending...
A test is ending...
A test is ending...
[2020-11-20 08:52:37,697] jasondeMacBook-Pro.local/INFO/locust.main: Running teardowns...
[2020-11-20 08:52:37,698] jasondeMacBook-Pro.local/INFO/locust.main: Shutting down (exit code 0), bye.
[2020-11-20 08:52:37,698] jasondeMacBook-Pro.local/INFO/locust.main: Cleaning up runner...
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                         0     0(0.00%)       0       0       0  |       0    0.00    0.00

Percentage of the requests completed within given times
 Type                 Name                                                           # reqs    50%    66%    75%    80%    90%    95%    98%    99%  99.9% 99.99%   100%
------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------

2> Module级别。

如果在module/locust file中有多个类呢?可以通过设置module级别的on_start 和on_stop来减少代码量。还以上面代码为原本进行改写,改写之后的版本如下,但是请注意⚠️:这里的on_start和on_stop只会执行1次,不管有多少的虚拟用户,所以请根据自己的需求来选择不同的模式。其中这种模式中的标签

@events.test_stop.add_listener是关键。

from locust import events,User,task,between

@events.test_start.add_listener
def on_start(**kwargs):
    print("A test will start...")

@events.test_stop.add_listener
def on_stop(**kwargs):
    print("A test is ending...")

class MyUser(User):
    wait_time = between(5,8)
    @task
    def task_1(self):
        print("it is task1")

    @task
    def task_2(self):
        print("it is task2")

Locust性能-零基础入门系列(9)-SequentialTaskSet应用

猜你喜欢

转载自blog.51cto.com/13734261/2571136