【发布时间】:2021-12-25 08:41:29
【问题描述】:
我有一个测试用例,我需要生成 1000 个 websocket 连接并通过 Locust 任务维持对它们的对话(它有一个用于 websocket 连接的预定义发送/接收过程)。我可以通过 locust 中的以下设置成功地做到这一点:
最大用户数:1000 孵化率:1000
但是,此设置每秒打开 1000 个连接。即使我降低孵化率,它也会达到每秒继续产生 1000 个 websocket 连接的时间。有没有办法立即产生 1000 个用户并在相当长的一段时间内停止/延迟群发送新的 1000 个连接?
我正在尝试测试我的服务器是否可以处理 1000 个用户通过 websocket 连接从我的服务器发送和接收消息。我在 python 中尝试过多处理方法,但我很难用 Locust 尽可能快地生成连接。
类用户行为(任务集):
statements = [
"Do you like coffee?",
"What's your favorite book?",
"Do you invest in crypto?",
"Who will host the Superbowl next year?",
"Have you listened to the new Adele?",
"Coldplay released a new album",
"I watched the premiere of Succession season 3 last night",
"Who is your favorite team in the NBA?",
"I want to buy the new Travis Scott x Jordan shoes",
"I want a Lamborghini Urus",
"Have you been to the Philippines?",
"Did you sign up for a Netflix account?"
]
def on_start(self):
pass
def on_quit(self):
pass
@task
def send_convo(self):
end = False
ws_url = "ws://xx.xx.xx.xx:8080/websocket"
self.ws = create_connection(ws_url)
body = json.dumps({"text": "start blender"})
self.ws.send(body)
while True:
#print("Waiting for response..")
response = self.ws.recv()
if response != None:
if "Sorry, this world closed" in response:
end = True
break
if not end:
body = json.dumps({"text": "begin"})
self.ws.send(body)
while True:
#print("Waiting for response..")
response = self.ws.recv()
if response != None:
# print("[BOT]: ", response)
if "Sorry, this world closed" in response:
end = True
self.ws.close()
break
if not end:
body = json.dumps({"text": random.choice(self.statements)})
start_at = time.time()
self.ws.send(body)
while True:
response = self.ws.recv()
if response != None:
if "Sorry, this world closed" not in response:
response_time = int((time.time() - start_at)*1000)
print(f"[BOT]Response: {response}")
response_length = len(response)
events.request_success.fire(
request_type='Websocker Recv',
name='test/ws/echo',
response_time=response_time,
response_length=response_length,
)
else:
end = True
self.ws.close()
break
if not end:
body = json.dumps({"text": "[DONE]"})
self.ws.send(body)
while True:
response = self.ws.recv()
if response != None:
if "Sorry, this world closed" in response:
end = True
self.ws.close()
break
if not end:
time.sleep(1)
body = json.dumps({"text": "EXIT"})
self.ws.send(body)
time.sleep(1)
self.ws.close()
class WebsiteUser(HttpUser):
tasks = [UserBehavior]
wait_time = constant(2)
host = "ws://xx.xx.xx.xx:8080/websocket"
对于这个特定的测试,我将最大用户数设置为1,将孵化率设置为1,很明显,Locust 每秒发送 1 个请求,如以下响应所示:
[BOT]Response: {"text": "No, I don't have a netflix account. I do have a Hulu account, though.", "quick_replies": null}
enter code here
[BOT]Response: {"text": "I have not, but I would love to go. I have always wanted to visit the Philippines.", "quick_replies": null
[BOT]Response: {"text": "No, I don't have a netflix account. I do have a Hulu account, though.", "quick_replies": null}
[BOT]Response: {"text": "I think it's going to be New Orleans. _POTENTIALLY_UNSAFE__", "quick_replies": null}
我的期望是在我将最大用户设置为 1 并且孵化率为 1 之后,将立即有 1 个 websocket 连接发送随机消息,并从 websocket 服务器接收 1 个主要响应。但发生的事情是它每秒不断重复task,直到我明确点击蝗虫仪表板上的停止按钮。
【问题讨论】:
-
请添加您的 locustfile。请记住,孵化率仅与启动有关,一旦所有用户都开始使用,它就没有任何区别。
-
我的意思是,如果可以将最大用户数设置为 1000,并让蝗虫立即向所有人发送一次消息,并“暂停”发送下一批 1000,因为默认情况下,它将每秒发送另外 1000 个。设置是这样的原因是我在来自蝗虫的那些消息中有一个循环,我在那里做一些计算。我需要测试我的服务器是否可以同时处理 1,000 个正在进行的计算。不是每秒 1,000 次计算。
-
@cyberwiz 要求查看代码的原因是用户数应该是 Locust 产生的最大数量,而不是每秒产生的数量。只要用户继续工作,Locust 就不应该产生另一个。因此,您的代码中可能有一些东西使 Locust 不会认为用户在生成后正在持续工作。如果没有看到代码,我们无能为力。
-
谢谢@Solowalker 我更新了发布蝗虫文件的问题以及我遇到的问题。似乎我的蝗虫测试每秒都在重复执行任务。根据您的评论,我应该有一个输出,因为 1 个最大用户和 1 个孵化率只会发送最多 1 个任务运行。
-
再次阅读本文和您的 cmets 后,我编辑了我的答案。如果仍然不是您要查找的内容,请继续澄清。
标签: python-3.x load-testing locust