-
Notifications
You must be signed in to change notification settings - Fork 32
🎨 Add load tests of functions section in api server
#7729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bisgaard-itis
merged 25 commits into
ITISFoundation:master
from
bisgaard-itis:add-functions-locust-test
May 26, 2025
Merged
Changes from 8 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
9e41864
add initial function workflow
bisgaard-itis 41ed2b8
improve names of endpoints in statistics
bisgaard-itis 9725818
add cleanup to workflow
bisgaard-itis 1aac5cc
minor cleanup
bisgaard-itis db922f2
add load test for run endpoint
bisgaard-itis fd5794a
Merge branch 'master' into add-functions-locust-test
bisgaard-itis d902093
remove comment
bisgaard-itis 8d8f781
Merge branch 'master' into add-functions-locust-test
bisgaard-itis 3c6efc4
default outside Field @pcrespov
bisgaard-itis 2d479cf
remove _ @pcrespov
bisgaard-itis fb14d90
@pcrespov use tmpdir trick
bisgaard-itis 269829a
add initial function workflow
bisgaard-itis 4433304
improve names of endpoints in statistics
bisgaard-itis 66ccb86
add cleanup to workflow
bisgaard-itis 5c3bf0e
minor cleanup
bisgaard-itis 7ee1664
add load test for run endpoint
bisgaard-itis 9c6686e
remove comment
bisgaard-itis 0890d5c
default outside Field @pcrespov
bisgaard-itis 8767187
remove _ @pcrespov
bisgaard-itis 431b34e
@pcrespov use tmpdir trick
bisgaard-itis 9903871
Merge branch 'add-functions-locust-test' of github.com:bisgaard-itis/…
bisgaard-itis 174fb88
Merge branch 'master' into add-functions-locust-test
bisgaard-itis ce8e87d
update to functions load test
bisgaard-itis 1dbc632
ensure locust load test is set up correctly
bisgaard-itis a38d25f
Merge branch 'master' into add-functions-locust-test
bisgaard-itis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| import json | ||
| import random | ||
| from datetime import timedelta | ||
| from pathlib import Path | ||
| from tempfile import TemporaryDirectory | ||
| from urllib.parse import quote | ||
| from uuid import UUID | ||
|
|
||
| from locust import HttpUser, task | ||
| from pydantic import BaseModel, Field | ||
| from pydantic_settings import BaseSettings, SettingsConfigDict | ||
| from requests.auth import HTTPBasicAuth | ||
| from tenacity import retry, retry_if_exception_type, stop_after_delay, wait_exponential | ||
| from urllib3 import PoolManager, Retry | ||
|
|
||
|
|
||
| class UserSettings(BaseSettings): | ||
| model_config = SettingsConfigDict(extra="ignore") | ||
| OSPARC_API_KEY: str = Field(default=...) | ||
| OSPARC_API_SECRET: str = Field(default=...) | ||
|
|
||
|
|
||
| _SOLVER_KEY = "simcore/services/comp/osparc-python-runner" | ||
| _SOLVER_VERSION = "1.2.0" | ||
|
|
||
| _PYTHON_SCRIPT = """ | ||
| import numpy as np | ||
| import pathlib as pl | ||
| import json | ||
|
|
||
| def main(): | ||
|
|
||
| input_json = pl.Path(os.environ["INPUT_FOLDER"]) / "function_inputs.json" | ||
| object = json.load(input_json..read_text()) | ||
| x = object["x"] | ||
| y = object["y"] | ||
|
|
||
| return np.sinc(x) * np.sinc(y) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| """ | ||
|
|
||
|
|
||
| class Schema(BaseModel): | ||
| schema_content: dict = Field(default={}) | ||
| schema_class: str = Field(default="application/schema+json") | ||
|
|
||
|
|
||
| class Function(BaseModel): | ||
| function_class: str = Field(default="SOLVER") | ||
| title: str | ||
| description: str | ||
| input_schema: Schema = Field(default=Schema()) | ||
| output_schema: Schema = Field(default=Schema()) | ||
| default_inputs: dict[str, str] = Field(default=dict()) | ||
| solver_key: str = Field(default=_SOLVER_KEY) | ||
| solver_version: str = Field(default=_SOLVER_VERSION) | ||
|
|
||
|
|
||
| class MetaModelingUser(HttpUser): | ||
| def __init__(self, *args, **kwargs): | ||
| self._user_settings = UserSettings() | ||
| self._auth = HTTPBasicAuth( | ||
| username=self._user_settings.OSPARC_API_KEY, | ||
| password=self._user_settings.OSPARC_API_SECRET, | ||
| ) | ||
| retry_strategy = Retry( | ||
| total=4, | ||
| backoff_factor=4.0, | ||
| status_forcelist={429, 503, 504}, | ||
| allowed_methods={ | ||
| "DELETE", | ||
| "GET", | ||
| "HEAD", | ||
| "OPTIONS", | ||
| "PUT", | ||
| "TRACE", | ||
| "POST", | ||
| "PATCH", | ||
| "CONNECT", | ||
| }, | ||
| respect_retry_after_header=True, | ||
| raise_on_status=True, | ||
| ) | ||
| self.pool_manager = PoolManager(retries=retry_strategy) | ||
|
|
||
| self._function_uid = None | ||
| self._input_json_uuid = None | ||
| self._script_uuid = None | ||
| self._run_uid = None | ||
| self._solver_job_uid = None | ||
|
|
||
| super().__init__(*args, **kwargs) | ||
|
|
||
| def on_stop(self) -> None: | ||
| if self._script_uuid is not None: | ||
| _ = self.client.delete( | ||
| f"/v0/files/{self._script_uuid}", | ||
| name="/v0/files/[file_id]", | ||
| auth=self._auth, | ||
| ) | ||
| if self._input_json_uuid is not None: | ||
| _ = self.client.delete( | ||
bisgaard-itis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| f"/v0/files/{self._input_json_uuid}", | ||
| name="/v0/files/[file_id]", | ||
| auth=self._auth, | ||
| ) | ||
| if self._function_uid is not None: | ||
| _ = self.client.delete( | ||
| f"/v0/functions/{self._function_uid}", | ||
| name="/v0/functions/[function_uid]", | ||
| auth=self._auth, | ||
| ) | ||
| if self._run_uid is not None: | ||
| _ = self.client.delete( | ||
| f"/v0/function_jobs/{self._run_uid}", | ||
| name="/v0/function_jobs/[function_run_uid]", | ||
| auth=self._auth, | ||
| ) | ||
|
|
||
| @task | ||
| def run_function(self): | ||
| with TemporaryDirectory() as tmpdir: | ||
| tmp_dir = Path(tmpdir) | ||
bisgaard-itis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| script = tmp_dir / "script.py" | ||
| script.write_text(_PYTHON_SCRIPT) | ||
| self._script_uuid = self.upload_file(script) | ||
|
|
||
| inputs = {"x": random.uniform(-10, 10), "y": random.uniform(-10, 10)} | ||
| input_json = tmp_dir / "function_inputs.json" | ||
| input_json.write_text(json.dumps(inputs)) | ||
| self._input_json_uuid = self.upload_file(input_json) | ||
|
|
||
| _function = Function( | ||
| title="Test function", | ||
| description="Test function", | ||
| default_inputs={"input_0": f"{self._script_uuid}"}, | ||
| ) | ||
| response = self.client.post( | ||
| "/v0/functions", json=_function.model_dump(), auth=self._auth | ||
| ) | ||
| response.raise_for_status() | ||
| self._function_uid = response.json().get("uid") | ||
| assert self._function_uid is not None | ||
|
|
||
| response = self.client.post( | ||
| f"/v0/functions/{self._function_uid}:run", | ||
| json={"input_1": f"{self._input_json_uuid}"}, | ||
| auth=self._auth, | ||
| name="/v0/functions/[function_uid]:run", | ||
| ) | ||
| response.raise_for_status() | ||
| self._run_uid = response.json().get("uid") | ||
| assert self._run_uid is not None | ||
| self._solver_job_uid = response.json().get("solver_job_id") | ||
| assert self._solver_job_uid is not None | ||
|
|
||
| self.wait_until_done() | ||
|
|
||
| response = self.client.get( | ||
| f"/v0/solvers/{quote(_SOLVER_KEY, safe='')}/releases/{_SOLVER_VERSION}/jobs/{self._solver_job_uid}/outputs", | ||
| auth=self._auth, | ||
| name="/v0/solvers/[solver_key]/releases/[solver_version]/jobs/[solver_job_id]/outputs", | ||
| ) | ||
| response.raise_for_status() | ||
|
|
||
| @retry( | ||
| stop=stop_after_delay(timedelta(minutes=10)), | ||
| wait=wait_exponential(multiplier=1, min=1, max=10), | ||
| retry=retry_if_exception_type(AssertionError), | ||
| reraise=False, | ||
| ) | ||
| def wait_until_done(self): | ||
| response = self.client.get( | ||
| f"/v0/function_jobs/{self._run_uid}/status", | ||
| auth=self._auth, | ||
| name="/v0/function_jobs/[function_run_uid]/status", | ||
| ) | ||
| response.raise_for_status() | ||
| status = response.json().get("status") | ||
| assert status in ["DONE", "FAILED"] | ||
|
|
||
| def upload_file(self, file: Path) -> UUID: | ||
| assert file.is_file() | ||
| with file.open(mode="rb") as f: | ||
| files = {"file": f} | ||
| response = self.client.put( | ||
| "/v0/files/content", files=files, auth=self._auth | ||
| ) | ||
| response.raise_for_status() | ||
| file_uuid = response.json().get("id") | ||
| assert file_uuid is not None | ||
| return UUID(file_uuid) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| function = Function( | ||
| title="Test function", | ||
| description="Test function", | ||
| default_inputs={}, | ||
| ) | ||
| print(function.model_dump_json()) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ locust-plugins | |
| parse | ||
| pydantic | ||
| pydantic-settings | ||
| tenacity | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.