-
Notifications
You must be signed in to change notification settings - Fork 1
test: add summary-cache pytest-benchmark suite with CI regression gate #120
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
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c126227
test: add summary-cache pytest-benchmark suite with CI regression gate
clean6378-max-it e4d79a6
fix(ci): unblock unittest matrix and seed ubuntu benchmark baselines
clean6378-max-it 83bb248
fix(test): address PR #120 benchmark review (gate, CI, baselines)
clean6378-max-it b1c2f93
fix(test): normalize benchmark names without truncating param values
clean6378-max-it f93160f
fix(ci): address PR #120 review (gate tests in matrix, benchmark-skip…
clean6378-max-it be3f46c
Fix: Restored the standard # via flask comment
clean6378-max-it 33a780e
Fix issue referral numbers
clean6378-max-it 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
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 |
|---|---|---|
|
|
@@ -44,3 +44,5 @@ Thumbs.db | |
| htmlcov/ | ||
| coverage.xml | ||
| .hypothesis/ | ||
| benchmark-results.json | ||
| benchmarks/_raw.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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "_note": "Gated means from local reference run with 1.5x slack (Windows dev host). Refresh from ubuntu-latest CI artifact after first green benchmark job.", | ||
| "updated": "2026-06-25T00:00:00Z", | ||
| "machine": "Windows", | ||
| "groups": { | ||
| "summary-cache": { | ||
| "test_summary_cache_hit": 8.91e-05, | ||
| "test_summary_cache_miss": 8.13e-05, | ||
| "test_fingerprint_workspace_entries[10]": 0.001708, | ||
| "test_fingerprint_workspace_entries[50]": 0.005457, | ||
| "test_fingerprint_workspace_entries[200]": 0.01715, | ||
| "test_summary_cache_round_trip": 0.001667 | ||
| } | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
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,142 @@ | ||
| """Compare pytest-benchmark JSON output against stored baselines.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import json | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| THRESHOLD = 1.20 | ||
|
|
||
|
|
||
| class BenchmarkDataError(ValueError): | ||
| """Raised when benchmark JSON input is malformed or missing required fields.""" | ||
|
|
||
|
|
||
| def load_results(results_path: str | Path) -> dict[str, float]: | ||
| path = Path(results_path) | ||
| try: | ||
| data = json.loads(path.read_text(encoding="utf-8")) | ||
| except OSError as exc: | ||
| raise BenchmarkDataError(f"cannot read {path}: {exc}") from exc | ||
| except json.JSONDecodeError as exc: | ||
| raise BenchmarkDataError(f"invalid JSON in {path}: {exc}") from exc | ||
| try: | ||
| benchmarks = data["benchmarks"] | ||
| except (KeyError, TypeError) as exc: | ||
| raise BenchmarkDataError(f"{path} missing top-level 'benchmarks' array") from exc | ||
| if not isinstance(benchmarks, list): | ||
| raise BenchmarkDataError(f"{path} 'benchmarks' must be an array") | ||
|
|
||
| results: dict[str, float] = {} | ||
| for index, entry in enumerate(benchmarks): | ||
| if not isinstance(entry, dict): | ||
| raise BenchmarkDataError(f"{path} benchmarks[{index}] must be an object") | ||
| try: | ||
| name = entry["name"] | ||
| mean = float(entry["stats"]["mean"]) | ||
| except (KeyError, TypeError, ValueError) as exc: | ||
| raise BenchmarkDataError( | ||
| f"{path} benchmarks[{index}] missing 'name' or 'stats.mean'" | ||
| ) from exc | ||
| name = str(name) | ||
| if name in results: | ||
| raise BenchmarkDataError(f"{path} duplicate benchmark name {name!r}") | ||
| results[name] = mean | ||
| return results | ||
|
|
||
|
|
||
| def load_baseline_means(baselines_path: str | Path) -> dict[str, float]: | ||
| path = Path(baselines_path) | ||
| try: | ||
| data = json.loads(path.read_text(encoding="utf-8")) | ||
| except OSError as exc: | ||
| raise BenchmarkDataError(f"cannot read {path}: {exc}") from exc | ||
| except json.JSONDecodeError as exc: | ||
| raise BenchmarkDataError(f"invalid JSON in {path}: {exc}") from exc | ||
| if not isinstance(data, dict): | ||
| raise BenchmarkDataError(f"{path} root value must be an object") | ||
|
|
||
| if "groups" not in data: | ||
| raise BenchmarkDataError(f"{path} missing required 'groups' key") | ||
| groups = data["groups"] | ||
| if not isinstance(groups, dict): | ||
| raise BenchmarkDataError(f"{path} 'groups' must be an object") | ||
|
|
||
| means: dict[str, float] = {} | ||
| for group_name, value in groups.items(): | ||
| if not isinstance(value, dict): | ||
| continue | ||
| for name, mean in value.items(): | ||
| name = str(name) | ||
| if name in means: | ||
| raise BenchmarkDataError(f"{path} duplicate benchmark name {name!r} across groups") | ||
| try: | ||
| means[name] = float(mean) | ||
| except (TypeError, ValueError) as exc: | ||
| raise BenchmarkDataError( | ||
| f"{path} groups[{group_name!r}][{name!r}] is not a numeric mean" | ||
| ) from exc | ||
| return means | ||
|
|
||
|
|
||
| def check_regression( | ||
| results_path: str | Path, | ||
| baselines_path: str | Path, | ||
| *, | ||
| threshold: float = THRESHOLD, | ||
| ) -> int: | ||
| """Return 0 when within threshold; 1 when any gated benchmark regresses.""" | ||
| flat = load_results(results_path) | ||
| baseline_means = load_baseline_means(baselines_path) | ||
|
|
||
| failures: list[str] = [] | ||
| for name, base in baseline_means.items(): | ||
| cur = flat.get(name) | ||
| if cur is None: | ||
| print(f"WARN: no current result for baseline {name!r}; skipping") | ||
| continue | ||
| if base == 0: | ||
| print(f"WARN: baseline for {name!r} is zero; skipping ratio check") | ||
| continue | ||
| ratio = cur / base | ||
| tag = "FAIL" if ratio > threshold else "ok" | ||
| print(f"[{tag}] {name}: {cur:.6f}s vs {base:.6f}s ({ratio:.2f}x)") | ||
| if ratio > threshold: | ||
| failures.append(name) | ||
|
|
||
|
clean6378-max-it marked this conversation as resolved.
|
||
| for name in flat: | ||
| if name not in baseline_means: | ||
| print(f"WARN: {name!r} has no baseline yet; not gated") | ||
|
|
||
| if failures: | ||
| print(f"\nREGRESSION: {len(failures)} benchmark(s) exceeded {threshold:.0%}") | ||
| return 1 | ||
| return 0 | ||
|
|
||
|
|
||
| def main(argv: list[str] | None = None) -> int: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("results_path", help="pytest-benchmark --benchmark-json output") | ||
| parser.add_argument("baselines_path", help="path to benchmarks/baselines.json") | ||
| parser.add_argument( | ||
| "--threshold", | ||
| type=float, | ||
| default=THRESHOLD, | ||
| help="fail when current mean exceeds baseline by more than this ratio (default: 1.20)", | ||
| ) | ||
| args = parser.parse_args(argv) | ||
| try: | ||
| return check_regression( | ||
| args.results_path, | ||
| args.baselines_path, | ||
| threshold=args.threshold, | ||
| ) | ||
| except BenchmarkDataError as exc: | ||
| print(f"ERROR: {exc}", file=sys.stderr) | ||
| return 2 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
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,89 @@ | ||
| """Synthetic workspace trees for summary-cache performance benchmarks.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import sys | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
|
|
||
| REPO_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | ||
| if REPO_ROOT not in sys.path: | ||
| sys.path.insert(0, REPO_ROOT) | ||
|
|
||
| from services import summary_cache # noqa: E402 | ||
| from services.summary_cache import fingerprint_workspace_storage # noqa: E402 | ||
|
|
||
|
|
||
| def make_workspace_entries(workspace_root: Path, count: int) -> list[dict[str, Any]]: | ||
| """Build *count* synthetic workspace entries with on-disk state files.""" | ||
| entries: list[dict[str, Any]] = [] | ||
| for i in range(count): | ||
| name = f"ws_{i:04d}" | ||
| entry_dir = workspace_root / name | ||
| entry_dir.mkdir(parents=True, exist_ok=True) | ||
| (entry_dir / "state.vscdb").write_bytes(b"bench") | ||
| workspace_json = entry_dir / "workspace.json" | ||
| workspace_json.write_text('{"folder": "/bench"}', encoding="utf-8") | ||
| entries.append( | ||
| { | ||
| "name": name, | ||
| "workspaceJsonPath": str(workspace_json), | ||
| } | ||
| ) | ||
| return entries | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def summary_cache_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: | ||
| """Redirect summary-cache files to an isolated temp directory.""" | ||
| cache_dir = tmp_path / "cache" | ||
| cache_dir.mkdir() | ||
| monkeypatch.setattr(summary_cache, "CACHE_DIR", cache_dir) | ||
| monkeypatch.setattr(summary_cache, "PROJECTS_CACHE_FILE", cache_dir / "projects.json") | ||
| monkeypatch.setattr( | ||
| summary_cache, | ||
| "COMPOSER_MAP_CACHE_FILE", | ||
| cache_dir / "composer-id-to-ws.json", | ||
| ) | ||
|
clean6378-max-it marked this conversation as resolved.
|
||
| return cache_dir | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def sample_projects() -> list[dict[str, Any]]: | ||
| return [ | ||
| { | ||
| "id": "ws_0000", | ||
| "name": "Bench Project", | ||
| "conversationCount": 3, | ||
| "lastModified": "2026-06-24T00:00:00Z", | ||
| } | ||
| ] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def synthetic_workspace(tmp_path: Path, request: pytest.FixtureRequest) -> tuple[str, list[dict[str, Any]]]: | ||
| """Workspace path + entries. Parametrize via indirect ``workspace_entry_count``.""" | ||
| count = getattr(request, "param", 10) | ||
| workspace_root = tmp_path / "workspaceStorage" | ||
| workspace_root.mkdir() | ||
| entries = make_workspace_entries(workspace_root, count) | ||
| return str(workspace_root), entries | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def workspace_fingerprint(synthetic_workspace: tuple[str, list[dict[str, Any]]]) -> dict[str, Any]: | ||
| workspace_path, entries = synthetic_workspace | ||
| return fingerprint_workspace_storage( | ||
| workspace_path, | ||
| entries, | ||
| global_db_path=None, | ||
| rules=[], | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def stale_fingerprint(workspace_fingerprint: dict[str, Any]) -> dict[str, Any]: | ||
| return {**workspace_fingerprint, "global_db_mtime_ns": 9_999_999_999} | ||
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,73 @@ | ||
| """pytest-benchmark coverage for services/summary_cache.py hot paths.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
|
|
||
| from services.summary_cache import ( | ||
| fingerprint_workspace_storage, | ||
| get_cached_projects, | ||
| set_cached_projects, | ||
| ) | ||
|
|
||
| @pytest.mark.benchmark(group="summary-cache") | ||
| def test_summary_cache_hit( | ||
| benchmark, | ||
| summary_cache_dir: Path, | ||
| workspace_fingerprint: dict[str, Any], | ||
| sample_projects: list[dict[str, Any]], | ||
| ) -> None: | ||
| set_cached_projects(workspace_fingerprint, sample_projects, []) | ||
| benchmark(get_cached_projects, workspace_fingerprint) | ||
|
|
||
|
|
||
| @pytest.mark.benchmark(group="summary-cache") | ||
| def test_summary_cache_miss( | ||
| benchmark, | ||
| summary_cache_dir: Path, | ||
| workspace_fingerprint: dict[str, Any], | ||
| stale_fingerprint: dict[str, Any], | ||
| sample_projects: list[dict[str, Any]], | ||
| ) -> None: | ||
| set_cached_projects(workspace_fingerprint, sample_projects, []) | ||
| benchmark(get_cached_projects, stale_fingerprint) | ||
|
clean6378-max-it marked this conversation as resolved.
|
||
|
|
||
|
|
||
| @pytest.mark.benchmark(group="summary-cache") | ||
| @pytest.mark.parametrize( | ||
| "synthetic_workspace", | ||
| [10, 50, 200], | ||
| indirect=True, | ||
| ) | ||
| def test_fingerprint_workspace_entries( | ||
| benchmark, | ||
| synthetic_workspace: tuple[str, list[dict[str, Any]]], | ||
| ) -> None: | ||
| workspace_path, entries = synthetic_workspace | ||
| benchmark( | ||
| fingerprint_workspace_storage, | ||
| workspace_path, | ||
| entries, | ||
| global_db_path=None, | ||
| rules=[], | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.benchmark(group="summary-cache") | ||
| def test_summary_cache_round_trip( | ||
| benchmark, | ||
| summary_cache_dir: Path, | ||
| workspace_fingerprint: dict[str, Any], | ||
| sample_projects: list[dict[str, Any]], | ||
| ) -> None: | ||
| fp = workspace_fingerprint | ||
| projects = sample_projects | ||
|
|
||
| def _run() -> None: | ||
| set_cached_projects(fp, projects, []) | ||
| get_cached_projects(fp) | ||
|
|
||
| benchmark(_run) | ||
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.