Locust performance-zero-based entry series (9)-SequentialTaskSet application

The first part:
How to ensure that multiple users (threads) follow a certain sequence when performing multiple test tasks under traffic pressure, rather than a random phenomenon. This requirement is also achievable in Locust, taskset can be inherited from TaskSet's subclass SequentialTaskSet. Take an example to illustrate this usage:

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")

In the above code example, the SequeneOfTasks class is embedded in the MyUser class. From the meaning of SequentialTaskSet setting, the task execution order of this example should be as follows:

  • first_task

  • function_task

  • second_task

  • third_task

Executed by the script, the console output is as follows:

 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
...

Part 2:
How to configure the code block that the virtual user will execute at the beginning and end? We know that in the previous tool LoadRunner, lr_start and lr_end can be set. Then it is also possible in Locust. There are two ways to explain here, one is the User class level, the other is the module level. These two methods will be described in detail next.

1> User class level

First on the code:

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")

As can be seen from the above code, all virtual users will execute the on_start function during setup, and will execute the on_stop function during teardown. From the running results (5 virtual users running scenarios for 20 seconds), the console output is as follows:

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 level.

What if there are multiple classes in the module/locust file? You can reduce the amount of code by setting on_start and on_stop at the module level. The above code is also rewritten as the original. The rewritten version is as follows, but please note ⚠️: The on_start and on_stop will only be executed once, no matter how many virtual users there are, so please choose a different mode according to your needs. Where the label in this pattern

@events.test_stop.add_listener is the key.

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 performance-zero-based entry series (9)-SequentialTaskSet application

Guess you like

Origin blog.51cto.com/13734261/2571136