如何以编程方式运行master/slave-hocust-runner,使从属服务器在最后停止

2024-06-23 19:52:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个简单的主/从脚本,使用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并没有完成——它挂起运行,什么也不做(我猜)

我希望从机退出或重新启动,并尝试再次连接到主机,以防我重新运行主脚本。请问有人知道怎么做吗


Tags: testimportselfmasterfalselocalhosthosttime
1条回答
网友
1楼 · 发布于 2024-06-23 19:52:59

我通常只是将任何参数设置为环境变量,然后从脚本中读取它们(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

相关问题 更多 >

    热门问题