-
-
Notifications
You must be signed in to change notification settings - Fork 50
Add JIT Compiler Benchmarking for Cortex Operations #330
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
Changes from all commits
332f874
ce15403
0105f98
173f26e
f0ebf61
2adc946
d50b01c
908e3a0
91fa2be
fd09c59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| \# Cortex JIT Benchmark Suite | ||
|
|
||
|
|
||
|
|
||
| This directory contains benchmarks to evaluate the impact of | ||
|
|
||
| Python 3.13's experimental JIT compiler on Cortex operations. | ||
|
|
||
|
|
||
|
|
||
| \## Benchmarks Included | ||
|
|
||
|
|
||
|
|
||
| 1\. CLI Startup Time | ||
|
|
||
| Measures cold start time of the `cortex` CLI. | ||
|
|
||
|
|
||
|
|
||
| 2\. Command Parsing | ||
|
|
||
| Benchmarks argparse-based command parsing overhead. | ||
|
|
||
|
|
||
|
|
||
| 3\. Cache-like Operations | ||
|
|
||
| Simulates dictionary-heavy workloads similar to internal caching. | ||
|
|
||
|
|
||
|
|
||
| 4\. Streaming | ||
|
|
||
| Measures generator and iteration performance. | ||
|
|
||
|
|
||
|
|
||
| \## How to Run | ||
|
|
||
|
|
||
|
|
||
| From this directory: | ||
|
|
||
|
|
||
|
|
||
| PYTHON\_JIT=0 python run\_benchmarks.py | ||
|
|
||
| PYTHON\_JIT=1 python run\_benchmarks.py | ||
|
|
||
|
|
||
|
|
||
| Or simply: | ||
|
|
||
|
|
||
|
|
||
| python run\_benchmarks.py | ||
|
|
||
|
|
||
|
|
||
| \## Findings | ||
|
|
||
|
|
||
|
|
||
| Python 3.13 JIT shows measurable improvements in: | ||
|
|
||
| \- Command parsing | ||
|
|
||
| \- Cache-like workloads | ||
|
|
||
|
|
||
|
|
||
| Streaming and startup times show minimal change, which is expected. | ||
|
|
||
|
|
||
|
|
||
| These results suggest Python JIT provides benefits for hot-path | ||
|
|
||
| operations used by Cortex. | ||
|
|
||
|
|
||
|
|
||
SWAROOP323 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import time | ||
|
|
||
|
|
||
| def benchmark() -> float: | ||
| """ | ||
| Benchmark cache-like dictionary operations. | ||
|
|
||
| This simulates a hot-path workload similar to internal caching | ||
| mechanisms used by Cortex, measuring insert and lookup performance. | ||
| """ | ||
| cache: dict[str, str] = {} | ||
|
|
||
| start = time.perf_counter() | ||
| for i in range(100_000): | ||
| key = f"prompt_{i}" | ||
| cache[key] = f"response_{i}" | ||
| _ = cache.get(key) | ||
| return time.perf_counter() - start | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| duration = benchmark() | ||
| print(f"Cache-like Operations Time: {duration:.4f} seconds") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import subprocess | ||
| import time | ||
|
|
||
|
|
||
| def benchmark(): | ||
| start = time.perf_counter() | ||
| subprocess.run( | ||
| ["python", "-m", "cortex", "--help"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL | ||
| ) | ||
| return time.perf_counter() - start | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| runs = 5 | ||
| times = [benchmark() for _ in range(runs)] | ||
| print(f"CLI Startup Avg: {sum(times)/runs:.4f} seconds") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import time | ||
| import argparse | ||
| from cortex.cli import CortexCLI | ||
|
|
||
|
|
||
| def benchmark(): | ||
| cli = CortexCLI(verbose=False) | ||
|
|
||
| start = time.perf_counter() | ||
| for _ in range(3000): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("command", nargs="?") | ||
| parser.parse_args(["status"]) | ||
SWAROOP323 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return time.perf_counter() - start | ||
SWAROOP323 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| duration = benchmark() | ||
| print(f"Command Parsing Time: {duration:.4f} seconds") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import time | ||
|
|
||
|
|
||
| def fake_stream(): | ||
| yield from range(2000) | ||
|
|
||
|
|
||
| def benchmark(): | ||
| start = time.perf_counter() | ||
| for _ in fake_stream(): | ||
| pass | ||
| return time.perf_counter() - start | ||
SWAROOP323 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| duration = benchmark() | ||
| print(f"Streaming Time: {duration:.4f} seconds") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import subprocess | ||
| import os | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| PROJECT_ROOT = Path(__file__).resolve().parents[1] | ||
|
|
||
| benchmarks = [ | ||
| "benchmark_cli_startup.py", | ||
| "benchmark_command_parsing.py", | ||
| "benchmark_cache_ops.py", | ||
| "benchmark_streaming.py", | ||
| ] | ||
|
|
||
|
|
||
| def run(jit_enabled): | ||
| env = os.environ.copy() | ||
| env["PYTHON_JIT"] = "1" if jit_enabled else "0" | ||
SWAROOP323 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Add project root to PYTHONPATH | ||
| env["PYTHONPATH"] = str(PROJECT_ROOT) | ||
|
|
||
| print("\n==============================") | ||
| print("JIT ENABLED:" if jit_enabled else "JIT DISABLED:") | ||
| print("==============================") | ||
|
|
||
| for bench in benchmarks: | ||
| subprocess.run([sys.executable, bench], env=env) | ||
|
Comment on lines
+16
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Add type hints and docstring. The function lacks type hints and a docstring, which are required by the coding guidelines. 🔎 Proposed fix-def run(jit_enabled):
+def run(jit_enabled: bool) -> None:
+ """Run all benchmarks with the specified JIT configuration.
+
+ Args:
+ jit_enabled: Whether to enable Python 3.13+ JIT compilation.
+ """
env = os.environ.copy()
env["PYTHON_JIT"] = "1" if jit_enabled else "0"As per coding guidelines, type hints and docstrings are required for all public APIs. 🤖 Prompt for AI AgentsCritical: Benchmark execution will fail when run from outside the benchmarks directory. Line 27 uses relative filenames ( 🔎 Proposed fix def run(jit_enabled):
env = os.environ.copy()
env["PYTHON_JIT"] = "1" if jit_enabled else "0"
# Add project root to PYTHONPATH
env["PYTHONPATH"] = str(PROJECT_ROOT)
print("\n==============================")
print("JIT ENABLED:" if jit_enabled else "JIT DISABLED:")
print("==============================")
+ # Set working directory to benchmarks folder
+ benchmarks_dir = Path(__file__).resolve().parent
+
for bench in benchmarks:
- subprocess.run([sys.executable, bench], env=env)
+ subprocess.run([sys.executable, bench], env=env, cwd=benchmarks_dir)Alternatively, construct absolute paths: + benchmarks_dir = Path(__file__).resolve().parent
+
for bench in benchmarks:
- subprocess.run([sys.executable, bench], env=env)
+ bench_path = benchmarks_dir / bench
+ subprocess.run([sys.executable, str(bench_path)], env=env)
🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| run(False) | ||
| run(True) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| try: | ||
| from rapidfuzz import process | ||
| except ImportError: | ||
| process = None | ||
|
|
||
| from cortex.branding import console, cx_print | ||
|
|
||
| # Temporary known package data (can be expanded later) | ||
| KNOWN_PACKAGES = [ | ||
| { | ||
| "name": "apache2", | ||
| "description": "Popular HTTP web server", | ||
| "downloads": 50000000, | ||
| "rating": 4.7, | ||
| "tags": ["web server", "http", "apache"], | ||
| }, | ||
| { | ||
| "name": "nginx", | ||
| "description": "High-performance event-driven web server", | ||
| "downloads": 70000000, | ||
| "rating": 4.9, | ||
| "tags": ["web server", "reverse proxy"], | ||
| }, | ||
| { | ||
| "name": "docker", | ||
| "description": "Container runtime", | ||
| "downloads": 100000000, | ||
| "rating": 4.8, | ||
| "tags": ["containers", "devops"], | ||
| }, | ||
| ] | ||
|
|
||
|
|
||
| def suggest_alternatives(query: str, limit: int = 3): | ||
| names = [pkg["name"] for pkg in KNOWN_PACKAGES] | ||
| if process is None: | ||
| return [] | ||
| matches = process.extract(query, names, limit=limit) | ||
|
|
||
| results = [] | ||
| for name, score, _ in matches: | ||
| pkg = next(p for p in KNOWN_PACKAGES if p["name"] == name) | ||
| results.append(pkg) | ||
|
|
||
| return results | ||
|
|
||
|
|
||
| def show_suggestions(packages): | ||
| cx_print("💡 Did you mean:", "info") | ||
|
|
||
| for i, pkg in enumerate(packages, 1): | ||
| console.print( | ||
| f"\n{i}. [bold]{pkg['name']}[/bold] (recommended)\n" | ||
| f" - {pkg['description']}\n" | ||
| f" - {pkg['downloads']:,} downloads\n" | ||
| f" - Rating: {pkg['rating']}/5" | ||
| ) | ||
SWAROOP323 marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+48
to
+57
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,17 @@ | ||||||||
| from cortex.suggestions.package_suggester import suggest_alternatives | ||||||||
|
|
||||||||
|
|
||||||||
| def test_suggests_apache_for_apache_server(): | ||||||||
| results = suggest_alternatives("apache-server") | ||||||||
| names = [pkg["name"] for pkg in results] | ||||||||
| assert "apache2" in names | ||||||||
|
|
||||||||
|
|
||||||||
| def test_suggest_returns_list(): | ||||||||
SWAROOP323 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| results = suggest_alternatives("randompkg") | ||||||||
| assert isinstance(results, list) | ||||||||
|
|
||||||||
|
|
||||||||
| def test_suggest_with_exact_match(): | ||||||||
| results = suggest_alternatives("apache2") | ||||||||
| assert results[0]["name"] == "apache2" | ||||||||
SWAROOP323 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| assert results[0]["name"] == "apache2" | |
| names = [pkg["name"] for pkg in results] | |
| assert "apache2" in names |
Uh oh!
There was an error while loading. Please reload this page.