[英]How can I translate this requests.post into a locust request?
I have a template for a Python function that calls an API and returns data.我有一个 Python function 的模板,它调用 API 并返回数据。 I'd like to run load testing against that API using locust.我想使用 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
How could I translate this into a locust class?我怎么能把它翻译成蝗虫 class?
I do it this way:我这样做:
# 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")
In this example, this code runs in a separate function called from the UserBehavior class, so connection_object is "self" if you are doing this inside the task in the class.在此示例中,此代码在从 UserBehavior class 调用的单独 function 中运行,因此如果您在 class 的任务中执行此操作,则 connection_object 为“self”。
In your example though, change但是,在您的示例中,请更改
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)
to至
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)
I am sure you have seen this , which shows "get", but not "post".我相信你已经看过这个,它显示的是“get”,而不是“post”。 If you are unfamiliar with the requests library, it can get confusing.如果您不熟悉 requests 库,可能会让人感到困惑。 Hope that helps!希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.