|
1 | | -"""Run the local Lighthouse benchmark with persistent caching.""" |
| 1 | +"""Run the local Lighthouse benchmark with a fresh app build.""" |
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import contextlib |
6 | 6 | import io |
| 7 | +import shutil |
| 8 | +from collections.abc import Callable |
7 | 9 | from pathlib import Path |
8 | 10 |
|
9 | 11 | from tests.integration.lighthouse_utils import ( |
10 | | - get_local_cached_app_root, |
| 12 | + LIGHTHOUSE_APP_NAME, |
| 13 | + LIGHTHOUSE_LANDING_APP_NAME, |
| 14 | + LighthouseBenchmarkResult, |
11 | 15 | run_blank_prod_lighthouse_benchmark, |
| 16 | + run_landing_prod_lighthouse_benchmark, |
12 | 17 | ) |
13 | 18 |
|
14 | 19 |
|
15 | | -def main() -> int: |
16 | | - """Run the Lighthouse benchmark and print a compact summary. |
| 20 | +def _run_benchmark( |
| 21 | + run_fn: Callable[..., LighthouseBenchmarkResult], |
| 22 | + app_root: Path, |
| 23 | + report_path: Path, |
| 24 | +) -> LighthouseBenchmarkResult: |
| 25 | + """Run a single benchmark, suppressing internal output. |
17 | 26 |
|
18 | 27 | Returns: |
19 | | - The process exit code. |
| 28 | + The benchmark result. |
20 | 29 | """ |
21 | | - app_root = get_local_cached_app_root() |
22 | | - report_path = Path(".states") / "lighthouse" / "blank-prod-lighthouse.json" |
23 | | - |
| 30 | + shutil.rmtree(app_root, ignore_errors=True) |
24 | 31 | stdout_buffer = io.StringIO() |
25 | 32 | stderr_buffer = io.StringIO() |
26 | 33 | with ( |
27 | 34 | contextlib.redirect_stdout(stdout_buffer), |
28 | 35 | contextlib.redirect_stderr(stderr_buffer), |
29 | 36 | ): |
30 | | - result = run_blank_prod_lighthouse_benchmark( |
31 | | - app_root=app_root, |
32 | | - report_path=report_path, |
33 | | - ) |
34 | | - |
35 | | - print(result.summary) # noqa: T201 |
36 | | - if result.failures: |
37 | | - return 1 |
38 | | - return 0 |
| 37 | + return run_fn(app_root=app_root, report_path=report_path) |
| 38 | + |
| 39 | + |
| 40 | +def main() -> int: |
| 41 | + """Run the Lighthouse benchmarks and print compact summaries. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + The process exit code. |
| 45 | + """ |
| 46 | + report_dir = Path(".states") / "lighthouse" |
| 47 | + all_failures = [] |
| 48 | + |
| 49 | + benchmarks = [ |
| 50 | + ( |
| 51 | + LIGHTHOUSE_APP_NAME, |
| 52 | + run_blank_prod_lighthouse_benchmark, |
| 53 | + report_dir / "blank-prod-lighthouse.json", |
| 54 | + ), |
| 55 | + ( |
| 56 | + LIGHTHOUSE_LANDING_APP_NAME, |
| 57 | + run_landing_prod_lighthouse_benchmark, |
| 58 | + report_dir / "landing-prod-lighthouse.json", |
| 59 | + ), |
| 60 | + ] |
| 61 | + |
| 62 | + for name, run_fn, report_path in benchmarks: |
| 63 | + app_root = Path(".states") / name |
| 64 | + result = _run_benchmark(run_fn, app_root, report_path) |
| 65 | + print(result.summary) # noqa: T201 |
| 66 | + print() # noqa: T201 |
| 67 | + all_failures.extend(result.failures) |
| 68 | + |
| 69 | + return 1 if all_failures else 0 |
39 | 70 |
|
40 | 71 |
|
41 | 72 | if __name__ == "__main__": |
|
0 commit comments