5

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.

PKS
  • 63
  • 1
  • 6

5 Answers5

5

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)
shtrom
  • 71
  • 1
  • 6
  • Looks like the locust attribute has been removed from TaskSet in locust version 1.0 and later. โ€“ TimSC Apr 03 '23 at 19:42
1

You can access the host variable by an instance of User()class. See the example below:

from locust import HttpLocust, TaskSet, task
import random, requests, time, os, inspect, json, sys


class UserBehaviour(TaskSet):
    @task(1)
    def test1(self):
        user = User()  
        print(user.host)
        self.client.get("/v3/User/ListOfCoupon/")

class User(HttpLocust):
    task_set = UserBehaviour
    min_wait = 1000
    max_wait = 3000

see the log:

~/P/m/p/general (master โšกโ†‘) locust -f app_couponlist.py --host=http://www.google.com
[2017-09-19 14:33:13,020] Mesuts-MacBook.local/INFO/locust.main: Starting web monitor at *:8089
[2017-09-19 14:33:13,021] Mesuts-MacBook.local/INFO/locust.main: Starting Locust 0.8a3
[2017-09-19 14:33:22,281] Mesuts-MacBook.local/INFO/locust.runners: Hatching and swarming 5 clients at the rate 1 clients/s...
[2017-09-19 14:33:22,282] Mesuts-MacBook.local/INFO/stdout: http://www.google.com
[2017-09-19 14:33:22,282] Mesuts-MacBook.local/INFO/stdout:
[2017-09-19 14:33:23,285] Mesuts-MacBook.local/INFO/stdout: http://www.google.com
[2017-09-19 14:33:23,285] Mesuts-MacBook.local/INFO/stdout:
[2017-09-19 14:33:24,226] Mesuts-MacBook.local/INFO/stdout: http://www.google.com
Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49
1

This is my code:

import time,csv,argparse

class MySQLLocust(Locust):
    parser = argparse.ArgumentParser()
    parser.add_argument('--host', '--host')
    args, unknown = parser.parse_known_args()
    print("Host = " + args.host)

Now when I give:

locust -f mysql_locust.py --host=myhost-vm-101 --no-web --clients=2 --hatch-rate=10 --run-time=5m

I get the print statement result as expected:

Host = myhost-vm-101

0

You can access it via sys.argv

Or parse options via argparse

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-H', '--host')
args, unknown = parser.parse_known_args()

print str(args.host)
TomVW
  • 1,510
  • 13
  • 26
0

For locust v2.x this works for me:

class MyTaskSet(SequentialTaskSet):

    def __init__(self, parent):

        super().__init__(parent)

        user = parent
        while isinstance(parent, TaskSet):
            user = user.parent
        assert (isinstance(parent, User))

        self.host = user.environment.host
TimSC
  • 1,459
  • 16
  • 20