Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locust. Read --host value from python code

Tags:

python

locust

locust --no-web --client=1 --hatch-rate=1 --num-request=2 --host= http://localhost

I want to read --host value provided in cmd line in my HTTPLocust class. I am aware I can use host attribute for direct assignment but I do not want it. I want to read the value from cmd line with in HTTPLocust class. I am building custom logs and want to pass that value to the logs. I tried HTTPLocust.host but that returns none.

Also I want to read --web-port from python code.

like image 455
PKS Avatar asked Aug 03 '26 02:08

PKS


1 Answers

New answer

There is a much more straightforward solution than my initial one below. Each TaskSet has a locust property that links back to their parent Locust locustinstance, so something like this will do exactly what you need:

class UserBehaviour(TaskSet):
    def __init__(self, parent):
        super().__init__(parent)
        print(self.locust.host)

Old answer

Looking at the code for HttpSession, it seems base_url is what you want.

So something like this should give you the current host, either default or specified on the command line:

class HostGetter(locust.TaskSet):
    @locust.Task()
    def get_host(self):
        print(self.client.base_url)
like image 165
shtrom Avatar answered Aug 05 '26 14:08

shtrom