Python 需要一种将变量从用户类传递到Locust中的任务集的方法吗
我是蝗虫新手,我正在尝试编写负载测试。我的蝗虫用户的目的是通过调用CreateItemAPI来创建一个项目。在这种情况下,每个请求中的item_id应该是唯一的,因为它具有唯一的条件 这就是我所做的Python 需要一种将变量从用户类传递到Locust中的任务集的方法吗,python,locust,Python,Locust,我是蝗虫新手,我正在尝试编写负载测试。我的蝗虫用户的目的是通过调用CreateItemAPI来创建一个项目。在这种情况下,每个请求中的item_id应该是唯一的,因为它具有唯一的条件 这就是我所做的 import json from random import randint from locust import HttpUser, constant, SequentialTaskSet, task from flow.helper import read_json class Webs
import json
from random import randint
from locust import HttpUser, constant, SequentialTaskSet, task
from flow.helper import read_json
class WebsiteUser(HttpUser):
"""
User class that does requests to the locust web server running on localhost
"""
host = "http://localhost:8080/api/"
wait_time = constant(3)
@task
class SequenceOfTasks(SequentialTaskSet):
item_id = randint(100, 9999)
@task
def create_item(self):
request = read_json('resources/create_item.json')
request['item-id'] = self.item_id
response = self.client.post('createItem', json.dumps(request),
headers={'Content-Type': 'application/json'})
assert response.status_code == 200
第一个请求成功,它成功创建了一个条目。但是后来所有的请求都失败了,在应用服务器的日志中,我可以看到它正在尝试创建具有相同item_id的项。是否有任何方法可以在WebsiteUser类中动态生成一个值并将其传递给SequenceOfTasks 这不是问题的确切答案,但我通过改变方法中的随机逻辑,使该代码在没有冲突的item_id的情况下工作。比如:-
@task
class SequenceOfTasks(SequentialTaskSet):
self.item_id = None
@task
def create_item(self):
request = read_json('resources/create_item.json')
self.item_id = randint(100, 9999)
request['item-id'] = self.item_id
...