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"
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.
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 .
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With