|
| 1 | +# |
| 2 | +# SEE https://docs.locust.io/en/stable/quickstart.html |
| 3 | +# |
| 4 | +# This script allows testing running a function via the map endpoint |
| 5 | +# |
| 6 | + |
| 7 | + |
| 8 | +import json |
| 9 | +from datetime import timedelta |
| 10 | + |
| 11 | +import jsf |
| 12 | +from common.base_user import OsparcWebUserBase |
| 13 | +from locust import events, task |
| 14 | +from locust.argument_parser import LocustArgumentParser |
| 15 | +from tenacity import ( |
| 16 | + Retrying, |
| 17 | + retry_if_exception_type, |
| 18 | + stop_after_delay, |
| 19 | + wait_exponential, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +# Register the custom argument with Locust's parser |
| 24 | +@events.init_command_line_parser.add_listener |
| 25 | +def _(parser: LocustArgumentParser) -> None: |
| 26 | + parser.add_argument( |
| 27 | + "--function_uid", |
| 28 | + type=str, |
| 29 | + required=True, |
| 30 | + help="The function UUID to test", |
| 31 | + ) |
| 32 | + parser.add_argument( |
| 33 | + "--function-input-json-schema", |
| 34 | + type=str, |
| 35 | + required=True, |
| 36 | + help="JSON schema for the function job inputs", |
| 37 | + ) |
| 38 | + parser.add_argument( |
| 39 | + "--max-poll-time-seconds", |
| 40 | + type=int, |
| 41 | + default=60, |
| 42 | + help="Maximum time to wait for the function job to complete", |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +class WebApiUser(OsparcWebUserBase): |
| 47 | + @task |
| 48 | + def get_endpoint(self) -> None: |
| 49 | + |
| 50 | + function_uid = self.environment.parsed_options.function_uid |
| 51 | + job_input_schema = json.loads(self.environment.parsed_options.body_json_schema) |
| 52 | + max_poll_time = timedelta( |
| 53 | + seconds=self.environment.parsed_options.max_poll_time_seconds |
| 54 | + ) |
| 55 | + |
| 56 | + # run function |
| 57 | + kwargs = {} |
| 58 | + job_input_faker = jsf.JSF(job_input_schema) |
| 59 | + kwargs["json"] = job_input_faker.generate() |
| 60 | + kwargs["headers"] = { |
| 61 | + "x-simcore-parent-project-uuid": "null", |
| 62 | + "x-simcore-parent-node-id": "null", |
| 63 | + } |
| 64 | + |
| 65 | + response = self.authenticated_post( |
| 66 | + url=f"/v0/functions/{function_uid}:run", kwargs=kwargs |
| 67 | + ) |
| 68 | + response.raise_for_status() |
| 69 | + job_uid = response.json().get("uid") |
| 70 | + |
| 71 | + # wait for the job to complete |
| 72 | + for attempt in Retrying( |
| 73 | + stop=stop_after_delay(max_delay=max_poll_time), |
| 74 | + wait=wait_exponential(multiplier=1, min=1, max=10), |
| 75 | + reraise=True, |
| 76 | + retry=retry_if_exception_type(ValueError), |
| 77 | + ): |
| 78 | + with attempt: |
| 79 | + job_status_response = self.authenticated_get( |
| 80 | + f"/v0/function_jobs/{job_uid}/status" |
| 81 | + ) |
| 82 | + job_status_response.raise_for_status() |
| 83 | + status = job_status_response.json().get("status") |
| 84 | + if status != "SUCCESS": |
| 85 | + raise ValueError( |
| 86 | + f"Function job ({job_uid=}) for function ({function_uid=}) returned {status=}" |
| 87 | + ) |
0 commit comments