【问题标题】:How to explicitly trigger MasterLocustRunner.quit() in locust如何在 locust 中显式触发 MasterLocustRunner.quit()
【发布时间】:2019-12-23 14:00:24
【问题描述】:

为了模拟 locust 中的特定迭代次数,我使用如下简单的代码流:

class NcsoTest(TaskSet):

REQ_HEADER = {
    "Accept": "application/json",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "keep-alive",
    "Content-Length": "860",
    "Content-Type": "application/json",
    "User-Agent": "python-requests/2.21.0",
 }

@seq_task(1)
def user_workflow(self):

    locrunner = MasterLocustRunner()
    for i in range(1, 100, 1):
        self.send_post_request()
        self.send_get_request()
    Singleton.logger().info("Test Number reached. Stopping Locust.")
    #runners.locust_runner.quit()
    locrunner.quit()

def send_post_request(self):
    response = self.client.post("/api/v2/services", data=Singleton.json_body, headers=NcsoTest.REQ_HEADER)
    print response


def send_get_request(self):
    response = self.client.get("/api/v2/services", headers=NcsoTest.REQ_HEADER)
    print response

class NcsoLoad(HttpLocust):

    max_wait = 300
    min_wait = 300
    sleep_time = 10

    task_set = NcsoTest

为了运行这个测试,我使用带有以下参数的命令:

--host https://10.123.123.123 --min_wait_time 300 --max_wait_time 300 --num_clients 1 --hatch_rate 1 --test_time 5m

虽然,我可以执行 100 个请求,但如果这 100 个请求在 5 分钟前完成,locust runner 会不断给出错误,指出 MasterLocustRunner 需要更多参数,即 locust_class 和选项(我在 locust 命令中指定)

如果我取消注释“runners.locust_runner.quit()”并注释“locrunner.quit()”,测试会继续等待,直到运行时结束后来自 Master 的优雅终止调用。

我想知道的是,如何手动/结束优雅地终止蝗虫 - 主从。

【问题讨论】:

    标签: locust


    【解决方案1】:

    这在 locust 1.1 中得到了很大改进。您现在可以从用户调用self.environment.runner.quit()(或从任务集调用self.user.environment.runner.quit()

    你也可以从一个完全独立的 greenlet 中调用它(也许不是你要找的,但我想我会提到它),就像这里 https://docs.locust.io/en/stable/extending-locust.html#run-a-background-greenlet

    您应该在您的用户任务中创建一个新的 Runner(您的代码会这样做)。这有点太野了:)

    【讨论】:

    • 我打电话给self.environment.runner.quit(),上面写着[2021-05-14 12:23:13,986] Karls-MacBook-Pro.local/INFO/locust.runners: Stopping 3 users,但测试只是继续在无头模式下进行(没有任务运行,它们停止了,但报告每隔几秒钟就会出现一次)。我必须按 ctrl+c 才能退出测试。我必须打电话给quit()吗?
    猜你喜欢
    • 1970-01-01
    • 2016-02-28
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 2014-11-28
    相关资源
    最近更新 更多