我有这个简单的主/从脚本,使用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()
启动主服务器和从服务器时,我可以看到主服务器如何等待从服务器出现,然后从服务器如何执行测试,并且看到主服务器在完成之前打印的报告。但是奴隶并没有完成-它挂起了,什么也没做(我想)。
如果我只是重新运行主脚本,我希望从属退出或重新启动并尝试再次连接到主服务器。请问有人对如何做有任何想法吗?
答案 0 :(得分:2)
我通常只是将任何参数设置为环境变量,然后从脚本中读取它们(os.environ ['MY_ENV_VAR'])
如果要在同一台服务器上运行从属服务器应该很容易(在启动进程之前运行export MY_ENV_VAR=Hello
),如果要在不同的计算机上运行从属服务器,则会有些复杂,但是请签出蝗虫群为您效劳(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