jhutar
jhutar

Reputation: 1491

How to run master/slave Locust runner programmatically so slaves stops at the end

I have this simple master/slave scripts, using locustio==0.13.5. This is the master:

#!/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

and this is the slave:

#!/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()

When I start master and slave, I can see how master waits for a slave to come up, then how slave is executing the test and I see report printed by master before it finished. But slave does not finishes - it hangs running, doing nothing (I assume).

I would like slave to either exit or to restart and attempt to connect to master again in case I just rerun the master script. Does anybody have any idea on how to do that please?

Upvotes: 2

Views: 2199

Answers (1)

Cyberwiz
Cyberwiz

Reputation: 11426

I usually just set any parameters as environment variables and read them from the script (os.environ['MY_ENV_VAR'])

If you're running the slaves on the same server that should be easy (just run export MY_ENV_VAR=Hello before starting the processes), if you are running slaves on different machines it would be a little more complicated but check out locust-swarm that does the work for you (https://github.com/SvenskaSpel/locust-swarm)

As for the "do stuff after the test" there is a "quitting" event that you can subscribe to:

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

Or, for the upcoming 1.0 version:

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

Upvotes: 2

Related Questions