Locust tasks that follow redirects

Viewed 3440

I'm load testing a local API that will redirect a user based on a few conditions. Locust is not redirecting the simulated users hitting the end points and I know this because the app logs all redirects. If I manually hit the end points using curl, I can see the status is 302 and the Location header is set.

According to the embedded clients.HttpSession.request object, the allow_redirects option is set to True by default.

Any ideas?

2 Answers

We use redirection in our locust test, especially during the login phase. The redirects are handled for us without a hitch. Print the status_code of the response that you get back. Is it 200, 3xx or something worse?

Another suggestion: Don't throw your entire testing workflow into the locust file. That makes it too difficult to debug problems. Instead, create a standalone python script that uses the python requests library directly to simulate your workflow. Iron out any kinks, like redirection problems, in that simple, non-locust test script. Once you have that working, extract what you did into a file or class and have the locust task use the class.

Here is an example of what I mean. FooApplication does the real work. He is consumed by the locust file and a simple test script.

foo_app.py

class FooApplication():
    def __init__(self, client):
        self.client = client
        self.is_logged_in = False

    def login(self):
        self.client.cookies.clear()
        self.is_logged_in = False
        name = '/login'
        response = self.client.post('/login', {
            'user': 'testuser',
            'password': '12345'
        }, allow_redirects=True, name=name)
        if not response.ok:
            self.log_failure('Login failed', name, response)

    def load_foo(self):
        name = '/foo'
        response = self.client.get('/foo', name=name)
        if not response.ok:
            self.log_failure('Foo request failed ', name, response)
    
    def log_failure(self, message, name, response):
        pass # add some logging

foo_test_client.py

# Use this test file to iron out kinks in your request workflow
import requests
from locust.clients import HttpSession
from foo_app import FooApplication

client = HttpSession('http://dev.foo.com')
app = FooApplication(client)
app.login()
app.load_foo()

locustfile.py

from foo_app import FooApplication
class FooTaskSet(TaskSet):
    def on_start(self):
        self.foo = FooApplication(self.client)

    @task(1)
    def login(self):
        if not self.foo.is_logged_in:
            self.foo.login()

    @task(5) # 5x more likely to load a foo vs logging in again
    def load_foo(self):
        if self.foo.is_logged_in:
            self.load_foo()
        else:
            self.login()

Since Locust uses the Requests HTTP library for Python, you might find your answer there. The Response object can be used to evaluate if a redirect has happened and what the history of redirects contains.

is_redirect:

True if this Response is a well-formed HTTP redirect that could have been processed automatically (by Session.resolve_redirects).

There might be an indication that the redirect is not well-formed.

Related