|
| 1 | +"""Synthetic corpora for parse/export/search performance benchmarks.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +from copy import deepcopy |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from app import create_app |
| 12 | + |
| 13 | +FIXTURES = Path(__file__).resolve().parents[1] / "fixtures" |
| 14 | +TEMPLATE_LINE = (FIXTURES / "session_with_tools.jsonl").read_text(encoding="utf-8").splitlines()[0] |
| 15 | + |
| 16 | + |
| 17 | +def write_jsonl(path: Path, line_count: int) -> Path: |
| 18 | + """Write a JSONL session file with *line_count* rows derived from the template fixture.""" |
| 19 | + template = json.loads(TEMPLATE_LINE) |
| 20 | + with path.open("w", encoding="utf-8") as f: |
| 21 | + for i in range(line_count): |
| 22 | + entry = deepcopy(template) |
| 23 | + entry["timestamp"] = f"2026-06-12T10:{i % 60:02d}:00Z" |
| 24 | + if i % 3 == 1: |
| 25 | + msg = entry.setdefault("message", {}) |
| 26 | + if isinstance(msg, dict) and "content" in msg: |
| 27 | + msg["content"] = [{"type": "text", "text": f"benchmark token {i} searchable"}] |
| 28 | + # json.dumps for file I/O — jsonify is Flask's HTTP helper, not file serialization. |
| 29 | + serialized = ( |
| 30 | + json.dumps(entry, separators=(",", ":")) + "\n" # linters-ignore: prefer-jsonify |
| 31 | + ) |
| 32 | + f.write(serialized) |
| 33 | + return path |
| 34 | + |
| 35 | + |
| 36 | +def seed_search_corpus( |
| 37 | + base_dir: Path, |
| 38 | + *, |
| 39 | + session_count: int = 50, |
| 40 | + lines_per_session: int = 20, |
| 41 | +) -> Path: |
| 42 | + """Create a multi-session project tree under *base_dir* for search benchmarks.""" |
| 43 | + project = base_dir / "bench-project" |
| 44 | + project.mkdir(parents=True, exist_ok=True) |
| 45 | + for i in range(session_count): |
| 46 | + write_jsonl(project / f"session_{i:04d}.jsonl", lines_per_session) |
| 47 | + return base_dir |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture(scope="session") |
| 51 | +def parse_small_file(tmp_path_factory: pytest.TempPathFactory) -> Path: |
| 52 | + root = tmp_path_factory.mktemp("bench") |
| 53 | + return write_jsonl(root / "small.jsonl", 10) |
| 54 | + |
| 55 | + |
| 56 | +@pytest.fixture(scope="session") |
| 57 | +def parse_medium_file(tmp_path_factory: pytest.TempPathFactory) -> Path: |
| 58 | + root = tmp_path_factory.mktemp("bench") |
| 59 | + return write_jsonl(root / "medium.jsonl", 500) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture(scope="session") |
| 63 | +def parse_large_file(tmp_path_factory: pytest.TempPathFactory) -> Path: |
| 64 | + root = tmp_path_factory.mktemp("bench") |
| 65 | + return write_jsonl(root / "large.jsonl", 5000) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.fixture |
| 69 | +def export_corpus(tmp_path: Path, request: pytest.FixtureRequest) -> Path: |
| 70 | + """Project dir with N session files. Parametrize N via indirect fixture.""" |
| 71 | + count = request.param |
| 72 | + project = tmp_path / "bench-project" |
| 73 | + project.mkdir() |
| 74 | + for i in range(count): |
| 75 | + write_jsonl(project / f"session_{i:04d}.jsonl", 20) |
| 76 | + return project |
| 77 | + |
| 78 | + |
| 79 | +@pytest.fixture |
| 80 | +def bench_client_search_corpus(tmp_path: Path): |
| 81 | + """Flask test client backed by a 50-session synthetic project tree.""" |
| 82 | + seed_search_corpus(tmp_path) |
| 83 | + app = create_app(base_dir=str(tmp_path)) |
| 84 | + app.config["TESTING"] = True |
| 85 | + return app.test_client() |
0 commit comments