python .\locust-workers.py -h
usage: locust-workers.py [-h] [-p path] [-s num] [-f LOCUSTFILE]
Run locust workers
optional arguments:
-h, --help show this help message and exit
-p path, --locust-path path
Locust execution path (default:
./.venv/Scripts/locust)
-s num, --number-slave num
Number of slaves (default: 2)
-f LOCUSTFILE, --locustfile LOCUSTFILE
Python module file to import, e.g. '../other.py'
(default: ./locustfile.py)
Last active
May 27, 2022 11:07
-
-
Save Thong-Tran/9b93661ebb9157759ed6a44516062315 to your computer and use it in GitHub Desktop.
Run locust workers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from os.path import abspath | |
from subprocess import call | |
from threading import Thread | |
from time import sleep | |
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter | |
if sys.platform == 'win32': | |
DEFAULT_PATH = './.venv/Scripts/locust' | |
else: | |
DEFAULT_PATH = './.venv/bin/locust' | |
running = True | |
def run_slave(*args, **kwargs): | |
while running: | |
call(*args, **kwargs) | |
def run_locust(locust_path=DEFAULT_PATH, number_slave=2, locustfile='./locustfile.py'): | |
locust_path = abspath(locust_path) | |
locustfile = abspath(locustfile) | |
threads = [ | |
Thread( | |
target=call, | |
args=('"{}" --web-host "127.0.0.1" -f "{}" --master'.format(locust_path, locustfile),), | |
kwargs={'shell': True} | |
) | |
] | |
for _ in range(number_slave): | |
threads.append(Thread( | |
target=run_slave, | |
args=('"{}" -f "{}" --worker'.format(locust_path, locustfile),), | |
kwargs={'shell': True} | |
)) | |
for t in threads: | |
t.start() | |
try: | |
while True: | |
sleep(5) | |
except KeyboardInterrupt: | |
pass | |
global running | |
running = False | |
for t in threads: | |
t.join() | |
def main(argv): | |
parser = ArgumentParser( | |
formatter_class=ArgumentDefaultsHelpFormatter, | |
description=('Run locust workers'), | |
add_help=True, | |
) | |
parser.add_argument( | |
'-p', "--locust-path", | |
metavar='path', type=str, | |
default= run_locust.__defaults__[0], | |
help='Locust execution path' | |
) | |
parser.add_argument( | |
'-s', "--number-slave", | |
metavar='num', type=int, | |
default= run_locust.__defaults__[1], | |
help='Number of slaves' | |
) | |
parser.add_argument( | |
'-f', "--locustfile", | |
metavar='LOCUSTFILE', type=str, | |
default= run_locust.__defaults__[2], | |
help="Python module file to import, e.g. '../other.py'" | |
) | |
kwargs = vars(parser.parse_args(argv)) | |
run_locust(**kwargs) | |
if __name__ == '__main__': | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment