Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In locust How to get a response from one task and pass it to other task

Tags:

python

locust

I have started using Locust to do performance test. I want to fire two post request to two different end points. But the second post request needs response of the first request. How to do this in convenient way. I have tried like below but not working.

from locust import HttpLocust, TaskSet, task

class GetDeliveryDateTasks(TaskSet):

    request_list = []

    @task
    def get_estimated_delivery_date(self):
        self.client.headers['Content-Type'] = "application/json"
        response = self.client.post("/api/v1/estimated-delivery-date/", json=
        {
            "xx": "yy"

        }
          )
        json_response_dict = response.json()
        request_id = json_response_dict['requestId']
        self.request_list.append(request_id)


    @task
    def store_estimated_delivery_date(self):
        self.client.headers['Content-Type'] = "application/json"
        response = self.client.post("/api/v1/estimated-delivery-date/" + str(self.request_list.pop(0)) + "/assign-order?orderId=1")


class EDDApiUser(HttpLocust):
    task_set = GetDeliveryDateTasks
    min_wait = 1000
    max_wait = 1000
    host = "http://localhost:8080"
like image 353
Bidyut Avatar asked Mar 14 '16 15:03

Bidyut


People also ask

What is HttpUser in Locust?

HttpUser is the most commonly used User . It adds a client attribute which is used to make HTTP requests. from locust import HttpUser, task, between class MyUser(HttpUser): wait_time = between(5, 15) @task(4) def index(self): self. get("/") @task(1) def about(self): self.

What is Wait_time in Locust?

The wait_time attribute It's used to determine for how long a simulated user will wait between executing tasks. Locust comes with a few built in functions that return a few common wait_time methods. The most common one is between .

How do you put RPS in a locust?

The code your Locust users runs needs to be written in such a way that each user is only making one request. Then in your Locust User class, you set wait_time = constant(1) or wait_time = constant_pacing(1) , whichever behavior you want. Edited my answer to address your RPS concern.


1 Answers

You can call on_start(self) function which prepare data for you before passing to the task list. See example below:

from locust import HttpLocust, TaskSet, task

class GetDeliveryDateTasks(TaskSet):

    request_list = []

    def get_estimated_delivery_date(self):
        self.client.headers['Content-Type'] = "application/json"
        response = self.client.post("/api/v1/estimated-delivery-date/", json=
        {
            "xx": "yy"

        }
          )
        json_response_dict = response.json()
        request_id = json_response_dict['requestId']
        self.request_list.append(request_id)

    def on_start(self):
        self.get_estimated_delivery_date()


    @task
    def store_estimated_delivery_date(self):
        self.client.headers['Content-Type'] = "application/json"
        response = self.client.post("/api/v1/estimated-delivery-date/" + str(self.request_list.pop(0)) + "/assign-order?orderId=1")


class EDDApiUser(HttpLocust):
    task_set = GetDeliveryDateTasks
    min_wait = 1000
    max_wait = 1000
    host = "http://localhost:8080"
like image 86
Mesut GUNES Avatar answered Sep 20 '22 14:09

Mesut GUNES