-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update actions.py to fix the --quiet flag. (#8)
Signed-off-by: Matthew Heaton <[email protected]>
- Loading branch information
1 parent
4472094
commit 175f885
Showing
1 changed file
with
125 additions
and
125 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,125 @@ | ||
import subprocess | ||
import os | ||
|
||
from chaoslib.deprecation import warn_about_moved_function | ||
from logzero import logger | ||
from pathlib import Path | ||
|
||
__all__ = ["run_script", "stress_endpoint"] | ||
|
||
|
||
def run_script(script_path: str = None, vus: int = 1, duration: str = "1s", log_file: str = None, debug: bool = False): | ||
""" | ||
Run an arbitrary k6 script with a configurable amount of VUs and duration. | ||
Depending on the specs of the attacking machine, possible VU amount may | ||
vary. | ||
For a non-customized 2019 Macbook Pro, it will cap around 250 +/- 50. | ||
Parameters | ||
---------- | ||
script_path : str | ||
Full path to the k6 test script | ||
vus : int | ||
Amount of virtual users to run the test with | ||
duration : str | ||
Duration, written as a string, ie: `1h2m3s` etc | ||
log_file: str | ||
(Optional) Relative path to the file where output should be logged. | ||
""" | ||
logger.info("Running " + script_path) | ||
_runScript( | ||
script=script_path, | ||
vus=vus, | ||
duration=duration, | ||
log_file=log_file, | ||
debug=debug | ||
) | ||
|
||
|
||
def stress_endpoint(endpoint: str = None, vus: int = 1, duration: str = "1s", log_file: str = None, debug: bool = False): | ||
""" | ||
Stress a single endpoint with a configurable amount of VUs and duration. | ||
Depending on the specs of the attacking machine, possible VU amount may | ||
vary. | ||
For a non-customized 2019 Macbook Pro, it will cap around 250 +/- 50. | ||
Parameters | ||
---------- | ||
endpoint : str | ||
The URL to the endpoint you want to stress, including the scheme prefix. | ||
vus : int | ||
Amount of virtual users to run the test with | ||
duration : str | ||
Duration, written as a string, ie: `1h2m3s` etc | ||
log_file: str | ||
(Optional) Relative path to the file where output should be logged. | ||
""" | ||
basePath = Path(__file__).parent | ||
jsPath = str(basePath.parent) + "/chaosk6/scripts" | ||
|
||
logger.info( | ||
'Stressing the endpoint "{}" with {} VUs for {}.'.format( | ||
endpoint, vus, duration | ||
) | ||
) | ||
|
||
env = dict(**os.environ, CHAOS_K6_URL=endpoint) | ||
r = _runScript( | ||
script=jsPath + "/single-endpoint.js", | ||
vus=vus, | ||
duration=duration, | ||
log_file=log_file, | ||
environ=env, | ||
debug=debug) | ||
|
||
logger.info("Stressing completed.") | ||
if log_file != None: | ||
logger.info("Logged K6 output to {}.".format(log_file)) | ||
return r | ||
|
||
|
||
def _runScript( | ||
script: str, | ||
vus: int, | ||
duration: str, | ||
log_file: str, | ||
environ: dict = None, | ||
debug: bool = False | ||
): | ||
|
||
if not environ: | ||
environ = dict(os.environ) | ||
command = [ | ||
"k6", | ||
"run", | ||
"-quiet", | ||
script, | ||
"--vus", str(vus), | ||
"--duration", | ||
str(duration) | ||
] | ||
|
||
# Default output to the void | ||
pipeoutput = subprocess.DEVNULL | ||
# Use log file location if provided | ||
if log_file != None: | ||
pipeoutput = open(log_file, "w") | ||
|
||
with subprocess.Popen( | ||
command, stderr=subprocess.STDOUT, stdout=None if debug is True else pipeoutput, env=environ | ||
) as p: | ||
return p.returncode is None | ||
|
||
|
||
def runScript(script_path: str = None, vus: int = 1, duration: str = "1s", debug: bool = False): | ||
warn_about_moved_function( | ||
"The action `runScript` is now called `run_script`." | ||
"Please consider updating your experiments accordingly.") | ||
return run_script(script_path, vus, duration) | ||
|
||
|
||
def stressEndpoint(endpoint: str = None, vus: int = 1, duration: str = "1s", debug: bool = False): | ||
warn_about_moved_function( | ||
"The action `stressEndpoint` is now called `stress_endpoint`." | ||
"Please consider updating your experiments accordingly.") | ||
return run_script(endpoint, vus, duration) | ||
import subprocess | ||
import os | ||
|
||
from chaoslib.deprecation import warn_about_moved_function | ||
from logzero import logger | ||
from pathlib import Path | ||
|
||
__all__ = ["run_script", "stress_endpoint"] | ||
|
||
|
||
def run_script(script_path: str = None, vus: int = 1, duration: str = "1s", log_file: str = None, debug: bool = False): | ||
""" | ||
Run an arbitrary k6 script with a configurable amount of VUs and duration. | ||
Depending on the specs of the attacking machine, possible VU amount may | ||
vary. | ||
For a non-customized 2019 Macbook Pro, it will cap around 250 +/- 50. | ||
Parameters | ||
---------- | ||
script_path : str | ||
Full path to the k6 test script | ||
vus : int | ||
Amount of virtual users to run the test with | ||
duration : str | ||
Duration, written as a string, ie: `1h2m3s` etc | ||
log_file: str | ||
(Optional) Relative path to the file where output should be logged. | ||
""" | ||
logger.info("Running " + script_path) | ||
_runScript( | ||
script=script_path, | ||
vus=vus, | ||
duration=duration, | ||
log_file=log_file, | ||
debug=debug | ||
) | ||
|
||
|
||
def stress_endpoint(endpoint: str = None, vus: int = 1, duration: str = "1s", log_file: str = None, debug: bool = False): | ||
""" | ||
Stress a single endpoint with a configurable amount of VUs and duration. | ||
Depending on the specs of the attacking machine, possible VU amount may | ||
vary. | ||
For a non-customized 2019 Macbook Pro, it will cap around 250 +/- 50. | ||
Parameters | ||
---------- | ||
endpoint : str | ||
The URL to the endpoint you want to stress, including the scheme prefix. | ||
vus : int | ||
Amount of virtual users to run the test with | ||
duration : str | ||
Duration, written as a string, ie: `1h2m3s` etc | ||
log_file: str | ||
(Optional) Relative path to the file where output should be logged. | ||
""" | ||
basePath = Path(__file__).parent | ||
jsPath = str(basePath.parent) + "/chaosk6/scripts" | ||
|
||
logger.info( | ||
'Stressing the endpoint "{}" with {} VUs for {}.'.format( | ||
endpoint, vus, duration | ||
) | ||
) | ||
|
||
env = dict(**os.environ, CHAOS_K6_URL=endpoint) | ||
r = _runScript( | ||
script=jsPath + "/single-endpoint.js", | ||
vus=vus, | ||
duration=duration, | ||
log_file=log_file, | ||
environ=env, | ||
debug=debug) | ||
|
||
logger.info("Stressing completed.") | ||
if log_file != None: | ||
logger.info("Logged K6 output to {}.".format(log_file)) | ||
return r | ||
|
||
|
||
def _runScript( | ||
script: str, | ||
vus: int, | ||
duration: str, | ||
log_file: str, | ||
environ: dict = None, | ||
debug: bool = False | ||
): | ||
|
||
if not environ: | ||
environ = dict(os.environ) | ||
command = [ | ||
"k6", | ||
"run", | ||
"--quiet", | ||
script, | ||
"--vus", str(vus), | ||
"--duration", | ||
str(duration) | ||
] | ||
|
||
# Default output to the void | ||
pipeoutput = subprocess.DEVNULL | ||
# Use log file location if provided | ||
if log_file != None: | ||
pipeoutput = open(log_file, "w") | ||
|
||
with subprocess.Popen( | ||
command, stderr=subprocess.STDOUT, stdout=None if debug is True else pipeoutput, env=environ | ||
) as p: | ||
return p.returncode is None | ||
|
||
|
||
def runScript(script_path: str = None, vus: int = 1, duration: str = "1s", debug: bool = False): | ||
warn_about_moved_function( | ||
"The action `runScript` is now called `run_script`." | ||
"Please consider updating your experiments accordingly.") | ||
return run_script(script_path, vus, duration) | ||
|
||
|
||
def stressEndpoint(endpoint: str = None, vus: int = 1, duration: str = "1s", debug: bool = False): | ||
warn_about_moved_function( | ||
"The action `stressEndpoint` is now called `stress_endpoint`." | ||
"Please consider updating your experiments accordingly.") | ||
return run_script(endpoint, vus, duration) |