我有这个简单的主/从脚本,使用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()
当我启动主服务器和从服务器时,我可以看到主服务器如何等待从服务器出现,然后从服务器如何执行测试,并且我看到主服务器在完成之前打印的报告。但奴隶并没有完成 - 它挂起运行,什么都不做(我认为)。
我希望从站退出或重新启动并尝试再次连接到主站,以防我重新运行主站脚本。有人知道如何做到这一点吗?
最佳答案
我通常只是将任何参数设置为环境变量并从脚本中读取它们(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 运行程序,以便从服务器在最后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60687998/