S
S
Sergey2020-01-29 16:50:54
Python
Sergey, 2020-01-29 16:50:54

How to get worker ID in Locust (python)?

Good day! There was a need to write a couple of tests for loading the system in Locust, but suddenly there was a problem with the limitations of this very system.

The bottom line is this: I need to bind a specific Locust worker (flow) to a specific account. For example, thread #1 - user1, thread #2 - user2. That is, so that all actions in a single thread are performed strictly by one user. But neither in the documentation, nor anywhere else did I find how to get the ID of a particular thread executing the code, or even the number of threads at least (except from the command line via sys.argv)

Is there any kind of workaround?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SvinkaBacilka, 2020-01-31
@SvinkaBacilka

I solved this problem in the following way. I created an additional class with the attributes I needed (ID, for example) and inherited it with a class that describes the behavior of the user's locust. After that, when creating a user, I assign him a unique ID, which is generated by a third-party function. It looks something like this:

class TestAttributes:
    def __init__(self):
        self.id = None


class UserBehavior(TaskSequence, TestAttributes):
    def on_start(self):
        self.id = generate_id()

I use this identifier in the logs and in the request name.
self.client.post(url=my_url, data=my_body, headers=my_headers, name='Test request: %s' % self.id)

There is an even easier option. Just add an ID attribute to the class that will describe the behavior of the user.
class UserBehavior(TaskSequence):
    user_id = generate_id()

And in the following methods, access this ID in this way:
self.user_id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question