python-3.x - 在 Locust 中创建单个设置步骤?
问题描述
您好,我正在尝试对我创建的 RESTful 烧瓶应用程序进行一些负载平衡测试。我正在使用蝗虫。
生成的每个用户都有一个on_start
方法。我想在客户端 ONCE 上创建资源,并让每个“用户”任务查询该资源。
class UserBehavior(TaskSet):
def on_start(self):
""" on_start is called when a Locust start before
any task is scheduled
"""
self.client.post("/resources/", json=RESOURCE_1, headers=headers_with_auth)
@task(1)
def profile(self):
self.client.get("/resources/", json={})
class WebsiteUser(HttpLocust):
task_set = UserBehavior
min_wait = 5000
max_wait = 9000
这将尝试为每个生成的用户创建一个资源。这将失败,因为资源需要是唯一的。
我试过了:
class UserBehavior(TaskSet):
def run(self, *args, **kwargs):
self.client.post("/resources/", json=RESOURCE_1, headers=headers_with_auth)
super().run(args, kwargs)
但这似乎也适用于每个用户。有没有办法使用创建单个设置步骤self.client
?谢谢
解决方案
这行得通,只是在设置中创建了我自己的客户端,并且仅在蜂群产生时调用一次
class WebsiteUser(HttpLocust):
def setup(self):
client = clients.HttpSession(base_url=self.host)
client.post("/resources/", json=RESOURCE_1, headers=headers_with_auth)
task_set = UserBehavior
min_wait = 500
max_wait = 900
推荐阅读
- python - 为什么我们需要列表构造函数来在 python 中创建列表。有什么优点和缺点
- python - 在网络摄像头中检测到物体时计数 5 秒。
- python - 将会话 Python 保存到 C# 中的类似会话的变量中
- php - 如何解决 抱歉,找不到您要查找的页面
- python - 打开 csv 文件 - SyntaxError: 无效的语法
- apache-spark - PySpark 数据框 - 用先前的有效值替换列中的连续 NaN 值
- javascript - 通过给定的字段值查找数组对象
- python - 伪造者安装问题
- javascript - Angular 5加载谷歌图表
- javascript - 使用 CSS 和 jQuery 构建金字塔