3

I have the next code from locustio documentation:

from locust import HttpLocust, TaskSet, between

def login(l):
    l.client.post("/login", {"username":"ellen_key", "password":"education"})

def logout(l):
    l.client.post("/logout", {"username":"ellen_key", "password":"education"})

def index(l):
    l.client.get("/")

def profile(l):
    l.client.get("/profile")

class UserBehavior(TaskSet):
    tasks = {index: 2, profile: 1}

    def on_start(self):
        login(self)

    def on_stop(self):
        logout(self)

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    wait_time = between(5.0, 9.0)

In the locust logs and locust web (localhost:8089) I see the next tasks

- /login
- /logout
- /
- /profile

But what if I need to have few requests in one task and get measure from full tasks (not 1 request).
What I want to see is:

- login
- logout
- index
- profile

I want to see the names of tasks instead of request url. In Jmeter I can insert few request in one action and get action time (not request).

SergeyMoroz
  • 117
  • 3
  • 11

2 Answers2

8

You can set the name by name attribute for each request, see the example:

def index(l):
  l.client.get("/", name="index")

def profile(l):
  l.client.get("/profile", name="my-profile")
Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49
1

You can achieve this by implementing a custom execute_task() method where you fire the request_success event.

Something like this should work:

import time

class TaskReportingTaskSet(TaskSet):
    def execute_task(self, task, *args, **kwargs):
        start = time.time()
        try:
            super().execute_task(task, *args, **kwargs)
        except:
            events.request_failure.fire(
                request_type="task",  
                name=task.__name__, 
                response_time=(time.time()-start)*1000, 
                response_length=0,
            )
            raise
        else:
            events.request_success.fire(
                request_type="task",  
                name=task.__name__, 
                response_time=(time.time()-start)*1000, 
                response_length=0,
            )

class UserBehavior(TaskReportingTaskSet):
    tasks = ...

With the above code the run time of all the tasks will be reported if the TaskSet inherit from TaskReportingTaskSet. request_success events would have to be fired separately if you want to include on_start and on_stop.

If you don't want HTTP requests to be reported, you can simply use an HTTP client that isn't one of the built in Locust HTTP clients. For example you could use python requests directly:

import requests

def index(l):
    requests.get("/")
heyman
  • 4,845
  • 3
  • 26
  • 19