-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f338269
chore: wire native pytest-benchmark regression gating; fix broken ben…
27Bslash6 b156400
chore: annotate benchmark conftest fixture return type
27Bslash6 3bd321b
chore: benchmark robustness — measurement integrity, GIL scaling, uni…
27Bslash6 bf80f09
chore: address CodeRabbit review — divisibility guard + reuse single-…
27Bslash6 f9914d2
Merge remote-tracking branch 'origin/main' into feat/benchmark-harness
27Bslash6 3703a9f
chore: validate n_threads before modulo in GIL benchmark (CodeRabbit)
27Bslash6 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
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
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
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,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 |
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,92 @@ | ||
| """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() | ||
| if n_threads <= 0: | ||
| raise ValueError("n_threads must be greater than zero") | ||
| if ops_total % n_threads != 0: | ||
| raise ValueError(f"ops_total={ops_total} must be evenly divisible by n_threads={n_threads}") | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| per = ops_total // n_threads | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| 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 = single if n_threads == 1 else 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}%") | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| 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() | ||
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.