I'm using locust to load test a bunch of apps, the problem is that it's using more than 90% of the CPU and so I want to switch to distributed mode with master and workers, I know how it's done in command line but I'm using locust as a library and it seems the docs don't cover this case, here's some snippets of my code:
User:
# host, port and reqs are external parameters
class User(HttpUser):
host = f"{host}/{port}"
@task
def task1(self):
for req in reqs:
self.client.request(req['method'],req['path'],name=f"{port}",headers=req['headers'],data=req['body'])
def on_start(self):
self.client.verify = False
main:
env = Environment(user_classes=[User])
env.create_local_runner()
# env.create_web_ui("127.0.0.1", 8089)
gevent.spawn(stats_printer(env.stats))
gevent.spawn(stats_history, env.runner)
csvWriter = StatsCSVFileWriter(
environment=env,
base_filepath=f"{CWD}/outputs/{port}",
full_history=True,
percentiles_to_report=[0.5,0.6,0.7,0.8,0.9, 0.95, 0.99]
)
gevent.spawn(csvWriter)
env.runner.start(100, spawn_rate=5)
gevent.spawn_later(30,lambda: saveReportAndQuit(env,port))
env.runner.greenlet.join()
# env.web_ui.stop()
python version: 3.9.7
Locust version: 2.4.0