Skip to content

Instantly share code, notes, and snippets.

@snackattas
Last active August 17, 2020 17:05
Show Gist options
  • Save snackattas/e5b66799c2d0e66cf82531e78d7c114b to your computer and use it in GitHub Desktop.
Save snackattas/e5b66799c2d0e66cf82531e78d7c114b to your computer and use it in GitHub Desktop.
Sample invoke task to run locust test
from invoke import task
import logging
from os import environ, mkdir, path
# Default options
environ['ENV'] = environ.get('ENV', 'QA')
environ['ACCOUNT_ID'] = environ.get('ACCOUNT_ID', 'abc')
environ['RUNTIME'] = environ.get('RUNTIME', '5m')
environ['NUM_USERS'] = environ.get('NUM_USERS', '200')
environ['HATCH_RATE'] = environ.get('HATCH_RATE', '5')
environ['RESULTS_DIR'] = environ.get('RESULTS_DIR', 'results')
@task
# Required options
opts = [
f"ENV={environ['ENV']}",
f"ACCOUNT_ID={environ['ACCOUNT_ID']}"
]
# Optional options
if environ.get('FILTERS'):
opts.append(f"FILTERS='{environ['FILTERS']}'")
headless = []
if environ.get('HEADLESS'):
mkdir(environ['RESULTS_DIR'], 0o777)
results_csv_filename = path.join(results_dir, 'results')
headless = [
'--headless',
f"--run-time {environ['RUNTIME']}",
f"-u {environ['NUM_USERS']}",
f"-r {environ['HATCH_RATE']}",
f"--csv={results_csv_filename} --csv-full-history"
]
# Assemble locust command and run
opts_string = ' '.join(opts)
headless_string = ' '.join(headless)
run_cmd = [
opts_string,
"locust -f ./tasks/tasks.py --host='nohost'",
headless_string,
]
run_cmd_string = ' '.join(run_cmd)
result = c.run(run_cmd_string)
return result.ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment