繁体   English   中英

如何将此 requests.post 转换为 locust 请求?

[英]How can I translate this requests.post into a locust request?

我有一个 Python function 的模板,它调用 API 并返回数据。 我想使用 locust 对 API 运行负载测试。

import requests

proxies = {"http" : None,
           "https" : None}

verify = "/data/certs/abc123.crt"


def call_api(par1, par2, par3):


    r = requests.post(url = 'https://ABCD123.XYZ.QWERTY:9010/public/api/v1/ABC_TEST/query',
                      json = {"par1" : par1, "par2" : par2, "par3" : par3}, verify = verify, proxies = proxies)
return r

我怎么能把它翻译成蝗虫 class?

我这样做:

# prepare the headers to send to the remote host
headers = {"key":"value",
           "key":"value"}

# prepare the data for the POST body
data = {"key":"value",
        "key":"value"}```

with connection_object.client.post(url, headers=headers, 
    json=data, name=task_name, catch_response=True) as response:
    # convert the response to JSON
    msg = json.loads(response.content.decode('utf-8'))
    if response.status_code == 200:
        # we got a 200 OK
        response.success()
    else:
        response.failure("failure text")

在此示例中,此代码在从 UserBehavior class 调用的单独 function 中运行,因此如果您在 class 的任务中执行此操作,则 connection_object 为“self”。

但是,在您的示例中,请更改

r = requests.post(url = 'https://ABCD123.XYZ.QWERTY:9010/public/api/v1/ABC_TEST/query',
                      json = {"par1" : par1, "par2" : par2, "par3" : par3}, verify = verify, proxies = proxies)


class UserBehavior(SequentialTaskSet):
    @task()
    def task1(self):
        r = self.client.post('https://ABCD123.XYZ.QWERTY:9010/public/api/v1/ABC_TEST/query', 
    json = {"par1" : par1, "par2" : par2, "par3" : par3}, 
    verify = verify, proxies = proxies)

我相信你已经看过这个,它显示的是“get”,而不是“post”。 如果您不熟悉 requests 库,可能会让人感到困惑。 希望有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM