我是蝗虫的新手,正在尝试编写负载测试。我的 Locust 用户的目的是通过调用 createItem API 来创建项目。在这种情况下,每个请求中的 item_id 应该是唯一的,因为它具有唯一的条件。
这就是我所做的。
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?
慕妹3242003
相关分类