generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 49
[Testing / CI/CD] Ability to automate scale testing with a mock server and test different datasets, loadgen, etc. and run it as a part of CI/CD (#274) #274
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
Open
huaxig
wants to merge
2
commits into
kubernetes-sigs:main
Choose a base branch
from
huaxig:cicd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+281
−10
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,35 @@ | ||
| name: E2E Test on change | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| - 'feature/**' | ||
| pull_request: | ||
| branches: | ||
| - main | ||
| - 'feature/**' | ||
|
|
||
| jobs: | ||
| e2e-tests: | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| matrix: | ||
| python-version: ['3.13'] | ||
| steps: | ||
| - name: Checkout Code | ||
| uses: actions/checkout@v4 | ||
| - name: Set up Python | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: ${{ matrix.python-version }} | ||
| - name: Set up PDM | ||
| uses: pdm-project/setup-pdm@v4 | ||
| with: | ||
| python-version: ${{ matrix.python-version }} | ||
| - name: Install dependencies | ||
| run: | | ||
| pdm sync -d | ||
| - name: Run e2e tests | ||
| run: | | ||
| pdm run test:e2e |
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
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
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
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
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,18 @@ | ||
| data: | ||
| type: mock | ||
| load: | ||
| type: constant | ||
| stages: | ||
| - rate: 1 | ||
| duration: 10 | ||
| num_workers: 2 | ||
| api: | ||
| type: chat | ||
| server: | ||
| type: mock | ||
| base_url: http://0.0.0.0:8000 | ||
| report: | ||
| request_lifecycle: | ||
| summary: true | ||
| per_stage: true | ||
| per_request: true | ||
Empty file.
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,22 @@ | ||
| import pytest | ||
|
|
||
| from utils.benchmark import run_benchmark_minimal | ||
|
|
||
|
|
||
| def test_simple_mock_client_benchmark(): | ||
| result = run_benchmark_minimal("e2e/configs/e2e_simple_mock_client.yaml", timeout_sec=None) | ||
| assert result.success, "Benchmark failed" | ||
| assert result.reports, "No reports generated from benchmark" | ||
| assert result.reports["per_request_lifecycle_metrics.json"], "Missing requests report" | ||
| assert result.reports["stage_0_lifecycle_metrics.json"], "Missing stage report" | ||
| assert result.reports["summary_lifecycle_metrics.json"], "Missing summary report" | ||
|
|
||
| requests_report = result.reports["per_request_lifecycle_metrics.json"] | ||
| stage_report = result.reports["stage_0_lifecycle_metrics.json"] | ||
| summary_report = result.reports["summary_lifecycle_metrics.json"] | ||
|
|
||
| assert len(requests_report) == 10, "the number of requests should be 10" | ||
| assert stage_report["load_summary"]["achieved_rate"] > 1 or stage_report["load_summary"]["achieved_rate"] == pytest.approx( | ||
| 1, abs=0.2 | ||
| ), "the achieved rate should be close to 1.0" | ||
| assert summary_report["successes"]["count"] == 10 |
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,115 @@ | ||
| import json | ||
| import os | ||
| import shlex | ||
| import subprocess | ||
| import tempfile | ||
| import yaml | ||
| import logging | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
| from typing import Any, Dict, Optional, List, Union | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @dataclass | ||
| class BenchmarkResult: | ||
| """Result of a minimal benchmark run.""" | ||
|
|
||
| success: bool # True if process exit code == 0 and not timed out | ||
| timed_out: bool # True if we hit timeout and killed the process | ||
| returncode: int # Raw process return code (or -9/-15 on kill) | ||
| stdout: str # Combined stdout/stderr text | ||
| work_dir: Path # Working directory used for the run | ||
| reports: Optional[Dict[str, Any]] # Parsed json for reports if present | ||
|
|
||
|
|
||
| def _process_yaml_config(config: Union[str, Path, Dict[str, Any]], out_dir: Path) -> Path: | ||
| out_dir.mkdir(parents=True, exist_ok=True) | ||
| cfg_path = out_dir / "config_input.yaml" | ||
|
|
||
| if isinstance(config, (str, Path)): | ||
| src = Path(config) | ||
| if not src.exists(): | ||
| raise FileNotFoundError(f"Config file not found: {src}") | ||
| config = yaml.safe_load(src.read_text(encoding="utf-8")) | ||
|
|
||
| # Overwrite output path to temporaty folder | ||
| config["storage"] = {"local_storage": {"path": out_dir.as_posix()}} | ||
|
|
||
| cfg_path.write_text( | ||
| yaml.safe_dump(config, sort_keys=False, default_flow_style=False), | ||
| encoding="utf-8", | ||
| ) | ||
| return cfg_path | ||
|
|
||
|
|
||
| def _find_report_files(path: Path) -> Optional[List[Path]]: | ||
| """Return the json reports files under path (if any).""" | ||
| candidates = list(path.glob("**/*.json")) | ||
| if not candidates: | ||
| return None | ||
| return candidates | ||
|
|
||
|
|
||
| def run_benchmark_minimal( | ||
| config: Union[str, Path, Dict[str, Any]], | ||
| *, | ||
| work_dir: Optional[Union[str, Path]] = None, | ||
| executable: str = "inference-perf", | ||
| timeout_sec: Optional[int] = 300, | ||
| extra_env: Optional[Dict[str, str]] = None, | ||
| ) -> BenchmarkResult: | ||
| """ | ||
| Minimal wrapper: | ||
| - materializes config to YAML in work_dir, | ||
| - runs `inference-perf --config_file <config.yml>`, | ||
| - returns success/failure, stdout text, and parsed report.json (if present). | ||
| On timeout: | ||
| - kills the spawned process, | ||
| - marks `timed_out=True`, returns collected stdout up to kill. | ||
| """ | ||
| wd = Path(work_dir) if work_dir else Path(tempfile.mkdtemp(prefix="inference-perf-e2e-")) | ||
| cfg_path = _process_yaml_config(config, wd) | ||
|
|
||
| env = os.environ.copy() | ||
| if extra_env: | ||
| env.update({k: str(v) for k, v in extra_env.items()}) | ||
|
|
||
| cmd = f"{shlex.quote(executable)} --config_file {shlex.quote(str(cfg_path))} --log-level DEBUG" | ||
|
|
||
| timed_out = False | ||
| try: | ||
| proc = subprocess.run( | ||
| cmd, | ||
| cwd=str(wd), | ||
| env=env, | ||
| shell=True, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.STDOUT, | ||
| text=True, | ||
| timeout=timeout_sec, | ||
| ) | ||
| stdout = proc.stdout | ||
| return_code = proc.returncode | ||
| except subprocess.TimeoutExpired as e: | ||
| timed_out = True | ||
| stdout = e.stdout | ||
| return_code = -9 | ||
|
|
||
| success = (return_code == 0) and (not timed_out) | ||
|
|
||
| logger.info("Benchmark output:\n%s", stdout) | ||
|
|
||
| # Attempt to read report.json (optional) | ||
| report_path = _find_report_files(wd) | ||
| reports = {report.name: json.loads(report.read_text(encoding="utf-8")) for report in report_path} if report_path else None | ||
|
|
||
| return BenchmarkResult( | ||
| success=success, | ||
| timed_out=timed_out, | ||
| returncode=return_code, | ||
| stdout=stdout or "", | ||
| work_dir=wd, | ||
| reports=reports, | ||
| ) |
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
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
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
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.