-
Notifications
You must be signed in to change notification settings - Fork 0
chore: benchmark suite — fix harness, one-folder consolidation, measurement integrity + GIL + unified runner #188
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,42 @@ | ||
| """Performance-suite fixtures: surface measurement integrity at session start. | ||
|
|
||
| The autouse fixture prints the system fingerprint (+ stable hash) and an environment | ||
| pre-flight verdict once per perf session. It is WARN-ONLY: a non-trustworthy | ||
| environment is reported, never gated — wall-clock benchmarks deliberately do not fail | ||
| the build (see .github/workflows/ci.yml). Tests that want a hard gate can request the | ||
| `measurement_env` fixture and inspect the returned verdict. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Iterator | ||
|
|
||
| import pytest | ||
|
|
||
| from .measurement_env import ( | ||
| EnvVerdict, | ||
| check_measurement_environment, | ||
| fingerprint_hash, | ||
| system_fingerprint, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session", autouse=True) | ||
| def measurement_env() -> Iterator[EnvVerdict]: | ||
| """Print the fingerprint + environment verdict once; yield it for optional gating.""" | ||
| fp = system_fingerprint() | ||
| verdict = check_measurement_environment() | ||
|
|
||
| print(f"\n{'=' * 70}") | ||
| print(f"Measurement environment [fingerprint {fingerprint_hash(fp)}]") | ||
| print(f"{'=' * 70}") | ||
| for key, value in fp.items(): | ||
| print(f" {key:<20} {value}") | ||
| if verdict.trustworthy: | ||
| print(" environment ✓ clean (no throttling / load warnings)") | ||
| else: | ||
| for warning in verdict.warnings: | ||
| print(f" environment ⚠ {warning}") | ||
| print(f"{'=' * 70}\n") | ||
|
|
||
| yield verdict |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,88 @@ | ||||||||||||||||||||||||||||||||||||||
| """GIL / free-threading thread-scaling benchmark for the cachekit serializer. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Runs an identical CPU-bound serialize workload across 1, 2, 4, 8 threads under the | ||||||||||||||||||||||||||||||||||||||
| *current* interpreter and reports speedup + parallel efficiency. Under a stock | ||||||||||||||||||||||||||||||||||||||
| (GIL-enabled) build, efficiency reveals how much the Rust ByteStorage path already | ||||||||||||||||||||||||||||||||||||||
| runs GIL-free; under a free-threaded build it shows the parallel ceiling. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Free-threaded comparison is interpreter-driven, not code-driven: run this same script | ||||||||||||||||||||||||||||||||||||||
| under a free-threaded interpreter and compare the efficiency column. Today that is | ||||||||||||||||||||||||||||||||||||||
| blocked for cachekit — PyO3 < 3.14 has no free-threaded support, and orjson / | ||||||||||||||||||||||||||||||||||||||
| numpy / pandas / pyarrow lack free-threaded wheels — so the no-GIL arm is reported as | ||||||||||||||||||||||||||||||||||||||
| unavailable. Once a free-threaded cachekit installs, no change here is needed: the | ||||||||||||||||||||||||||||||||||||||
| same run produces the no-GIL numbers. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Run: uv run python tests/performance/gil_benchmark.py | ||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| from __future__ import annotations | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| import sys | ||||||||||||||||||||||||||||||||||||||
| import time | ||||||||||||||||||||||||||||||||||||||
| from concurrent.futures import ThreadPoolExecutor | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| from cachekit.serializers import StandardSerializer | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| THREAD_COUNTS = (1, 2, 4, 8) | ||||||||||||||||||||||||||||||||||||||
| OPS_PER_THREAD = 2000 | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def gil_enabled() -> bool: | ||||||||||||||||||||||||||||||||||||||
| """True on a stock GIL build; matches src/cachekit/hiredis_compat.py's check.""" | ||||||||||||||||||||||||||||||||||||||
| return bool(getattr(sys, "_is_gil_enabled", lambda: True)()) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def _payload() -> dict[str, dict[str, object]]: | ||||||||||||||||||||||||||||||||||||||
| """Medium nested dict — exercises msgpack encode + Rust ByteStorage (LZ4 + checksum).""" | ||||||||||||||||||||||||||||||||||||||
| return {f"key_{i}": {"value": f"data_{i}", "count": i, "vals": list(range(20))} for i in range(200)} | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def _work(serializer: StandardSerializer, data: object, ops: int) -> None: | ||||||||||||||||||||||||||||||||||||||
| for _ in range(ops): | ||||||||||||||||||||||||||||||||||||||
| serializer.serialize(data) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def run_threads(n_threads: int, ops_total: int) -> float: | ||||||||||||||||||||||||||||||||||||||
| """Run ops_total serialize calls spread across n_threads; return wall seconds. | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Setup (serializer + payload) happens before the clock starts, so the measurement | ||||||||||||||||||||||||||||||||||||||
| captures only the concurrent serialize work. | ||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||
| serializer = StandardSerializer() | ||||||||||||||||||||||||||||||||||||||
| data = _payload() | ||||||||||||||||||||||||||||||||||||||
| per = ops_total // n_threads | ||||||||||||||||||||||||||||||||||||||
|
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. 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win Guard against non-divisible thread counts silently dropping ops.
🛡️ Proposed defensive check serializer = StandardSerializer()
data = _payload()
+ if ops_total % n_threads != 0:
+ raise ValueError(f"ops_total={ops_total} must be evenly divisible by n_threads={n_threads}")
per = ops_total // n_threads📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
| start = time.perf_counter() | ||||||||||||||||||||||||||||||||||||||
| with ThreadPoolExecutor(max_workers=n_threads) as pool: | ||||||||||||||||||||||||||||||||||||||
| futures = [pool.submit(_work, serializer, data, per) for _ in range(n_threads)] | ||||||||||||||||||||||||||||||||||||||
| for future in futures: | ||||||||||||||||||||||||||||||||||||||
| future.result() | ||||||||||||||||||||||||||||||||||||||
| return time.perf_counter() - start | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def main() -> None: | ||||||||||||||||||||||||||||||||||||||
| gil = gil_enabled() | ||||||||||||||||||||||||||||||||||||||
| label = "GIL ENABLED (stock)" if gil else "FREE-THREADED (no-GIL)" | ||||||||||||||||||||||||||||||||||||||
| ops_total = OPS_PER_THREAD * max(THREAD_COUNTS) # fixed total work across all thread counts | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| print("\nGIL thread-scaling: StandardSerializer.serialize") | ||||||||||||||||||||||||||||||||||||||
| print(f" interpreter: {sys.version.split()[0]} | {label}") | ||||||||||||||||||||||||||||||||||||||
| print(f" total ops: {ops_total} (held constant across thread counts)\n") | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| run_threads(1, max(THREAD_COUNTS)) # warmup | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| single = run_threads(1, ops_total) | ||||||||||||||||||||||||||||||||||||||
| print(f" {'threads':>7} {'wall (s)':>9} {'speedup':>8} {'efficiency':>11}") | ||||||||||||||||||||||||||||||||||||||
| for n_threads in THREAD_COUNTS: | ||||||||||||||||||||||||||||||||||||||
| wall = run_threads(n_threads, ops_total) | ||||||||||||||||||||||||||||||||||||||
| speedup = single / wall | ||||||||||||||||||||||||||||||||||||||
| efficiency = (speedup / n_threads) * 100 | ||||||||||||||||||||||||||||||||||||||
| print(f" {n_threads:>7} {wall:>9.4f} {speedup:>7.2f}x {efficiency:>10.0f}%") | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+71
to
+79
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. 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win Redundant re-measurement for
♻️ Proposed fix to reuse the baseline measurement for n_threads in THREAD_COUNTS:
- wall = run_threads(n_threads, ops_total)
+ wall = single if n_threads == 1 else run_threads(n_threads, ops_total)
speedup = single / wall
efficiency = (speedup / n_threads) * 100📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if gil: | ||||||||||||||||||||||||||||||||||||||
| print("\n No-GIL arm: run this under a free-threaded interpreter to compare the") | ||||||||||||||||||||||||||||||||||||||
| print(" efficiency column. Blocked today for cachekit (PyO3 < 3.14; orjson /") | ||||||||||||||||||||||||||||||||||||||
| print(" numpy / pandas / pyarrow lack free-threaded wheels).") | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||||||||||||||||||||
| main() | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial | ⚡ Quick win
Keep
benchmark-compareunder the Make lint limit.This recipe is seven lines, so
checkmakewill flag it against the configured five-line max. Folding the logging/error handling into a helper target or script would keep the lint green.🧰 Tools
🪛 checkmake (0.3.2)
[warning] 380-380: Target body for "benchmark-compare" exceeds allowed length of 5 lines (7).
(maxbodylength)
🤖 Prompt for AI Agents
Source: Linters/SAST tools