0

I am trying to add some variables (e.g. self.boolean_flag) to HttpUser. Which represents the user state. This variable is used in scenario load testing.

According to the documentation, I should use on_start to initialise variables. However, when I use tasks = [TaskSet] like below, The on_start doesn't seem to work.

AttributeError: 'ExampleTask' object has no attribute 'boolean_flag':

class ExampleTask(TaskSet):
    @task
    def example_one(self):
        print(self.boolean_flag)   # AttributeError: 'ExampleTask' object has no attribute 'boolean_flag'
        make_api_request(self, "example_one")


class CustomUser(HttpUser):
    wait_time = between(
        int(os.getenv("LOCUST_MIN_WAIT", 200)), int(os.getenv("LOCUST_MAX_WAIT", 1000))
    )

    def on_start(self):
        self.boolean_flag = False

    tasks = {ExampleTask1 : 10, ExampleTask2: 5 ... }

The bottom works though:

class CustomUser(HttpUser):
    wait_time = between(
        int(os.getenv("LOCUST_MIN_WAIT", 200)), int(os.getenv("LOCUST_MAX_WAIT", 1000))
    )

    def on_start(self):
        self.boolean_flag = False

    @task
    def example_one(self):
        print(self.boolean_flag)
        make_api_request(self, "example_one")

Since I have many different scenarios that reuse many Tasksets, I need to use Tasks = {}..

I also tried subclassing HttpUser and add those variables in init(). But that doesn't work well with tasks={} either.

class CustomUser(HttpUser):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.boolean_flag = False


class AllOfApisCallForLoadAtOneGo(CustomUser):
    wait_time = between(
    int(os.getenv("LOCUST_MIN_WAIT", 200)), int(os.getenv("LOCUST_MAX_WAIT", 1000))
)
    tasks = {ExampleTask1 : 10, ExampleTask2: 5 ... }
(loadtest-GvbsrA_X-py3.8) ➜  loadtest git:(abcd) ✗ locust -f locustfile_scenario.py first -H https://www.somehost.com
[2020-09-02 06:24:27,276] MacBook-Pro.local/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces)
[2020-09-02 06:24:27,286] MacBook-Pro.local/INFO/locust.main: Starting Locust 1.2.3
[2020-09-02 06:24:35,881] MacBook-Pro.local/INFO/locust.runners: Spawning 10 users at the rate 3 users/s (0 users already running)...
[2020-09-02 06:24:35,883] MacBook-Pro.local/ERROR/locust.user.task: You must specify the base host. Either in the host attribute in the User class, or on the command line using the --host option.
Traceback (most recent call last):
  File "/Users/poetry/virtualenvs/loadtest-GvbsrA_X-py3.8/lib/python3.8/site-packages/locust/user/task.py", line 284, in run
    self.execute_next_task()
  File "/Users/poetry/virtualenvs/loadtest-GvbsrA_X-py3.8/lib/python3.8/site-packages/locust/user/task.py", line 309, in execute_next_task
    self.execute_task(self._task_queue.pop(0))
  File "/Users/poetry/virtualenvs/loadtest-GvbsrA_X-py3.8/lib/python3.8/site-packages/locust/user/task.py", line 422, in execute_task
    task(self.user)
  File "/Users/poetry/virtualenvs/loadtest-GvbsrA_X-py3.8/lib/python3.8/site-packages/locust/user/users.py", line 224, in __init__
    raise LocustError(
locust.exception.LocustError: You must specify the base host. Either in the host attribute in the User class, or on the command line using the --host option.
zcahfg2
  • 861
  • 1
  • 12
  • 27

1 Answers1

1

It appears you're assuming that TaskSet inherits from or somehow otherwise is called directly from HttpUser, which isn't the case. But TaskSet does have the user passed into it when it's instantiated. You just have to use self.user. So in your case instead of print(self.boolean_flag) in your task, you'd do print(self.user.boolean_flag).

Solowalker
  • 2,431
  • 8
  • 13