Locust 基准测试 post API headers

Locust benchmark test with post API with headers

当我尝试触发 HTTP.get 基准测试时,这段代码非常适合我:

    from locust import HttpLocust, TaskSet, task

    class ReferenceWsTest(TaskSet):
        @task(1)
        def index(self):
            response = self.client.request(method="GET",
                url="/v1/reference/charges/config?chargeTypes=SUBTOWIN",

                headers = {
                    "country": 'DEU'
                })
            print ("Preview; Response status code:", 
    response.status_code)

    class WebsiteUser(HttpLocust):
        task_set = ReferenceWsTest
        min_wait = 5000
        max_wait = 9000

虽然,当我尝试 post API 和 headers 时,此代码不起作用:

    from locust import HttpLocust, TaskSet, task
    import uuid

    datas = """
    {"eventCode":"INITPAYOUT","skipCreditValidation":false,"lotId":50017353,"party":{"id":"163","type":"VDR","attributes":{"zip":"88898","country":"ESP","facilityId":"5101","city":"Madrid","mailingCity":"Madrid","operatingCntryCd":"ESP","vatId":"ESB86979853","mailingZip":"88898","paymentFreqValue":"1","bulkPaymentFlag":"false","phone":"34788787888","addrLine2":null,"addrLine1":"Luna","mailingCountry":"ESP","paymentFreq":"MNT","defBankAccountNum":"4UYDyn5Pt8v88joT4plyJQ==","mailingAddrLine1":"Luna","name":"G2.5 Before Pick up  Spain Vendor","invRcptFlg":"false","mailingAddrLine2":null,"mailingState":null,"state":null,"paymentTerm":"0D","email":"mamatha.bompelli@gmail.com"},"enrichmentNotRequired":false},"facilityId":5101,"payment":{"bills":[{"billNumber":"59001713","billId":62648,"eventCategoryCode":"PURCHASE","dueAmount":"363.00","sequence":1,"currency":"EUR","hasChargeProduct":false,"exportType":"DMSTC"}],"paymentAmount":"363.00","paymentDate":"2018-09-01T15:19:44.980Z","paymentDetails":[{"sequence":1,"paymentMethod":"BNKTR","paymentMethodId":"1234567890","txnReferenceNum":null,"attributes":{"bankAccountName":"G2.5 Before Pick up  Spain Vendor","bankRoutingNumber":"/VwoXekvy23LSGEN7/OUqQ==","bankIBAN":"+Aa8/A7NVvZnO5CSwcGwkUCp+3k/yb0ATcF8rZkoFUA=","bankName":"Yard Portal QA4 BAnk","bankCountry":"ESP"},"amount":"363.00"}]},"correction":false,"referenceBills":[],"copartOwnedLot":false,"accumulation":false,"forcePay":false,"processCurrentFlowCharges":false,"bulkProcess":false,"nothingToProcess":false,"requestLogId":169853}
    """

    header = {"country": "ESP","source": "yard","correlationid": "lalalalal","flowId": 1,"Content-type": "application/json"}


    class ReferenceWsTest(TaskSet):
        @task(1)
        def index(self):
            header["correlationid"] = my_random_string(10)
            response = self.client.post("/v1/events/", datas, headers = header)
            print(header)
            print(datas)
            print ("Preview; Response status code:", response.status_code)

    class WebsiteUser(HttpLocust):
        task_set = ReferenceWsTest
        min_wait = 5000
        max_wait = 9000

    def my_random_string(string_length=10):
        """Returns a random string of length string_length."""
        random = str(uuid.uuid4()) # Convert UUID format to a Python string.
        random = random.upper() # Make all characters uppercase.
        random = random.replace("-","") # Remove the UUID '-'.
        return random[0:string_length] # Return the random string.

当触发基准测试时,我得到如下响应:

        [2018-08-20 15:15:40,449] localhost/INFO/stdout: ('Preview; Response status code:', 0)

您将 header 创建为全局任务,因此每个任务都共享相同的 correlationid。您需要将 headers 放入您的任务中,以便它们在每个实例中都是唯一的。