Dana Yeger
Dana Yeger

Reputation: 711

Locust on_start() method not executing requests

I am using Locust for performance testing, and I have a task that performs an API call in the on_start method. The on_start method prints the "started" message, but the request inside the try block does not execute. The same request works fine when executed outside of Locust (e.g., in a standalone Python script).

Here is my Locust script:

import gevent
from locust import HttpUser, task, between
from locust.env import Environment
from locust.stats import stats_printer, stats_history


class QuickstartUser(HttpUser):
    wait_time = between(1, 5)
    host = 'https://MY_HOST'

    @task
    def hello_world(self):
        self.client.get("/hello")
        self.client.get("/world")

    def on_start(self):
        print('started')
        try:
            res = self.client.post(
                "'https://MY_HOST'/api/auth/logon",
                json={"username": "adminUser1", "password": "12345678"},
                headers={'Content-Type': 'application/json'},
                verify=False)
            print(res.text)
        except Exception as ex:
            print(ex)


# Setup Environment and Runner
env = Environment(user_classes=[QuickstartUser])
runner = env.create_local_runner()

# Start a WebUI instance
web_ui = env.create_web_ui("127.0.0.1", 8089)

# Execute init event handlers (only really needed if you have registered any)
env.events.init.fire(environment=env, runner=runner, web_ui=web_ui)

# Start a greenlet that periodically outputs the current stats
gevent.spawn(stats_printer(env.stats))

# Start a greenlet that saves current stats to history
gevent.spawn(stats_history, env.runner)

# Start the test
runner.start(user_count=1

What I have tried: Verified the API request works correctly outside Locust using requests in a standalone script. Checked the URL, payload, and headers to ensure they are correct. I removed on_start method and try the request from @task

Observations: The print('started') message is displayed. No output from the print(res.text) line, indicating the request isn't being sent or executed. No exceptions are caught in the except block.

Questions: Why isn't the request in on_start executing? How can I ensure the on_start method runs the request properly within Locust? Any help or insights would be greatly appreciated!

Upvotes: 0

Views: 56

Answers (0)

Related Questions