我们正在尝试实现基于用户场景的负载测试,该测试基本上是在一个URL中创建一个订单,然后我们必须在下一个URL中传递订单ID才能获得该订单的状态。
我们正在使用蝗虫顺序任务。因为我们希望它像第一个请求->第二个请求->第三个请求一样顺序运行。我们已经获得了预期的响应数据,但是我们无法将该变量数据发送到第三个任务以显示订单状态。
import json
from locust import HttpLocust, TaskSet, task, TaskSequence, seq_task
class MyTaskSequence(TaskSequence):
response_data = ""
@seq_task(1)
def index(self):
print("--- First Task")
response = self.client.get("/order/testing-06a5c/")
print(response.status_code)
@seq_task(2)
def get_details(self):
print("--- Second Task")
response = self.client.post(return_data, headers={"authority": "staging.domain.com", "referer":"https://staging.domain.com/"})
print(response.status_code)
response_data = json.loads(response.text)
print(response_data["details"]["claim_uri"])
self.response_data
def on_start(self):
self.get_details()
@seq_task(3)
def post_details(self):
print(self.get_details())
print("-- Third Task", self.response_data)
#return_data = self.response_data["details"]["claim_uri"]
#response = self.client.post(return_data, headers={"authority": "staging.domain.com", "referer":"https://staging.domain.com/"})
#print(response.text)
class MyLocust(HttpLocust):
task_set = MyTaskSequence
min_wait = 5000
max_wait = 15000
host = 'https://staging.domain.com'
输出:
[2018-11-19 19:24:19,784] system.local/INFO/stdout:
[2018-11-19 19:24:19,784] system.local/INFO/stdout: --- First Task
[2018-11-19 19:24:19,785] system.local/INFO/stdout:
[2018-11-19 19:24:20,235] system.local/INFO/stdout: 200
[2018-11-19 19:24:20,235] system.local/INFO/stdout:
[2018-11-19 19:24:29,372] system.local/INFO/stdout: --- Second Task
[2018-11-19 19:24:29,373] system.local/INFO/stdout:
[2018-11-19 19:24:29,653] system.local/INFO/stdout: 200
[2018-11-19 19:24:29,654] system.local/INFO/stdout:
[2018-11-19 19:24:29,654] system.local/INFO/stdout: /payment/initiate/claim/bc6d5024-f608-41af-8e78-191798c31a69/
[2018-11-19 19:24:29,654] system.local/INFO/stdout:
[2018-11-19 19:24:37,089] system.local/INFO/stdout: --- Second Task
[2018-11-19 19:24:37,089] system.local/INFO/stdout:
[2018-11-19 19:24:37,367] system.local/INFO/stdout: 200
[2018-11-19 19:24:37,367] system.local/INFO/stdout:
[2018-11-19 19:24:37,367] system.local/INFO/stdout: /payment/initiate/claim/72217a35-01fc-488e-885e-aea81a57a463/
[2018-11-19 19:24:37,368] system.local/INFO/stdout:
[2018-11-19 19:24:37,368] system.local/INFO/stdout: None
[2018-11-19 19:24:37,368] system.local/INFO/stdout:
[2018-11-19 19:24:37,368] system.local/INFO/stdout: ('-- Third Task', '')
[2018-11-19 19:24:37,368] system.local/INFO/stdout:
^C[2018-11-19 19:24:40,598] system.local/ERROR/stderr: KeyboardInterrupt
我们想将response_data["details"]["claim_uri"]
变量传递给@seq_task(3)。这样我们就可以形成一个动态目标,即domain.com/response_data["details"]["claim_uri"]
。此变量将为字符串类型,每次调用第三项任务时都必须分别传递。
我们尝试了this方法,但输出结果为None。
是否有任何遗漏的东西?
答案 0 :(得分:1)
问题出在可变响应数据上。在不使用self的情况下调用该变量时,其范围是局部的。
下面的代码有效。
@seq_task(2)
def get_details(self):
<-- function data -->
self.response_data = json.loads(response.text)