askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
end_to_end_create.py · 3.8 KB · 104 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """End-to-end: what does swapping the torn-tail scan do to a real append-mode |
| 3 | create? Monkeypatches object_records._repair_torn_tail IN THIS PROCESS ONLY |
| 4 | (the repo's source is not edited) with a copy of its logic that scans bytes |
| 5 | with a pluggable function, then times create_collection_record with the |
| 6 | records cache warm -- the situation of any collection that has been listed. |
| 7 | |
| 8 | python experiments/end_to_end_create.py --rows 20000,100000 --creates 12 |
| 9 | """ |
| 10 | from __future__ import annotations |
| 11 | |
| 12 | import argparse |
| 13 | import hashlib |
| 14 | import statistics |
| 15 | import sys |
| 16 | import tempfile |
| 17 | import time |
| 18 | from pathlib import Path |
| 19 | |
| 20 | REPO = Path(__file__).resolve().parents[3] |
| 21 | sys.path.insert(0, str(REPO)) |
| 22 | sys.path.insert(0, str(Path(__file__).resolve().parents[1])) |
| 23 | sys.path.insert(0, str(Path(__file__).resolve().parent)) |
| 24 | |
| 25 | import object_records as R # noqa: E402 |
| 26 | import storage_bench # noqa: E402 |
| 27 | from committed_prefix import parity_bytes, rust_bytes # noqa: E402 |
| 28 | |
| 29 | ORIGINAL = R._repair_torn_tail |
| 30 | |
| 31 | |
| 32 | def make_repair(scan): |
| 33 | """Same decisions as the original (skip on a matching warm sidecar, anchor |
| 34 | on a partial one), but the scan is scan(bytes) -> byte offset.""" |
| 35 | def repair(path: Path) -> None: |
| 36 | signature = R._stat_signature(path) |
| 37 | if signature is None: |
| 38 | return |
| 39 | size = signature[1] |
| 40 | if size == 0: |
| 41 | return |
| 42 | cache_key = str(path.resolve(strict=False)) |
| 43 | anchor = 0 |
| 44 | oidx = R._OIDX_CACHE.get(cache_key) |
| 45 | if oidx is not None: |
| 46 | ino, covered, _ = oidx |
| 47 | if ino == signature[2] and 0 < covered <= size: |
| 48 | if covered == size: |
| 49 | return |
| 50 | anchor = covered |
| 51 | with path.open("rb+") as handle: |
| 52 | handle.seek(anchor) |
| 53 | data = handle.read(size - anchor) |
| 54 | committed = anchor + scan(data) |
| 55 | if committed < size: |
| 56 | handle.truncate(committed) |
| 57 | return repair |
| 58 | |
| 59 | |
| 60 | VARIANTS = {"original": ORIGINAL, "parity (python)": make_repair(parity_bytes)} |
| 61 | if rust_bytes: |
| 62 | VARIANTS["rust (pyo3)"] = make_repair(rust_bytes) |
| 63 | |
| 64 | |
| 65 | def run(variant: str, rows: int, creates: int) -> tuple[float, float, str]: |
| 66 | R._repair_torn_tail = VARIANTS[variant] |
| 67 | d = Path(tempfile.mkdtemp()) |
| 68 | storage_bench.seed(d, "bench", rows, "append") |
| 69 | kw = dict(base_dir=d, roots=[]) |
| 70 | R._RECORDS_CACHE.clear(); R._OIDX_CACHE.clear() |
| 71 | R.list_collection_records("bench", limit=50, **kw) # warm the cache like a page view |
| 72 | R.create_collection_record("bench", {"id": "warmup", "title": "x"}, **kw) |
| 73 | samples = [] |
| 74 | for k in range(creates): |
| 75 | t = time.perf_counter() |
| 76 | R.create_collection_record( |
| 77 | "bench", {"id": f"n{k}", "title": "created", "qty": "1", "status": "open", "owner": "o", "note": "n"}, **kw) |
| 78 | samples.append((time.perf_counter() - t) * 1000) |
| 79 | digest = hashlib.sha256((d / "collections/bench/records.tsv").read_bytes()).hexdigest()[:12] |
| 80 | samples.sort() |
| 81 | return statistics.median(samples), samples[int(len(samples) * 0.95) - 1], digest |
| 82 | |
| 83 | |
| 84 | def main() -> int: |
| 85 | ap = argparse.ArgumentParser() |
| 86 | ap.add_argument("--rows", default="20000,100000") |
| 87 | ap.add_argument("--creates", type=int, default=12) |
| 88 | args = ap.parse_args() |
| 89 | print(f"{'rows':>8} {'variant':<18} {'median ms':>10} {'p95 ms':>9} {'speedup':>8} file sha256[:12]") |
| 90 | for rows in [int(x) for x in args.rows.split(",")]: |
| 91 | base = None |
| 92 | digests = set() |
| 93 | for name in VARIANTS: |
| 94 | med, p95, dig = run(name, rows, args.creates) |
| 95 | base = base or med |
| 96 | digests.add(dig) |
| 97 | print(f"{rows:>8} {name:<18} {med:>10.2f} {p95:>9.2f} {base / med:>7.1f}x {dig}") |
| 98 | print(f"{'':>8} identical file bytes across variants: {'YES' if len(digests) == 1 else 'NO (!)'}") |
| 99 | R._repair_torn_tail = ORIGINAL |
| 100 | return 0 |
| 101 | |
| 102 | |
| 103 | if __name__ == "__main__": |
| 104 | raise SystemExit(main()) |