Locust performance-zero-based entry series (10)-event application

This article continues to talk about the application of locust on events.

Usage of init event

The init event is triggered at the beginning of each Locust process. This event mechanism is especially useful for distributed testing requirements. We can set init events for each worker process. For example, when the test is triggered, we need to pull global variables and other state values ​​for each worker process. It is more appropriate to use init events for this kind of requirement. The following is adapted from the official case.

from locust import events,between,User,task
from locust.runners import MasterRunner

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

    @events.init.add_listener
    def on_locust_init(environment,**kwargs):
        if isinstance(environment.runner,MasterRunner):
            print("I'm on master node")
        else:
            print("I'm on a worker or standalone node")

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

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

After executing the above code, the console output information is as follows, and we can find that the locust process we are executing locally is a worker/standalone node, which is correct. Because it is definitely not the master node at first, the reasons are:

1) We are not a distributed test here, you can understand it as a single machine test, so it is not a master node.

2) The Master node will not simulate any virtual users. The master node is mainly used to display the real-time data display at the beginning of the test, and to undertake the web interface of locust.

The subsequent series of courses will also explain the content of distributed testing in detail, so stay tuned.

[2020-11-26 09:06:41,135] jasondeMacBook-Pro.local/INFO/locust.main: Starting web interface at http://:8089
I'm on a worker or standalone node
[2020-11-26 09:06:41,144] jasondeMacBook-Pro.local/INFO/locust.main: Starting Locust 1.1.1
[2020-11-26 09:06:47,454] jasondeMacBook-Pro.local/INFO/locust.runners: Hatching and swarming 1 users at the rate 1 users/s (0 users already running)...
[2020-11-26 09:06:47,454] jasondeMacBook-Pro.local/INFO/locust.runners: All users hatched: MyUser: 1 (0 already running)
it is task1
it is task2
it is task1
it is task1
it is task2
it is task1
...

Locust performance-zero-based entry series (10)-event application

Guess you like

Origin blog.51cto.com/13734261/2571559