|
| 1 | +"""Quick local benchmark for PDF partition cold/warm timing. |
| 2 | +
|
| 3 | +Examples: |
| 4 | + uv run --active --frozen --no-sync scripts/performance/quick_partition_bench.py \ |
| 5 | + --pdf example-docs/pdf/DA-1p.pdf --strategy fast --repeats 4 --warmups 1 --mode both |
| 6 | +
|
| 7 | + uv run --active --frozen --no-sync scripts/performance/quick_partition_bench.py \ |
| 8 | + --pdf example-docs/pdf/DA-1p.pdf --pdf example-docs/pdf/chevron-page.pdf \ |
| 9 | + --strategy hi_res --repeats 3 --warmups 1 --mode both |
| 10 | +""" |
| 11 | + |
| 12 | +import argparse |
| 13 | +import io |
| 14 | +import json |
| 15 | +import statistics |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | +import time |
| 19 | +from contextlib import redirect_stderr, redirect_stdout |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | +REPO_ROOT = Path(__file__).resolve().parents[2] |
| 23 | +if str(REPO_ROOT) not in sys.path: |
| 24 | + sys.path.insert(0, str(REPO_ROOT)) |
| 25 | + |
| 26 | + |
| 27 | +def _partition_once(pdf: str, strategy: str) -> dict[str, object]: |
| 28 | + sink_out = io.StringIO() |
| 29 | + sink_err = io.StringIO() |
| 30 | + start = time.perf_counter() |
| 31 | + try: |
| 32 | + from unstructured.partition.auto import partition |
| 33 | + |
| 34 | + with redirect_stdout(sink_out), redirect_stderr(sink_err): |
| 35 | + elements = partition(filename=pdf, strategy=strategy) |
| 36 | + return { |
| 37 | + "ok": True, |
| 38 | + "elapsed_s": time.perf_counter() - start, |
| 39 | + "elements": len(elements), |
| 40 | + } |
| 41 | + except Exception as exc: # noqa: BLE001 |
| 42 | + return {"ok": False, "error": f"{type(exc).__name__}: {exc}"} |
| 43 | + |
| 44 | + |
| 45 | +def _summary(values: list[float]) -> dict[str, float]: |
| 46 | + return { |
| 47 | + "mean_s": statistics.mean(values), |
| 48 | + "median_s": statistics.median(values), |
| 49 | + "min_s": min(values), |
| 50 | + "max_s": max(values), |
| 51 | + "stdev_s": statistics.stdev(values) if len(values) > 1 else 0.0, |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | +def _run_cold(pdf: str, strategy: str, repeats: int) -> tuple[list[float], int, list[str]]: |
| 56 | + times: list[float] = [] |
| 57 | + elements = -1 |
| 58 | + errors: list[str] = [] |
| 59 | + |
| 60 | + for _ in range(repeats): |
| 61 | + proc = subprocess.run( |
| 62 | + [ |
| 63 | + sys.executable, |
| 64 | + __file__, |
| 65 | + "--_child", |
| 66 | + "--pdf", |
| 67 | + pdf, |
| 68 | + "--strategy", |
| 69 | + strategy, |
| 70 | + ], |
| 71 | + capture_output=True, |
| 72 | + text=True, |
| 73 | + check=False, |
| 74 | + ) |
| 75 | + |
| 76 | + lines = [line.strip() for line in (proc.stdout or "").splitlines() if line.strip()] |
| 77 | + json_line = next((line for line in reversed(lines) if line.startswith("{")), "") |
| 78 | + if not json_line: |
| 79 | + stderr_tail = (proc.stderr or "").strip().splitlines() |
| 80 | + detail = stderr_tail[-1] if stderr_tail else "no json output" |
| 81 | + errors.append(f"child failed rc={proc.returncode} ({detail})") |
| 82 | + continue |
| 83 | + |
| 84 | + row = json.loads(json_line) |
| 85 | + if bool(row.get("ok")): |
| 86 | + times.append(float(row["elapsed_s"])) |
| 87 | + elements = int(row["elements"]) |
| 88 | + else: |
| 89 | + errors.append(str(row.get("error", "unknown error"))) |
| 90 | + |
| 91 | + return times, elements, errors |
| 92 | + |
| 93 | + |
| 94 | +def _run_warm( |
| 95 | + pdf: str, |
| 96 | + strategy: str, |
| 97 | + repeats: int, |
| 98 | + warmups: int, |
| 99 | +) -> tuple[list[float], int, list[str]]: |
| 100 | + errors: list[str] = [] |
| 101 | + |
| 102 | + for _ in range(warmups): |
| 103 | + row = _partition_once(pdf=pdf, strategy=strategy) |
| 104 | + if not bool(row.get("ok")): |
| 105 | + errors.append(str(row.get("error", "unknown error"))) |
| 106 | + return [], -1, errors |
| 107 | + |
| 108 | + times: list[float] = [] |
| 109 | + elements = -1 |
| 110 | + for _ in range(repeats): |
| 111 | + row = _partition_once(pdf=pdf, strategy=strategy) |
| 112 | + if bool(row.get("ok")): |
| 113 | + times.append(float(row["elapsed_s"])) |
| 114 | + elements = int(row["elements"]) |
| 115 | + else: |
| 116 | + errors.append(str(row.get("error", "unknown error"))) |
| 117 | + |
| 118 | + return times, elements, errors |
| 119 | + |
| 120 | + |
| 121 | +def _collect_pdfs(pdf_args: list[str], pdf_dir_args: list[str]) -> list[str]: |
| 122 | + paths = [str(Path(p)) for p in pdf_args] |
| 123 | + |
| 124 | + for pdf_dir in pdf_dir_args: |
| 125 | + root = Path(pdf_dir) |
| 126 | + if not root.is_dir(): |
| 127 | + raise FileNotFoundError(f"pdf-dir does not exist: {root}") |
| 128 | + paths.extend(str(p) for p in sorted(root.rglob("*.pdf"))) |
| 129 | + |
| 130 | + if not paths: |
| 131 | + raise ValueError("Provide at least one --pdf or --pdf-dir") |
| 132 | + |
| 133 | + deduped = [Path(p) for p in dict.fromkeys(paths)] |
| 134 | + missing = [str(p) for p in deduped if not p.exists()] |
| 135 | + if missing: |
| 136 | + raise FileNotFoundError(f"Missing files: {', '.join(missing)}") |
| 137 | + |
| 138 | + return [str(p) for p in deduped] |
| 139 | + |
| 140 | + |
| 141 | +def _print_mode(label: str, values: list[float], elements: int, errors: list[str]) -> None: |
| 142 | + if not values: |
| 143 | + first = errors[0] if errors else "unknown error" |
| 144 | + print(f" {label} FAILED ({first})", flush=True) |
| 145 | + return |
| 146 | + |
| 147 | + s = _summary(values) |
| 148 | + print( |
| 149 | + f" {label} mean={s['mean_s']:.4f}s median={s['median_s']:.4f}s " |
| 150 | + f"min={s['min_s']:.4f}s max={s['max_s']:.4f}s n={len(values)} elements={elements}", |
| 151 | + flush=True, |
| 152 | + ) |
| 153 | + if errors: |
| 154 | + print(f" {label} partial_failures={len(errors)} first_error={errors[0]}", flush=True) |
| 155 | + |
| 156 | + |
| 157 | +def main() -> None: |
| 158 | + parser = argparse.ArgumentParser(description="Quick local PDF partition benchmark") |
| 159 | + parser.add_argument("--pdf", action="append", default=[], help="PDF path (repeatable)") |
| 160 | + parser.add_argument("--pdf-dir", action="append", default=[], help="Directory of PDFs") |
| 161 | + parser.add_argument("--strategy", default="fast", choices=["fast", "hi_res", "auto"]) |
| 162 | + parser.add_argument("--repeats", type=int, default=3) |
| 163 | + parser.add_argument("--warmups", type=int, default=1) |
| 164 | + parser.add_argument("--mode", default="both", choices=["cold", "warm", "both"]) |
| 165 | + parser.add_argument("--json-out", default="", help="Optional JSON output path") |
| 166 | + parser.add_argument("--_child", action="store_true", help=argparse.SUPPRESS) |
| 167 | + args = parser.parse_args() |
| 168 | + |
| 169 | + if args._child: |
| 170 | + print(json.dumps(_partition_once(args.pdf[0], args.strategy)), flush=True) |
| 171 | + return |
| 172 | + |
| 173 | + pdfs = _collect_pdfs(args.pdf, args.pdf_dir) |
| 174 | + |
| 175 | + print( |
| 176 | + f"strategy={args.strategy} mode={args.mode} repeats={args.repeats} " |
| 177 | + f"warmups={args.warmups} pdf_count={len(pdfs)}", |
| 178 | + flush=True, |
| 179 | + ) |
| 180 | + |
| 181 | + by_mode_times: dict[str, list[float]] = {"cold": [], "warm": []} |
| 182 | + by_mode_file_means: dict[str, list[float]] = {"cold": [], "warm": []} |
| 183 | + by_mode_failed_files: dict[str, int] = {"cold": 0, "warm": 0} |
| 184 | + results: list[dict[str, object]] = [] |
| 185 | + |
| 186 | + for pdf in pdfs: |
| 187 | + row: dict[str, object] = {"pdf": pdf} |
| 188 | + print(f"FILE {pdf}", flush=True) |
| 189 | + |
| 190 | + if args.mode in ("cold", "both"): |
| 191 | + times, elements, errors = _run_cold(pdf, args.strategy, args.repeats) |
| 192 | + _print_mode("cold", times, elements, errors) |
| 193 | + if times: |
| 194 | + by_mode_times["cold"].extend(times) |
| 195 | + by_mode_file_means["cold"].append(statistics.mean(times)) |
| 196 | + row["cold"] = {"ok": True, "times_s": times, "elements": elements, "errors": errors} |
| 197 | + else: |
| 198 | + by_mode_failed_files["cold"] += 1 |
| 199 | + row["cold"] = {"ok": False, "errors": errors} |
| 200 | + |
| 201 | + if args.mode in ("warm", "both"): |
| 202 | + times, elements, errors = _run_warm(pdf, args.strategy, args.repeats, args.warmups) |
| 203 | + _print_mode("warm", times, elements, errors) |
| 204 | + if times: |
| 205 | + by_mode_times["warm"].extend(times) |
| 206 | + by_mode_file_means["warm"].append(statistics.mean(times)) |
| 207 | + row["warm"] = { |
| 208 | + "ok": True, |
| 209 | + "times_s": times, |
| 210 | + "elements": elements, |
| 211 | + "errors": errors, |
| 212 | + "warmups": args.warmups, |
| 213 | + } |
| 214 | + else: |
| 215 | + by_mode_failed_files["warm"] += 1 |
| 216 | + row["warm"] = {"ok": False, "errors": errors, "warmups": args.warmups} |
| 217 | + |
| 218 | + results.append(row) |
| 219 | + |
| 220 | + aggregate: dict[str, object] = {} |
| 221 | + print("AGGREGATE", flush=True) |
| 222 | + for mode in ("cold", "warm"): |
| 223 | + times = by_mode_times[mode] |
| 224 | + if not times: |
| 225 | + continue |
| 226 | + s = _summary(times) |
| 227 | + file_mean = statistics.mean(by_mode_file_means[mode]) |
| 228 | + succeeded = len(by_mode_file_means[mode]) |
| 229 | + failed = by_mode_failed_files[mode] |
| 230 | + aggregate[mode] = { |
| 231 | + "summary": s, |
| 232 | + "file_mean_s": file_mean, |
| 233 | + "samples": len(times), |
| 234 | + "succeeded_files": succeeded, |
| 235 | + "failed_files": failed, |
| 236 | + } |
| 237 | + print( |
| 238 | + f" {mode} succeeded_files={succeeded} failed_files={failed} " |
| 239 | + f"file_mean={file_mean:.4f}s mean={s['mean_s']:.4f}s median={s['median_s']:.4f}s " |
| 240 | + f"min={s['min_s']:.4f}s max={s['max_s']:.4f}s " |
| 241 | + f"stdev={s['stdev_s']:.4f}s samples={len(times)}", |
| 242 | + flush=True, |
| 243 | + ) |
| 244 | + |
| 245 | + if args.json_out: |
| 246 | + out = Path(args.json_out) |
| 247 | + out.parent.mkdir(parents=True, exist_ok=True) |
| 248 | + out.write_text( |
| 249 | + json.dumps( |
| 250 | + { |
| 251 | + "strategy": args.strategy, |
| 252 | + "mode": args.mode, |
| 253 | + "repeats": args.repeats, |
| 254 | + "warmups": args.warmups, |
| 255 | + "pdf_count": len(pdfs), |
| 256 | + "per_file": results, |
| 257 | + "aggregate": aggregate, |
| 258 | + }, |
| 259 | + indent=2, |
| 260 | + ) |
| 261 | + ) |
| 262 | + print(f"json_out={out}", flush=True) |
| 263 | + |
| 264 | + |
| 265 | +if __name__ == "__main__": |
| 266 | + main() |
0 commit comments