gpt4 book ai didi

python - 如何以编程方式运行主/从 Locust runner 以便奴隶在最后停止

转载 作者:行者123 更新时间:2023-12-04 10:24:27 24 4
gpt4 key购买 nike

我有这个简单的主/从脚本,使用 locustio==0.13.5 .这是主人:

#!/usr/bin/env python3

import logging
import argparse
import os
import sys
import time

import urllib3

import locust

import utils


class TestSomething(locust.TaskSet):
@locust.task(1)
def get_hosts_small(self):
print(self.locust.message)
return self.client.get(url='http://localhost', verify=False)

class TheSomething(locust.HttpLocust):
task_set = TestSomething
wait_time = locust.constant(0)


urllib3.disable_warnings()
logging.basicConfig(level=logging.DEBUG)

options = argparse.Namespace()
options.host = "http://localhost"
options.num_clients = 1
options.hatch_rate = options.num_clients
options.num_requests = 10
options.stop_timeout = 1
options.step_load = False
options.reset_stats = False
options.test_duration = 3

options.master_host = 'localhost'
options.master_port = 5557
options.master_bind_host = '*'
options.master_bind_port = 5557
options.heartbeat_liveness = 3
options.heartbeat_interval = 1

options.expect_slaves = 1


test_set = TheSomething
test_set.message = 'Hello'

locust_runner = locust.runners.MasterLocustRunner([test_set], options)
while len(locust_runner.clients.ready) < options.expect_slaves:
logging.info("Waiting for slaves to be ready, %s of %s connected", len(locust_runner.clients.ready), options.expect_slaves)
time.sleep(1)

locust_runner.start_hatching(locust_count=options.num_clients, hatch_rate=options.hatch_rate)
time.sleep(options.test_duration)
locust_runner.quit()
locusts.events.quitting.fire(reverse=True)

print(locust_runner.stats) # actually using custom function to format results

这是奴隶:
#!/usr/bin/env python3

import logging
import argparse
import os
import sys
import time

import locust


class TestSomething(locust.TaskSet):
@locust.task(1)
def get_hosts_small(self):
print(self.locust.message)
return self.client.get(url='http://localhost', verify=False)

class TheSomething(locust.HttpLocust):
task_set = TestSomething
wait_time = locust.constant(0)


logging.basicConfig(level=logging.DEBUG)

options = argparse.Namespace()
options.host = "http://localhost"
options.num_clients = 1
options.hatch_rate = options.num_clients
options.num_requests = 10
options.stop_timeout = 1
options.step_load = False
options.reset_stats = False
options.test_duration = 3

options.master_host = 'localhost'
options.master_port = 5557
options.master_bind_host = '*'
options.master_bind_port = 5557
options.heartbeat_liveness = 3
options.heartbeat_interval = 1


test_set = TheSomething
test_set.message = 'Hello'

locust_runner = locust.runners.SlaveLocustRunner([test_set], options)
locust_runner.worker()

当我启动 master 和 slave 时,我可以看到 master 如何等待 slave 出现,然后 slave 如何执行测试,我看到 master 在完成之前打印的报告。但是奴隶没有完成 - 它挂起运行,什么都不做(我假设)。

我希望 slave 退出或重新启动并尝试再次连接到 master,以防我重新运行 master 脚本。有没有人知道如何做到这一点?

最佳答案

我通常只是将任何参数设置为环境变量并从脚本中读取它们(os.environ['MY_ENV_VAR'])

如果您在同一台服务器上运行从服务器应该很容易(只需在启动进程之前运行 export MY_ENV_VAR=Hello),如果您在不同机器上运行从服务器它会稍微复杂一些,但请查看 locust-swarm为你工作 ( https://github.com/SvenskaSpel/locust-swarm )

至于“在测试后做事”,您可以订阅一个“退出”事件:

https://docs.locust.io/en/0.14.5/api.html#available-hooks

或者,对于即将发布的 1.0 版本:

https://docs.locust.io/en/latest/api.html#locust.event.Events.quitting

关于python - 如何以编程方式运行主/从 Locust runner 以便奴隶在最后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60687998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com