如何以编程方式 运行 master/slave Locust 运行ner 以便奴隶在最后停止
How to run master/slave Locust runner programmatically so slaves stops at the end
我有这个简单的 master/slave 脚本,使用 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
在开始进程之前),如果您 运行在不同的机器上使用 slave 会稍微复杂一些,但请查看为您完成工作的 locust-swarm (https://github.com/SvenskaSpel/locust-swarm)
至于 "do stuff after the test" 有一个 "quitting" 您可以订阅的事件:
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
我有这个简单的 master/slave 脚本,使用 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
在开始进程之前),如果您 运行在不同的机器上使用 slave 会稍微复杂一些,但请查看为您完成工作的 locust-swarm (https://github.com/SvenskaSpel/locust-swarm)
至于 "do stuff after the test" 有一个 "quitting" 您可以订阅的事件:
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