【问题标题】:Pause/Delay sending of new batch of users from swarm暂停/延迟从 Swarm 发送新一批用户
【发布时间】: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


【解决方案1】:

我会调试你的逻辑。在每个if 块中的各个位置和每个块之间放置更多print 语句。在处理一长串决策时,很容易出错。

在这种情况下,您只想在非常具体的情况下sleep,但它没有发生。很可能您在没想到时设置了end = True,这样您就不会在睡觉,而是会立即获得新用户。

编辑: 再次查看您的问题和问题描述,听起来您希望 Locust 发送一个请求,然后再发送另一个请求。这不是蝗虫的工作方式。 Locust 将为用户运行您的任务代码。完成后,该用户会离开并等待一段时间(看起来您已将其设置为 2 秒),然后它会生成另一个用户并重新开始任务。这个想法是它会尽量保持你告诉它的用户数量接近恒定。默认情况下,它不仅会运行 1000 个用户然后结束测试。

如果你想让所有 1000 个用户都运行,你需要让他们继续执行代码。例如,您可以将任务中的所有内容放在另一个 while 循环中,并以另一种方式中断和结束。这样,即使在建立了套接字连接并发送了您期望的单个消息之后,用户仍将保持在循环中并且不会因为没有事情可做而结束。这样做需要更多的工作和协调,但这是可能的。如果这不是您正在寻找的,那么可能还有其他关于不同方法的问题。

【讨论】:

  • 谢谢,这回答了我的担忧。我想确认 Locust 是否确实每秒执行一个请求,并且您已经验证了这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-09
  • 1970-01-01
  • 2012-04-06
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
相关资源
最近更新 更多