Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Hocust中,如何从一个任务获得响应并将其传递给另一个任务_Python_Locust - Fatal编程技术网

Python 在Hocust中,如何从一个任务获得响应并将其传递给另一个任务

Python 在Hocust中,如何从一个任务获得响应并将其传递给另一个任务,python,locust,Python,Locust,我已经开始使用蝗虫做性能测试。 我想将两个post请求发送到两个不同的端点。但是第二个post请求需要第一个请求的响应。如何以方便的方式进行此操作。我试过像下面这样,但没有工作 from locust import HttpLocust, TaskSet, task class GetDeliveryDateTasks(TaskSet): request_list = [] @task def get_estimated_delivery_date(self):

我已经开始使用蝗虫做性能测试。 我想将两个post请求发送到两个不同的端点。但是第二个post请求需要第一个请求的响应。如何以方便的方式进行此操作。我试过像下面这样,但没有工作

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"

您可以在启动时调用
(self)
函数,在传递到
任务
列表之前为您准备数据。见下例:

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"