在蝗虫如何从一个任务获得响应并将其传递给其他任务

时间:2016-03-14 15:17:42

标签: python locust

我已经开始使用Locust进行性能测试。 我想向两个不同的终点发出两个帖子请求。但是第二个帖子请求需要第一个请求的响应。如何以方便的方式做到这一点。我尝试过以下但没有工作。

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"

1 个答案:

答案 0 :(得分:10)

您可以调用on_start(self)函数为您准备数据,然后再转到task列表。见下面的例子:

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"
相关问题