|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Measure partition() runtime over a fixed set of representative example-docs files. |
| 3 | +
|
| 4 | +Follows the same conventions as the existing scripts/performance tooling: |
| 5 | + - PDFs and images are run with strategy="hi_res". |
| 6 | + - Everything else is run with strategy="fast". |
| 7 | + - Each file is timed over NUM_ITERATIONS runs (after a warmup) and the |
| 8 | + average is recorded, matching time_partition.py behaviour. |
| 9 | +
|
| 10 | +Writes a JSON file mapping each file to its average runtime, plus a ``__total__`` |
| 11 | +key with the wall-clock total. An optional positional argument sets the output |
| 12 | +path (default: scripts/performance/partition-speed-test/benchmark_results.json). |
| 13 | +
|
| 14 | +Also writes the total duration to $GITHUB_OUTPUT as ``duration=<seconds>``. |
| 15 | +
|
| 16 | +Usage: |
| 17 | + uv run --no-sync python scripts/performance/benchmark_partition.py [output.json] |
| 18 | +
|
| 19 | +Environment variables: |
| 20 | + NUM_ITERATIONS number of timed iterations per file (default: 1) |
| 21 | +""" |
| 22 | + |
| 23 | +from __future__ import annotations |
| 24 | + |
| 25 | +import json |
| 26 | +import logging |
| 27 | +import os |
| 28 | +import sys |
| 29 | +import time |
| 30 | +from pathlib import Path |
| 31 | + |
| 32 | +from unstructured.partition.auto import partition |
| 33 | + |
| 34 | +logging.basicConfig(level=logging.INFO, format="%(message)s") |
| 35 | +logger = logging.getLogger(__name__) |
| 36 | + |
| 37 | + |
| 38 | +BENCHMARK_FILES: list[tuple[str, str]] = [ |
| 39 | + # PDFs - hi_res |
| 40 | + ("example-docs/pdf/a1977-backus-p21.pdf", "hi_res"), |
| 41 | + ("example-docs/pdf/copy-protected.pdf", "hi_res"), |
| 42 | + ("example-docs/pdf/reliance.pdf", "hi_res"), |
| 43 | + ("example-docs/pdf/pdf-with-ocr-text.pdf", "hi_res"), |
| 44 | + # Images - hi_res |
| 45 | + ("example-docs/double-column-A.jpg", "hi_res"), |
| 46 | + ("example-docs/double-column-B.jpg", "hi_res"), |
| 47 | + ("example-docs/embedded-images-tables.jpg", "hi_res"), |
| 48 | + # Other document types - fast |
| 49 | + ("example-docs/contains-pictures.docx", "fast"), |
| 50 | + ("example-docs/example-10k-1p.html", "fast"), |
| 51 | + ("example-docs/science-exploration-1p.pptx", "fast"), |
| 52 | +] |
| 53 | + |
| 54 | +NUM_ITERATIONS: int = int(os.environ.get("NUM_ITERATIONS", "1")) |
| 55 | + |
| 56 | +DEFAULT_OUTPUT = Path(__file__).parent / "partition-speed-test" / "benchmark_results.json" |
| 57 | + |
| 58 | + |
| 59 | +def _warmup(filepath: str) -> None: |
| 60 | + """Run a single fast-strategy partition to warm the process up. |
| 61 | +
|
| 62 | + Mirrors warm_up_process() in time_partition.py: uses a warmup-docs/ |
| 63 | + variant if present, otherwise falls back to the file itself. |
| 64 | + """ |
| 65 | + |
| 66 | + warmup_dir = Path(__file__).parent / "warmup-docs" |
| 67 | + warmup_file = warmup_dir / f"warmup{Path(filepath).suffix}" |
| 68 | + target = str(warmup_file) if warmup_file.exists() else filepath |
| 69 | + partition(target, strategy="fast") |
| 70 | + |
| 71 | + |
| 72 | +def _measure(filepath: str, strategy: str, iterations: int) -> float: |
| 73 | + """Return the average wall-clock seconds for partitioning *filepath*. |
| 74 | +
|
| 75 | + Identical logic to time_partition.measure_execution_time(). |
| 76 | + """ |
| 77 | + |
| 78 | + total = 0.0 |
| 79 | + for _ in range(iterations): |
| 80 | + t0 = time.perf_counter() |
| 81 | + partition(filepath, strategy=strategy) |
| 82 | + total += time.perf_counter() - t0 |
| 83 | + return total / iterations |
| 84 | + |
| 85 | + |
| 86 | +def _set_github_output(key: str, value: str) -> None: |
| 87 | + """Write key=value to $GITHUB_OUTPUT when running in Actions.""" |
| 88 | + gho = os.environ.get("GITHUB_OUTPUT") |
| 89 | + if gho: |
| 90 | + with open(gho, "a") as fh: |
| 91 | + fh.write(f"{key}={value}\n") |
| 92 | + |
| 93 | + |
| 94 | +def main() -> None: |
| 95 | + output_path = Path(sys.argv[1]) if len(sys.argv) > 1 else DEFAULT_OUTPUT |
| 96 | + repo_root = Path(__file__).resolve().parent.parent.parent # scripts/performance/ -> repo root |
| 97 | + |
| 98 | + logger.info("=" * 60) |
| 99 | + logger.info(f"Partition benchmark (NUM_ITERATIONS={NUM_ITERATIONS})") |
| 100 | + logger.info("=" * 60) |
| 101 | + |
| 102 | + results: dict[str, float] = {} |
| 103 | + grand_start = time.perf_counter() |
| 104 | + |
| 105 | + for rel_path, strategy in BENCHMARK_FILES: |
| 106 | + filepath = repo_root / rel_path |
| 107 | + if not filepath.exists(): |
| 108 | + logger.warning(f" WARNING: {rel_path} not found – skipping.") |
| 109 | + continue |
| 110 | + |
| 111 | + logger.info(f" {rel_path} (strategy={strategy}, iterations={NUM_ITERATIONS})") |
| 112 | + _warmup(str(filepath)) |
| 113 | + avg = _measure(str(filepath), strategy, NUM_ITERATIONS) |
| 114 | + results[rel_path] = round(avg, 4) |
| 115 | + logger.info(f" avg {avg:.2f}s") |
| 116 | + |
| 117 | + total_seconds = round(time.perf_counter() - grand_start, 2) |
| 118 | + results["__total__"] = total_seconds |
| 119 | + |
| 120 | + logger.info(f"\nTotal wall-clock time: {total_seconds}s") |
| 121 | + |
| 122 | + # Write JSON results file (consumed by compare_benchmark.py) |
| 123 | + output_path.parent.mkdir(parents=True, exist_ok=True) |
| 124 | + output_path.write_text(json.dumps(results, indent=2) + "\n") |
| 125 | + logger.info(f"Results written to {output_path}") |
| 126 | + |
| 127 | + # Also expose total as a GitHub Actions step output |
| 128 | + _set_github_output("duration", str(int(total_seconds))) |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + main() |
0 commit comments