-
-
Notifications
You must be signed in to change notification settings - Fork 11.3k
perf: release JS parse trees between symbol-fact collection files #3353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VasuBansal7576
wants to merge
3
commits into
Graphify-Labs:v8
Choose a base branch
from
VasuBansal7576:codex/stream-js-symbol-facts
base: v8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+578
−43
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| """Compare JS fact collection or extraction in two existing git checkouts. | ||
|
|
||
| Example (run with the same Python environment for both checkouts):: | ||
|
|
||
| python scripts/benchmark_js_fact_collection.py \ | ||
| --baseline ../graphify-base --candidate . --source ../openclaw \ | ||
| --files 2000 --runs 5 --output /tmp/js-facts.json | ||
|
|
||
| Use --mode graph to also verify ordered serialized extraction output. Each | ||
| measurement runs in a fresh process, alternates baseline/candidate order, and | ||
| uses the same seeded sample. No filesystem-cache flush is attempted. RSS is | ||
| process high-water memory before result serialization, not total system memory | ||
| or the sum of graph extraction workers. Results are measurements, not CI gates. | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import hashlib | ||
| import json | ||
| import os | ||
| from pathlib import Path | ||
| import platform | ||
| import random | ||
| import statistics | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
|
|
||
|
|
||
| def git(checkout: Path, *args: str) -> str: | ||
| return subprocess.check_output( | ||
| ["git", "-C", str(checkout), *args], text=True, | ||
| ).strip() | ||
|
|
||
|
|
||
| def worker(source: Path, manifest: Path, mode: str) -> None: | ||
| import contextlib | ||
| from dataclasses import asdict, fields | ||
| from importlib.metadata import version | ||
| import resource | ||
| import time | ||
|
|
||
| from graphify.extract import _raise_recursion_limit, extract | ||
| from graphify.extractors import resolution | ||
| from graphify.extractors.models import _SymbolResolutionFacts | ||
| from graphify.extractors.resolution import _collect_js_symbol_resolution_facts | ||
|
|
||
| _raise_recursion_limit() | ||
| paths = [Path(name) for name in json.loads(manifest.read_text())] | ||
| os.chdir(source) | ||
| # A new empty cache directory for every graph run prevents one arm from | ||
| # benefiting from the other's serialized extraction cache. | ||
| with tempfile.TemporaryDirectory() as cache, contextlib.redirect_stdout(sys.stderr): | ||
| start = time.perf_counter() | ||
| if mode == "facts": | ||
| result = _SymbolResolutionFacts() | ||
| _collect_js_symbol_resolution_facts(paths, result) | ||
| else: | ||
| result = extract([source / path for path in paths], root=source, | ||
| cache_root=Path(cache), max_workers=2) | ||
| elapsed = time.perf_counter() - start | ||
| peak_rss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss | ||
|
|
||
| if mode == "facts": | ||
| counts = {field.name: len(getattr(result, field.name)) for field in fields(result)} | ||
| result = asdict(result) | ||
| failed_sources = [] | ||
| else: | ||
| counts = {name: len(result[name]) for name in ("nodes", "edges")} | ||
| failed_sources = result["failed_sources"] | ||
|
|
||
| def encode_path(value): | ||
| if isinstance(value, Path): | ||
| return str(value.relative_to(source) if value.is_absolute() else value) | ||
| raise TypeError(f"Unexpected output type: {type(value).__name__}") | ||
|
|
||
| # Sort object keys only. List order is part of the compatibility contract. | ||
| serialized = json.dumps(result, sort_keys=True, separators=(",", ":"), default=encode_path) | ||
| print(json.dumps({ | ||
| "elapsed_seconds": elapsed, | ||
| "peak_rss_mib": peak_rss / (1024 ** 2 if sys.platform == "darwin" else 1024), | ||
| "output_sha256": hashlib.sha256(serialized.encode()).hexdigest(), | ||
| "output_bytes": len(serialized.encode()), | ||
| "counts": counts, | ||
| "failed_sources": failed_sources, | ||
| "loaded_resolution_sha256": hashlib.sha256( | ||
| Path(resolution.__file__).read_bytes()).hexdigest(), | ||
| "python": sys.version, | ||
| "dependencies": {name: version(name) for name in ( | ||
| "tree-sitter", "tree-sitter-javascript", "tree-sitter-typescript", | ||
| )}, | ||
| })) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("--baseline", type=Path) | ||
| parser.add_argument("--candidate", type=Path) | ||
| parser.add_argument("--source", required=True, type=Path) | ||
| parser.add_argument("--files", type=int, default=2000, help="0 selects all eligible files") | ||
| parser.add_argument("--runs", type=int, default=5) | ||
| parser.add_argument("--seed", type=int, default=0) | ||
| parser.add_argument("--mode", choices=("facts", "graph"), default="facts") | ||
| parser.add_argument("--output", type=Path) | ||
| parser.add_argument("--worker-manifest", type=Path, help=argparse.SUPPRESS) | ||
| args = parser.parse_args() | ||
| source = args.source.resolve() | ||
| if args.worker_manifest: | ||
| worker(source, args.worker_manifest, args.mode) | ||
| return | ||
| if not args.baseline or not args.candidate or not args.output: | ||
| parser.error("--baseline, --candidate and --output are required") | ||
| if args.files < 0 or args.runs < 1: | ||
| parser.error("--files must be nonnegative and --runs must be positive") | ||
|
|
||
| checkouts = {"baseline": args.baseline.resolve(), "candidate": args.candidate.resolve()} | ||
| # Read tracked paths, so untracked outputs and dependency installations | ||
| # never silently change the corpus. Suffixes mirror the collector contract. | ||
| suffixes = {".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs", ".mts", ".cts", | ||
| ".vue", ".svelte"} | ||
| tracked = subprocess.check_output(["git", "-C", str(source), "ls-files", "-z"]) | ||
| eligible = sorted(name.decode() for name in tracked.split(b"\0") | ||
| if name and Path(name.decode()).suffix in suffixes) | ||
| if not eligible: | ||
| parser.error("source has no tracked JS/TS files") | ||
| selected = eligible if not args.files else sorted( | ||
| random.Random(args.seed).sample(eligible, min(args.files, len(eligible))) | ||
| ) | ||
| corpus_hash = hashlib.sha256() | ||
| for name in selected: | ||
| corpus_hash.update(name.encode() + b"\0") | ||
| corpus_hash.update(hashlib.sha256((source / name).read_bytes()).digest()) | ||
| receipt = { | ||
| "mode": args.mode, "seed": args.seed, "files": selected, | ||
| "corpus_sha256": corpus_hash.hexdigest(), | ||
| "source_commit": git(source, "rev-parse", "HEAD"), | ||
| "source_status": git(source, "status", "--porcelain", "--untracked-files=no"), | ||
| "platform": platform.platform(), "machine": platform.machine(), | ||
| "checkouts": {arm: { | ||
| "commit": git(path, "rev-parse", "HEAD"), | ||
| "status": git(path, "status", "--porcelain"), | ||
| "resolution_sha256": hashlib.sha256( | ||
| (path / "graphify/extractors/resolution.py").read_bytes()).hexdigest(), | ||
| } for arm, path in checkouts.items()}, | ||
| "measurements": [], | ||
| } | ||
| output = args.output.resolve() | ||
| output.parent.mkdir(parents=True, exist_ok=True) | ||
| with tempfile.TemporaryDirectory() as scratch: | ||
| manifest = Path(scratch) / "files.json" | ||
| manifest.write_text(json.dumps(selected)) | ||
| for repeat in range(args.runs): | ||
| order = ("baseline", "candidate") if repeat % 2 == 0 else ("candidate", "baseline") | ||
| for arm in order: | ||
| env = dict(os.environ, PYTHONPATH=str(checkouts[arm]), PYTHONHASHSEED="0") | ||
| run = subprocess.run([ | ||
| sys.executable, str(Path(__file__).resolve()), "--source", str(source), | ||
| "--mode", args.mode, "--worker-manifest", str(manifest), | ||
| ], cwd=checkouts[arm], env=env, capture_output=True, text=True) | ||
| if run.returncode: | ||
| print(run.stderr, file=sys.stderr) | ||
| raise SystemExit(f"{arm} worker exited {run.returncode}") | ||
| measurement = dict(json.loads(run.stdout), arm=arm, repeat=repeat + 1) | ||
| if measurement["loaded_resolution_sha256"] != receipt["checkouts"][arm]["resolution_sha256"]: | ||
| raise SystemExit(f"Wrong collector loaded for {arm}") | ||
| receipt["measurements"].append(measurement) | ||
| output.write_text(json.dumps(receipt, indent=2) + "\n") | ||
| print(f"{arm} {repeat + 1}: {measurement['elapsed_seconds']:.3f}s, " | ||
| f"{measurement['peak_rss_mib']:.1f} MiB", flush=True) | ||
|
|
||
| hashes = {item["output_sha256"] for item in receipt["measurements"]} | ||
| receipt["ordered_output_equal"] = len(hashes) == 1 | ||
| receipt["extraction_complete"] = all( | ||
| not item["failed_sources"] for item in receipt["measurements"] | ||
| ) | ||
| summary = {} | ||
| for arm in checkouts: | ||
| summary[arm] = {} | ||
| for metric in ("elapsed_seconds", "peak_rss_mib"): | ||
| values = [item[metric] for item in receipt["measurements"] if item["arm"] == arm] | ||
| summary[arm][metric] = { | ||
| "median": statistics.median(values), "min": min(values), "max": max(values), | ||
| } | ||
| receipt["summary"] = summary | ||
| output.write_text(json.dumps(receipt, indent=2) + "\n") | ||
| print(json.dumps(receipt["summary"], indent=2)) | ||
| if not receipt["ordered_output_equal"]: | ||
| raise SystemExit("FAIL: ordered outputs differ; see measurement hashes") | ||
| if not receipt["extraction_complete"]: | ||
| raise SystemExit("FAIL: extraction has failed sources; see measurement receipts") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.